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

simple script to compare 2 values.....

Scheduled Pinned Locked Moved Resources
8 Posts 3 Posters 914 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.
  • S Offline
    skywatch
    last edited by 8 Mar 2020, 18:05

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

    J 1 Reply Last reply 9 Mar 2020, 13:42 Reply Quote 1
    • J Offline
      jkandasa @skywatch
      last edited by 9 Mar 2020, 13:42

      @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

      S 1 Reply Last reply 9 Mar 2020, 14:22 Reply Quote 0
      • S Offline
        skywatch @jkandasa
        last edited by skywatch 3 Oct 2020, 15:29 9 Mar 2020, 14:22

        @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......

        A 1 Reply Last reply 9 Mar 2020, 14:57 Reply Quote 0
        • A Offline
          Avamander @skywatch
          last edited by Avamander 3 Sept 2020, 20:31 9 Mar 2020, 14:57

          @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();
              }
          }
          
          S 1 Reply Last reply 10 Mar 2020, 09:41 Reply Quote 0
          • S Offline
            skywatch @Avamander
            last edited by 10 Mar 2020, 09:41

            @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 😉

            A J 2 Replies Last reply 10 Mar 2020, 13:44 Reply Quote 0
            • A Offline
              Avamander @skywatch
              last edited by 10 Mar 2020, 13:44

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

              1 Reply Last reply Reply Quote 0
              • J Offline
                jkandasa @skywatch
                last edited by 6 Apr 2020, 04:10

                @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
                  }
                }
                
                S 1 Reply Last reply 6 Apr 2020, 15:01 Reply Quote 0
                • S Offline
                  skywatch @jkandasa
                  last edited by 6 Apr 2020, 15:01

                  @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

                  629

                  Users

                  532

                  Topics

                  3.4k

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