<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to set sensor value with javascript]]></title><description><![CDATA[<p dir="auto">Hi</p>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
<p dir="auto">Please can you let me know how to save a value for a sensor using javascript.</p>
<p dir="auto">Thanks</p>
]]></description><link>http://forum.mycontroller.org/topic/146/how-to-set-sensor-value-with-javascript</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 13:45:44 GMT</lastBuildDate><atom:link href="http://forum.mycontroller.org/topic/146.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 23 Mar 2017 09:31:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to set sensor value with javascript on Sat, 16 Mar 2019 09:40:24 GMT]]></title><description><![CDATA[<p dir="auto">Maybe I missed something, but I have fake sensors defined on real nodes, and I update them thru scripts, and I have ack enabled.<br />
Everything works fine!</p>
<p dir="auto">The message goes thru the gateway, the node receives it and replies with ack, even if it doesn't anything with it.</p>
<p dir="auto">Is your nose sleeping maybe?</p>
]]></description><link>http://forum.mycontroller.org/post/2496</link><guid isPermaLink="true">http://forum.mycontroller.org/post/2496</guid><dc:creator><![CDATA[Daniele]]></dc:creator><pubDate>Sat, 16 Mar 2019 09:40:24 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Wed, 13 Mar 2019 01:40:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seant" aria-label="Profile: seant">@<bdi>seant</bdi></a> unfortunately no. This looks useful request. can you please create a ticket in <a href="https://github.com/mycontroller-org/mycontroller/issues" rel="nofollow ugc">github</a> issues?</p>
]]></description><link>http://forum.mycontroller.org/post/2490</link><guid isPermaLink="true">http://forum.mycontroller.org/post/2490</guid><dc:creator><![CDATA[jkandasa]]></dc:creator><pubDate>Wed, 13 Mar 2019 01:40:26 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Mon, 11 Mar 2019 18:42:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jkandasa" aria-label="Profile: jkandasa">@<bdi>jkandasa</bdi></a>  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.</p>
]]></description><link>http://forum.mycontroller.org/post/2487</link><guid isPermaLink="true">http://forum.mycontroller.org/post/2487</guid><dc:creator><![CDATA[seant]]></dc:creator><pubDate>Mon, 11 Mar 2019 18:42:26 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Thu, 23 Mar 2017 18:58:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jkandasa" aria-label="Profile: jkandasa">@<bdi>jkandasa</bdi></a> Thanks. Got it working.</p>
]]></description><link>http://forum.mycontroller.org/post/850</link><guid isPermaLink="true">http://forum.mycontroller.org/post/850</guid><dc:creator><![CDATA[seant]]></dc:creator><pubDate>Thu, 23 Mar 2017 18:58:04 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Thu, 23 Mar 2017 17:59:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seant" aria-label="Profile: seant">@<bdi>seant</bdi></a></p>
<blockquote>
<p dir="auto">var pingDistance =  tankDistanceSensor.value; //Water tank level percent</p>
</blockquote>
<p dir="auto">You have to use <code>parseInt(tankDistanceSensor.value)</code>, Sensor variable always have String value.<br />
<code>var pingDistance =  parseInt(tankDistanceSensor.value); //Water tank level percent</code></p>
<blockquote>
<p dir="auto">//THESE 2 LINES DO NOT SEEM TO SET THE VALUES<br />
tankLitresSensor.setValue(litres);<br />
tankPercentageSensor.setValue(percentage);</p>
</blockquote>
<p dir="auto">After these two lines add the following lines,</p>
<pre><code class="language-javascript">  mcApi.sensor().sendPayload(tankLitresSensor); 
  mcApi.sensor().sendPayload(tankPercentageSensor); 
</code></pre>
]]></description><link>http://forum.mycontroller.org/post/849</link><guid isPermaLink="true">http://forum.mycontroller.org/post/849</guid><dc:creator><![CDATA[jkandasa]]></dc:creator><pubDate>Thu, 23 Mar 2017 17:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Thu, 23 Mar 2017 17:52:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jkandasa" aria-label="Profile: jkandasa">@<bdi>jkandasa</bdi></a> Thanks I have already done those steps. The issue I have is trying to set a value for the virtual sensor via javascript.<br />
What I have done thus far is</p>
<ol>
<li>Create 2 virtual sensors</li>
<li>Create a "operation" script - using javascript</li>
<li>Create a rule that triggers the script when value changes</li>
</ol>
<p dir="auto">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?</p>
<pre><code class="language-javascript">// 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 &gt; 0 &amp;&amp; pingDistance &lt; (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);

}
</code></pre>
]]></description><link>http://forum.mycontroller.org/post/847</link><guid isPermaLink="true">http://forum.mycontroller.org/post/847</guid><dc:creator><![CDATA[seant]]></dc:creator><pubDate>Thu, 23 Mar 2017 17:52:18 GMT</pubDate></item><item><title><![CDATA[Reply to How to set sensor value with javascript on Thu, 23 Mar 2017 17:06:11 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/seant" aria-label="Profile: seant">@<bdi>seant</bdi></a> For now there is no event feature in MyController. You can create a virtual sensor and update value with javascript.</p>
<ul>
<li>Map your script with operation</li>
<li>Create a rule with real sensor and set threshold as in negative number(ex: <code>&gt; -1</code>). And uncheck ignore duplicate check box. Map your script operation with this rule.</li>
<li>Your script will be executed, when you receive value from your sensor.</li>
</ul>
]]></description><link>http://forum.mycontroller.org/post/845</link><guid isPermaLink="true">http://forum.mycontroller.org/post/845</guid><dc:creator><![CDATA[jkandasa]]></dc:creator><pubDate>Thu, 23 Mar 2017 17:06:11 GMT</pubDate></item></channel></rss>