@jkandasa Thanks. Got it working.
S
Offline
Posts
-
RE: How to set sensor value with javascript
-
RE: How to set sensor value with javascript
@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- Create 2 virtual sensors
- Create a "operation" script - using javascript
- 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); }