simple script to compare 2 values.....

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

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

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

  • @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
                mode = 0;
            } else { // Reset to mode 0 because lights weren't confirmed
                mode = 0;
            timer = millis();

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

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

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

    var myImports = new JavaImporter(, 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";
        //Wait time
        Thread.sleep(500); // in milliseconds

  • @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!

Suggested Topics