# Value wrong interpreted

**URL:** <https://discourse.nodered.org/t/value-wrong-interpreted/48345>\
**Category:** General\
**Created:** [13 July 2021 04:27 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345 "2021-07-13T04:27:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [13 July 2021 04:27 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/1 "2021-07-13T04:27:31Z")

</div>

Hi,  
I use a simple function:

```auto
if (msg.payload < 300) {
  msg.payload = 3
} else {
  // nothing
}

if (msg.payload > 500) {
  msg.payload = 0
}

```

I have monitored the value of the msg.payload:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/b/5b30fe2833808d44d7cd44e75c4590060ab2a640.png)  
The higher graph is the value after processing the function, the lower is the input value of this function. As you can see, at the times where a value of 3 leaves the function, the input value was never below 300.

What did I have made wrong?

Thanks, Frank

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 July 2021 05:05 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/2 "2021-07-13T05:05:04Z")

</div>

Feed the input and output into debug nodes, name then so it is easy to see which is which, sand see what it shows. Make sure the input is a number and not a string.

Show us what they show for one of the incorrect outputs.

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [13 July 2021 06:21 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/3 "2021-07-13T06:21:22Z")

</div>

Instead of output debug node I've logged it directly into Influx DB:  
Input:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d70861171941b546fa23503764336109b54b0c7a.png)

Output:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/574d12025f42a2abb38f15837fd533947ce2aba6.png)

The input value is read from MQTT Broker.  
Here is the flow:

```auto
[{"id":"82665303.2b40c8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ae889e1b.e9893","type":"link in","z":"82665303.2b40c8","name":"","links":["596fc398.e850a4"],"x":123.16667175292969,"y":205.16665649414062,"wires":[["6c6b2ffc.d8e938","5de6d312.822674"]]},{"id":"6c6b2ffc.d8e938","type":"function","z":"82665303.2b40c8","name":"if raining => park","func":"if (msg.payload < 300) {\n msg.payload = 3\n} else {\n // msg.payload = 0;\n}\n\nif (msg.payload > 500) {\n msg.payload = 0\n}\n\nmsg.topic=\"husq-automower/0/mower/action\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":266.99998474121094,"y":140.42581176757812,"wires":[["8f94eea1.b592b"]]},{"id":"8f94eea1.b592b","type":"trigger","z":"82665303.2b40c8","name":"","op1":"3","op2":"0","op1type":"num","op2type":"str","duration":"0","extend":false,"overrideDelay":false,"units":"ms","reset":"0","bytopic":"all","topic":"topic","outputs":1,"x":494.23333740234375,"y":140.05908203125,"wires":[["24087f75.358ee"]]},{"id":"24087f75.358ee","type":"influxdb out","z":"82665303.2b40c8","influxdb":"bdce0459.b3c93","name":"Automower lastStatus","measurement":"AMG_NodeCommand","precision":"","retentionPolicy":"","x":730.1666870117188,"y":139.59249877929688,"wires":[]},{"id":"5de6d312.822674","type":"influxdb out","z":"82665303.2b40c8","influxdb":"bdce0459.b3c93","name":"","measurement":"RegenSensor","precision":"","retentionPolicy":"","x":352.16668701171875,"y":240.16665649414062,"wires":[]},{"id":"bdce0459.b3c93","type":"influxdb","hostname":"192.168.130.171","port":"8086","protocol":"http","database":"UVR-Home","name":"","usetls":false,"tls":""}]

```

Have now add in front of the function a change node which converts string to number to be safe.

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [13 July 2021 06:56 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/4 "2021-07-13T06:56:02Z")

</div>

> [@digidax](#):
>
> Instead of output debug node I've logged

Thanks, but logging in Influx is not back to basic.  
To get the data in influx you are doing things with the data that will influence it.  
We need the raw data coming from the mqtt node in the debug window.

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [13 July 2021 07:16 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/5 "2021-07-13T07:16:30Z")

</div>

Okay, understand. Will do so and report, if the problem comes up again. It was sporadically within the last weeks.

Thanks.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 July 2021 15:37 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/6 "2021-07-13T15:37:13Z")

</div>

Your trigger node is set to send 3, then when payload is over 500 it sends 0 to the trigger node, which resets it. When reset if it then receives over 500 again, it sends 0 and resets.

Whats the trigger node for?

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [14 July 2021 03:49 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/7 "2021-07-14T03:49:14Z")

</div>

The Flow does the following:  
An analog rain sensor gives a value between around 280 to 980. The value is updated every 10 seconds to a MQTT broker. Node Red has subscribed this topic.

When the value is below 300, it's the indication that it is raining. If it so, the value 3 is send to a mower which told him, to stop mowing and park for an unlimited time. It's important that only one time is sent the 3 as a command. If the input value is over 500, it's indicating for me, that it's dry enough and when it's raining again (the input is below 300) the park command 3 will send again. It's a kind of hysteresis combined with with a one time signal command.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 July 2021 07:35 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/8 "2021-07-14T07:35:42Z")

</div>

What does it send when over 500? If it is 3 again, then try this

