• header.categories
    • header.recent
    • header.tags
    • header.popular
    • register
    • login

    How to set sensor value with javascript

    scheduled pinned locked moved Developers Zone
    8 posts 3 posters 4.0k views 2 watching
    loading-more-posts
    • oldest-to-newest
    • newest-to-oldest
    • most-votes
    reply
    • reply-as-topic
    guest-login-reply
    deleted-message
    • S offline
      seant
      global:last-edited-by,

      Hi

      I have an ultrasonic distance sensor that sends distances in cm to mycontroller. I would like to add 2 virtual sensors, one for litres and one for percentage and the values for these to be calculated and set/saved when the distance sensor values are received.

      I have attempted to do this via a javascript script, but I cannot seem to figure out how to set the values for the virtual sensors.

      Please can you let me know how to save a value for a sensor using javascript.

      Thanks

      jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
      • jkandasaJ offline
        jkandasa @seant
        global:last-edited-by,

        @seant For now there is no event feature in MyController. You can create a virtual sensor and update value with javascript.

        • Map your script with operation
        • Create a rule with real sensor and set threshold as in negative number(ex: > -1). And uncheck ignore duplicate check box. Map your script operation with this rule.
        • Your script will be executed, when you receive value from your sensor.
        one-reply-to-this-post last-reply-time reply quote 0
        • S offline
          seant
          global:last-edited-by, jkandasa

          @jkandasa Thanks I have already done those steps. The issue I have is trying to set a value for the virtual sensor via javascript.
          What I have done thus far is

          1. Create 2 virtual sensors
          2. Create a "operation" script - using javascript
          3. Create a rule that triggers the script when value changes

          I've added my javascript here, but the last 2 lines of code are not setting the virtual sensors values. Can you assist with how to do this please?

          // Get Pump sensor via UID-Tag
          var tankDistanceSensor = mcApi.uidTag().getByUid("water-tank-distance").getResource();
          var tankLitresSensor = mcApi.uidTag().getByUid("water-tank-litres").getResource();
          var tankPercentageSensor = mcApi.uidTag().getByUid("water-tank-percentage").getResource();
          
          //get config in variable
          tankConfig = mcApi.variable().get("water-tank-config");
          fullDistance       = tankConfig.value; // Value 
          emptyDistance = tankConfig.value2; // Value 2
          totalLitres = tankConfig.value3; // Value 3
          
          //Get sensor values with UID Tags
          var pingDistance =  tankDistanceSensor.value; //Water tank level percent
          
          if (pingDistance > 0 && pingDistance < (fullDistance + 50)) {
            percentage =  ((pingDistance - emptyDistance) / (fullDistance - emptyDistance) * 100);
            litres = percentage / 100 * totalLitres;
          
            //THESE 2 LINES DO NOT SEEM TO SET THE VALUES  
            tankLitresSensor.setValue(litres);
            tankPercentageSensor.setValue(percentage);
          
          }
          
          one-reply-to-this-post last-reply-time reply quote 1
          • jkandasaJ offline
            jkandasa
            global:last-edited-by,

            @seant

            var pingDistance = tankDistanceSensor.value; //Water tank level percent

            You have to use parseInt(tankDistanceSensor.value), Sensor variable always have String value.
            var pingDistance = parseInt(tankDistanceSensor.value); //Water tank level percent

            //THESE 2 LINES DO NOT SEEM TO SET THE VALUES
            tankLitresSensor.setValue(litres);
            tankPercentageSensor.setValue(percentage);

            After these two lines add the following lines,

              mcApi.sensor().sendPayload(tankLitresSensor); 
              mcApi.sensor().sendPayload(tankPercentageSensor); 
            
            one-reply-to-this-post last-reply-time reply quote 2
            • S offline
              seant
              global:last-edited-by,

              @jkandasa Thanks. Got it working.

              S one-reply-to-this-post last-reply-time reply quote 1
              • S offline
                seant @seant
                global:last-edited-by, seant

                @jkandasa This method no longer works when you set the gateway to use ACK. Is there not a way to set a value for a sensor variable only on the server, without transmitting through the gateway as well.

                jkandasaJ one-reply-to-this-post last-reply-time reply quote 0
                • jkandasaJ offline
                  jkandasa @seant
                  global:last-edited-by,

                  @seant unfortunately no. This looks useful request. can you please create a ticket in github issues?

                  one-reply-to-this-post last-reply-time reply quote 1
                  • D offline
                    Daniele
                    global:last-edited-by,

                    Maybe I missed something, but I have fake sensors defined on real nodes, and I update them thru scripts, and I have ack enabled.
                    Everything works fine!

                    The message goes thru the gateway, the node receives it and replies with ack, even if it doesn't anything with it.

                    Is your nose sleeping maybe?

                    one-reply-to-this-post last-reply-time reply quote 0
                    • first-post
                      last-post

                    0

                    online

                    644

                    users

                    532

                    topics

                    3.4k

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