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

    Unable to catch status variable in a script

    Scheduled Pinned Locked Moved Troubleshooting
    3 Posts 2 Posters 874 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.
    • D Offline
      Daniele
      last edited by

      Hi,

      I'm quite new to MyController, and I'm starting working with scripts.

      I've got 5 status variables.
      The first 3 represent PIRs status, and I can work with them as expected.
      The last 2 represent a relays status, but every test I do with their value seems to be false.

      This is the script I'm using:

      var myImports = new JavaImporter(java.io, java.lang, java.util);
      
      with(myImports) {
      
      var PIR_C = mcApi.uidTag().getByUid("P0_PIR_Caldaia").getResource();
      var PIR_F = mcApi.uidTag().getByUid("P0_PIR_Finestra").getResource();
      var PIR_S = mcApi.uidTag().getByUid("P0_PIR_Studio").getResource();
      
      var Consenso = mcApi.uidTag().getByUid("P0_Consenso_Caldaia").getResource();
      var Consenso_C = mcApi.uidTag().getByUid("P0_Consenso_Check").getResource();
      
      var PIR = (PIR_C.value == "0" && PIR_F.value == "0" && PIR_S.value == "0");
      var Caldaia = (Consenso == "1" || Consenso_C == "1");
      
      mcResult = (PIR && Caldaia);
      
      }
      

      and this is the result:

      {
        "PIR_C": {
          "id": 8,
          "sensor": {
            "id": 9,
            "sensorId": "1",
            "type": "S_BINARY",
            "name": "PIR Caldaia",
            "lastSeen": 1513095387214,
            "node": {
              "id": 8,
              "eui": "1",
              "gatewayTable": {
                "id": 1,
                "enabled": true,
                "name": "MQTT_Gateway",
                "networkType": "MY_SENSORS",
                "state": "UP",
                "statusMessage": "Connected Successfully",
                "statusSince": 1513073882255,
                "type": "MQTT",
                "properties": {
                  "p": "",
                  "qos": 0,
                  "u": "",
                  "bh": "tcp://localhost:1883",
                  "txDelay": 0,
                  "tp": "casa_in",
                  "cid": "mygateway1",
                  "ts": "casa_out"
                }
              },
              "name": "P0 - PIR Caldaia",
              "version": "1.1",
              "type": "S_ARDUINO_REPEATER_NODE",
              "libVersion": "2.1.1",
              "state": "UP",
              "lastSeen": 1513095504118,
              "properties": {
                "hbTx": 1513095493224
              },
              "parentNodeEui": "0",
              "registrationState": "REGISTERED",
              "smartSleepEnabled": false
            },
            "room": {
              "id": 1,
              "name": "Sala Giochi",
              "fullPath": "Sala Giochi"
            },
            "variableTypes": [
              "Status"
            ],
            "idforVariables": 9
          },
          "variableType": "V_STATUS",
          "metricType": "BINARY",
          "timestamp": 1513095387161,
          "value": "0",
          "previousValue": "0",
          "unitType": "U_NONE",
          "readOnly": false,
          "offset": 0,
          "priority": 100,
          "properties": {
            "marginRight": 20,
            "color": "#ff7f0e",
            "interpolate": "linear",
            "subType": "line",
            "marginBottom": 60,
            "useGlobal": true,
            "type": "lineChart",
            "marginTop": 5,
            "marginLeft": 65
          },
          "metricsGraph": {
            "id": 18,
            "metricName": "Status",
            "type": "lineChart",
            "interpolate": "step-after",
            "subType": "line",
            "color": "#ff7f0e"
          }
        },
        "PIR_S": {
          "id": 1,
          "sensor": {
            "id": 1,
            "sensorId": "1",
            "type": "S_BINARY",
            "name": "PIR Studio",
            "lastSeen": 1513095306409,
            "node": {
              "id": 2,
              "eui": "3",
              "gatewayTable": {
                "id": 1,
                "enabled": true,
                "name": "MQTT_Gateway",
                "networkType": "MY_SENSORS",
                "state": "UP",
                "statusMessage": "Connected Successfully",
                "statusSince": 1513073882255,
                "type": "MQTT",
                "properties": {
                  "p": "",
                  "qos": 0,
                  "u": "",
                  "bh": "tcp://localhost:1883",
                  "txDelay": 0,
                  "tp": "casa_in",
                  "cid": "mygateway1",
                  "ts": "casa_out"
                }
              },
              "name": "P0 - PIR Studio",
              "version": "1.1",
              "type": "S_ARDUINO_NODE",
              "libVersion": "2.1.1",
              "state": "UP",
              "lastSeen": 1513095503748,
              "properties": {
                "hbTx": 1513095493224
              },
              "parentNodeEui": "0",
              "registrationState": "REGISTERED",
              "smartSleepEnabled": true
            },
            "room": {
              "id": 2,
              "name": "Studio",
              "fullPath": "Studio"
            },
            "variableTypes": [
              "Status"
            ],
            "idforVariables": 1
          },
          "variableType": "V_STATUS",
          "metricType": "BINARY",
          "timestamp": 1513095306351,
          "value": "0",
          "previousValue": "0",
          "unitType": "U_NONE",
          "readOnly": false,
          "offset": 0,
          "priority": 100,
          "properties": {
            "marginRight": 20,
            "color": "#ff7f0e",
            "interpolate": "linear",
            "subType": "line",
            "marginBottom": 60,
            "useGlobal": true,
            "type": "lineChart",
            "marginTop": 5,
            "marginLeft": 65
          },
          "metricsGraph": {
            "id": 18,
            "metricName": "Status",
            "type": "lineChart",
            "interpolate": "step-after",
            "subType": "line",
            "color": "#ff7f0e"
          }
        },
        "myImports": {},
        "PIR": true,
        "mcResult": false,
        "PIR_F": {
          "id": 3,
          "sensor": {
            "id": 6,
            "sensorId": "1",
            "type": "S_BINARY",
            "name": "PIR Finestra",
            "lastSeen": 1513095507060,
            "node": {
              "id": 6,
              "eui": "2",
              "gatewayTable": {
                "id": 1,
                "enabled": true,
                "name": "MQTT_Gateway",
                "networkType": "MY_SENSORS",
                "state": "UP",
                "statusMessage": "Connected Successfully",
                "statusSince": 1513073882255,
                "type": "MQTT",
                "properties": {
                  "p": "",
                  "qos": 0,
                  "u": "",
                  "bh": "tcp://localhost:1883",
                  "txDelay": 0,
                  "tp": "casa_in",
                  "cid": "mygateway1",
                  "ts": "casa_out"
                }
              },
              "name": "P0 - PIR Finestra",
              "version": "1.1",
              "type": "S_ARDUINO_NODE",
              "libVersion": "2.1.1",
              "state": "UP",
              "lastSeen": 1513095507080,
              "properties": {
                "hbTx": 1513095493224
              },
              "parentNodeEui": "1",
              "registrationState": "REGISTERED",
              "smartSleepEnabled": true
            },
            "room": {
              "id": 1,
              "name": "Sala Giochi",
              "fullPath": "Sala Giochi"
            },
            "variableTypes": [
              "Status"
            ],
            "idforVariables": 6
          },
          "variableType": "V_STATUS",
          "metricType": "BINARY",
          "timestamp": 1513095507017,
          "value": "0",
          "previousValue": "0",
          "unitType": "U_NONE",
          "readOnly": false,
          "offset": 0,
          "priority": 100,
          "properties": {
            "marginRight": 20,
            "color": "#ff7f0e",
            "interpolate": "linear",
            "subType": "line",
            "marginBottom": 60,
            "useGlobal": true,
            "type": "lineChart",
            "marginTop": 5,
            "marginLeft": 65
          },
          "metricsGraph": {
            "id": 18,
            "metricName": "Status",
            "type": "lineChart",
            "interpolate": "step-after",
            "subType": "line",
            "color": "#ff7f0e"
          }
        },
        "Consenso_C": {
          "id": 19,
          "sensor": {
            "id": 21,
            "sensorId": "3",
            "type": "S_BINARY",
            "name": "Consenso Check",
            "lastSeen": 1513095510243,
            "node": {
              "id": 8,
              "eui": "1",
              "gatewayTable": {
                "id": 1,
                "enabled": true,
                "name": "MQTT_Gateway",
                "networkType": "MY_SENSORS",
                "state": "UP",
                "statusMessage": "Connected Successfully",
                "statusSince": 1513073882255,
                "type": "MQTT",
                "properties": {
                  "p": "",
                  "qos": 0,
                  "u": "",
                  "bh": "tcp://localhost:1883",
                  "txDelay": 0,
                  "tp": "casa_in",
                  "cid": "mygateway1",
                  "ts": "casa_out"
                }
              },
              "name": "P0 - PIR Caldaia",
              "version": "1.1",
              "type": "S_ARDUINO_REPEATER_NODE",
              "libVersion": "2.1.1",
              "state": "UP",
              "lastSeen": 1513095504118,
              "properties": {
                "hbTx": 1513095493224
              },
              "parentNodeEui": "0",
              "registrationState": "REGISTERED",
              "smartSleepEnabled": false
            },
            "room": {
              "id": 1,
              "name": "Sala Giochi",
              "fullPath": "Sala Giochi"
            },
            "variableTypes": [
              "Status"
            ],
            "idforVariables": 21
          },
          "variableType": "V_STATUS",
          "metricType": "BINARY",
          "timestamp": 1513095510222,
          "value": "1",
          "previousValue": "0",
          "unitType": "U_NONE",
          "readOnly": false,
          "offset": 0,
          "priority": 100,
          "properties": {
            "marginRight": 20,
            "color": "#ff7f0e",
            "interpolate": "linear",
            "subType": "line",
            "marginBottom": 60,
            "useGlobal": true,
            "type": "lineChart",
            "marginTop": 5,
            "marginLeft": 65
          },
          "metricsGraph": {
            "id": 18,
            "metricName": "Status",
            "type": "lineChart",
            "interpolate": "step-after",
            "subType": "line",
            "color": "#ff7f0e"
          }
        },
        "Caldaia": false,
        "Consenso": {
          "id": 9,
          "sensor": {
            "id": 10,
            "sensorId": "2",
            "type": "S_BINARY",
            "name": "Consenso Caldaia",
            "lastSeen": 1513094395195,
            "node": {
              "id": 8,
              "eui": "1",
              "gatewayTable": {
                "id": 1,
                "enabled": true,
                "name": "MQTT_Gateway",
                "networkType": "MY_SENSORS",
                "state": "UP",
                "statusMessage": "Connected Successfully",
                "statusSince": 1513073882255,
                "type": "MQTT",
                "properties": {
                  "p": "",
                  "qos": 0,
                  "u": "",
                  "bh": "tcp://localhost:1883",
                  "txDelay": 0,
                  "tp": "casa_in",
                  "cid": "mygateway1",
                  "ts": "casa_out"
                }
              },
              "name": "P0 - PIR Caldaia",
              "version": "1.1",
              "type": "S_ARDUINO_REPEATER_NODE",
              "libVersion": "2.1.1",
              "state": "UP",
              "lastSeen": 1513095504118,
              "properties": {
                "hbTx": 1513095493224
              },
              "parentNodeEui": "0",
              "registrationState": "REGISTERED",
              "smartSleepEnabled": false
            },
            "room": {
              "id": 1,
              "name": "Sala Giochi",
              "fullPath": "Sala Giochi"
            },
            "variableTypes": [
              "Status"
            ],
            "idforVariables": 10
          },
          "variableType": "V_STATUS",
          "metricType": "BINARY",
          "timestamp": 1513094395164,
          "value": "0",
          "previousValue": "1",
          "unitType": "U_NONE",
          "readOnly": false,
          "offset": 0,
          "priority": 100,
          "properties": {
            "marginRight": 20,
            "color": "#ff7f0e",
            "interpolate": "linear",
            "subType": "line",
            "marginBottom": 60,
            "useGlobal": true,
            "type": "lineChart",
            "marginTop": 5,
            "marginLeft": 65
          },
          "metricsGraph": {
            "id": 18,
            "metricName": "Status",
            "type": "lineChart",
            "interpolate": "step-after",
            "subType": "line",
            "color": "#ff7f0e"
          }
        }
      }
      

      As you can see, Consenso_C value is 1 in the output, but the test Consenso_C == "1" is false.

      It's surely some stupid error, but I cannot see where it is...

      Daniele

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

        @daniele

        this line[var Caldaia = (Consenso == "1" || Consenso_C == "1");] should be as follows,

        var Caldaia = (Consenso.value == "1" || Consenso_C.value == "1");
        
        1 Reply Last reply Reply Quote 0
        • D Offline
          Daniele
          last edited by

          What a stupid error!
          Thank you very much!

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

          0

          Online

          587

          Users

          529

          Topics

          3.4k

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