Value wrong interpreted

Hi,
I use a simple function:

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:


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

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.

Instead of output debug node I've logged it directly into Influx DB:
Input:
grafik

Output:
grafik

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

[{"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.

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.

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

Thanks.

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?

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.

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

[{"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":[]}]

@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

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;

@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

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

[{"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":[]}]

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.

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

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

[{"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

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