• Categories
    • Recent
    • Tags
    • Popular
    • Register
    • Login

    simple script to compare 2 values.....

    Scheduled Pinned Locked Moved Resources
    8 Posts 3 Posters 706 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • skywatchS Offline
      skywatch
      last edited by

      How do you compare 2 sensor values in a script to send different messages dependant on the values?

      jkandasaJ 1 Reply Last reply Reply Quote 1
      • jkandasaJ Offline
        jkandasa @skywatch
        last edited by

        @skywatch you have to define uid tags for those sensors and take values and compare with if loop.
        let me know if you need a help

        skywatchS 1 Reply Last reply Reply Quote 0
        • skywatchS Offline
          skywatch @jkandasa
          last edited by skywatch

          @jkandasa Thank you - An example to follow would be very welcome 😉

          Here is what I want to achieve (in Arduino like C++).....

          SensorA = lux level reading...
          if (SensorA <= value && sensor B == 1){ <------This bit I need help with!
          sendmsg.set(lights = 1);
          wait(500); <-------------- This bit I need help with!
          //here test light level to see if lights actually are on by new lux reading sent automatically by node.
          if lights are confirmed on, send success email.
          If lights are confirmed not on, send fail email.

          I can sort out the operation for the email no problem. What I don't know how to do is the comparison and delay in a script.

          All help appreciated 🙂

          I know I can do it all in the node, but I need to learn more about MyController
          capabilities and it will reduce RF network overhead as this is such a simple thing......

          AvamanderA 1 Reply Last reply Reply Quote 0
          • AvamanderA Offline
            Avamander @skywatch
            last edited by Avamander

            @skywatch

            First thing is that delays are a bad thing, they slow your code down, make it impossible to multi-task and at some point the delays definitely get unmanageable. Instead you could do this:

            void setup() {
               // Set your pinModes and everything
            }
            
            
            uint64_t timer = 0;
            uint8_t mode = 0;
            void loop() {
                // You can do a lot more here now, check your sensors every 100 milliseconds if you wish
                // Because this loop runs really really fast unless the timer below triggers
                // Doing a moving average of a brightness sensor wouldn't be a bad idea :)
                if (millis() - timer >= 500) {
                    // It has been ~500ms since the last time this ran
                    if (mode == 0 && sensor_a <= value && sensor_b == 1) { // If it's the first time sensor values match lights on
                        mode = 1;
                    } else if (mode == 1 && sensor_a <= value && sensor_b == 1) { // It is the second time sensor values match lights on
                        sendUpdateToGateway();
                        mode = 0;
                    } else { // Reset to mode 0 because lights weren't confirmed
                        mode = 0;
                    }
                    timer = millis();
                }
            }
            
            skywatchS 1 Reply Last reply Reply Quote 0
            • skywatchS Offline
              skywatch @Avamander
              last edited by

              @Avamander Thanks for the response, but maybe I wasn't that clear about the previous post.

              What I posted was a 'rough' idea of what I want to do in MyController script. I just thought that was a quick way to show what I wanted to do in the script and where I was having trouble working out what to do.

              I do appreciate your time in trying to help me out though 😉

              AvamanderA jkandasaJ 2 Replies Last reply Reply Quote 0
              • AvamanderA Offline
                Avamander @skywatch
                last edited by

                @skywatch A-ha, I understood that you had problems with the MySensors code 😄

                1 Reply Last reply Reply Quote 0
                • jkandasaJ Offline
                  jkandasa @skywatch
                  last edited by

                  @skywatch Sorry for the delayed response,
                  let me know if you need additional help.

                  var myImports = new JavaImporter(java.io, java.lang, java.util, java.text);
                  
                  with(myImports) {
                    var sensorA = mcApi.uidTag().getByUid("sensor-a").getResource();
                    var sensorB = mcApi.uidTag().getByUid("sensor-b").getResource();
                    var sensorLight = mcApi.uidTag().getByUid("sensor-light").getResource();
                   
                    // convert value from string to 
                    // to integer:  parseInt()
                    // to float  :  parseFloat()
                   
                    var sA = parseFloat(sensorA.value)
                    var sB = parseInt(sensorB.value)
                    
                    var val = 20.5;
                    
                    if (sA <= val && sB === 1){
                      sensorLight.value = "1";
                      mcApi.sensor().sendPayload(sensorLight);
                      //Wait time
                      Thread.sleep(500); // in milliseconds
                    }
                  }
                  
                  skywatchS 1 Reply Last reply Reply Quote 0
                  • skywatchS Offline
                    skywatch @jkandasa
                    last edited by

                    @jkandasa said in simple script to compare 2 values.....:

                    @skywatch Sorry for the delayed response,
                    let me know if you need additional help.

                    No problem! - I know you are very busy lately.

                    Thanks for the pointers, I will try them in the next few days I hope!

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post

                    0

                    Online

                    587

                    Users

                    529

                    Topics

                    3.4k

                    Posts
                    Copyright © 2015-2025 MyController.org | Contributors | Localization