```auto
[{"id":"8dfe9f6c.5345a8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"200","payloadType":"num","x":110,"y":1880,"wires":[["74ad1327.c11174"]]},{"id":"74ad1327.c11174","type":"function","z":"c74669a0.6a34f8","name":"if raining => park","func":"if(msg.payload < 300){\n msg.payload = 0\n}else if(msg.payload > 500){\n msg.payload = 1\n}else{\n return null;\n}\nmsg.topic=\"husq-automower/0/mower/action\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1940,"wires":[["ae75dc8c.80b3c8"]]},{"id":"d80b219d.55aa38","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"400","payloadType":"num","x":120,"y":1940,"wires":[["74ad1327.c11174"]]},{"id":"622a5cfc.acb8ec","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"600","payloadType":"num","x":120,"y":2000,"wires":[["74ad1327.c11174"]]},{"id":"ae75dc8c.80b3c8","type":"rbe","z":"c74669a0.6a34f8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":520,"y":1940,"wires":[["9aec6a2f.2ce4a8"]]},{"id":"9aec6a2f.2ce4a8","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":1940,"wires":[["9478755d.244bf8"]]},{"id":"9478755d.244bf8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":2020,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [14 July 2021 08:00 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/9 "2021-07-14T08:00:10Z")

</div>

@digidax did you realise that if the value is between 300 and 500 that your function passes on the original message with the payload unchanged?

If you want it to send nothing in that case then you need something like

```auto
msg.topic="husq-automower/0/mower/action"
if (msg.payload < 300) {
  msg.payload = 3
} else if (msg.payload > 500) {
  msg.payload = 0
} else {
  msg = null
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [14 July 2021 08:18 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/10 "2021-07-14T08:18:41Z")

</div>

[@E1cid](https://discourse.nodered.org/u/E1cid)  
If it's over 500, the function will send 0 and the trigger node take it as a reset and will not produce an output (I think so)

Have tested your flow. Thanks, but: I inject 200 (raining) the correct output of 3 is the result.  
But when I inject 600, Output 3 will be produced again. That's not correct.

using a s7-\* PLC progam, we call it: detect on falling edge: So when your function node will switch the output from 1 =\> 0

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [14 July 2021 08:21 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/11 "2021-07-14T08:21:20Z")

</div>

Then remove the change node and set the values 3 and 0 in the function  
e.g.

```auto
[{"id":"74ad1327.c11174","type":"function","z":"c74669a0.6a34f8","name":"if raining => park","func":"if(msg.payload < 300){\n msg.payload = 3\n}else if(msg.payload > 500){\n msg.payload = 0\n}else{\n return null;\n}\nmsg.topic=\"husq-automower/0/mower/action\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":1940,"wires":[["ae75dc8c.80b3c8"]]},{"id":"8dfe9f6c.5345a8","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"200","payloadType":"num","x":110,"y":1880,"wires":[["74ad1327.c11174"]]},{"id":"d80b219d.55aa38","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"400","payloadType":"num","x":120,"y":1940,"wires":[["74ad1327.c11174"]]},{"id":"622a5cfc.acb8ec","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"600","payloadType":"num","x":120,"y":2000,"wires":[["74ad1327.c11174"]]},{"id":"ae75dc8c.80b3c8","type":"rbe","z":"c74669a0.6a34f8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":520,"y":1940,"wires":[["9478755d.244bf8"]]},{"id":"9478755d.244bf8","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":2020,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [14 July 2021 08:21 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/12 "2021-07-14T08:21:34Z")

</div>

Yes, thats not a problem. The following trigger node waits only for the 3 as a value and forward it to the output and 0 for the reset command. The input value will never be below 250, so there are not problems with wrong interpreted commands by the mower.

---

<div class="post-metadata">

**Author:** ![digidax](https://avatars.discourse-cdn.com/v4/letter/d/edb3f5/32.png) [@digidax](https://discourse.nodered.org/u/digidax)\
**Post date:** [14 July 2021 08:27 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/13 "2021-07-14T08:27:46Z")

</div>

Same problem:  
Injecting 200 =\> Payload is 3  
Injecting 600 =\> Payload is 0

So my solution is now based on your great idea with rbe:

```auto
[{"id":"a20ce12b.85f888","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"74ad1327.c11174","type":"function","z":"a20ce12b.85f888","name":"if raining => park","func":"if(msg.payload < 300){\n msg.payload = 3\n}else if(msg.payload > 500){\n msg.payload = 0\n}else{\n return null;\n}\nmsg.topic=\"husq-automower/0/mower/action\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":373.16668701171875,"y":172.16665649414062,"wires":[["ae75dc8c.80b3c8"]]},{"id":"8dfe9f6c.5345a8","type":"inject","z":"a20ce12b.85f888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"200","payloadType":"num","x":163.16668701171875,"y":112.16665649414062,"wires":[["74ad1327.c11174"]]},{"id":"d80b219d.55aa38","type":"inject","z":"a20ce12b.85f888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"400","payloadType":"num","x":173.16668701171875,"y":172.16665649414062,"wires":[["74ad1327.c11174"]]},{"id":"622a5cfc.acb8ec","type":"inject","z":"a20ce12b.85f888","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"600","payloadType":"num","x":173.16668701171875,"y":232.16665649414062,"wires":[["74ad1327.c11174"]]},{"id":"ae75dc8c.80b3c8","type":"rbe","z":"a20ce12b.85f888","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":573.1666870117188,"y":172.16665649414062,"wires":[["786bc850.6662a"]]},{"id":"9478755d.244bf8","type":"debug","z":"a20ce12b.85f888","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":577.1666870117188,"y":311.1666564941406,"wires":[]},{"id":"786bc850.6662a","type":"function","z":"a20ce12b.85f888","name":"if raining => park","func":"if(msg.payload > 0){\n msg.payload = msg.payload\n}else{\n return null;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":563.1666870117188,"y":239.16665649414062,"wires":[["9478755d.244bf8"]]}]

```

Will test it and thanks for the helpful inspiration,  
Frank

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 September 2021 08:28 UTC](https://discourse.nodered.org/t/value-wrong-interpreted/48345/14 "2021-09-12T08:28:03Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
