How to change state of digital output based on payload

Hi guys,

this is going to come across as a real newbie question, but try as I might I have not been able to find an answer this online. This has got to be simple, surely.

I have a message payload value from a 1 wire temperature sensor. I am connected, and that is working just fine, I can see the temperature coming through as payload.value. However no matter what I do, I cannot figure out how to implement a simple "if the temperature is >25 degrees then turn on relay"

How on Earth do you do this with Node-Red without resorting to code in a Function block?

Note I have my Relay output working (I can manually control it using an inject node) but it is simply not intuitive in terms of how I connect my message payload to the relay.

1 Like

did u test the switch node?

[{"id":"76803807.7359f8","type":"switch","z":"5e5b0b32.5446e4","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"gte","v":"25","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":620,"wires":[[],[]],"inputLabels":["payload.value"],"outputLabels":["if >= 25","if <25"]}]

example:

[{"id":"7eb473b.f7c178c","type":"debug","z":"239412b3.3d4e9e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":590,"y":720,"wires":[]},{"id":"a05a109d.d941e","type":"change","z":"239412b3.3d4e9e","name":"true","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":740,"wires":[["5f0598a0.f19058"]]},{"id":"211b3c65.be2504","type":"inject","z":"239412b3.3d4e9e","name":"","topic":"","payload":"33","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":760,"wires":[["317abc59.55b7e4"]]},{"id":"5f0598a0.f19058","type":"debug","z":"239412b3.3d4e9e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":760,"wires":[]},{"id":"97bda989.492478","type":"switch","z":"239412b3.3d4e9e","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"gte","v":"25","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":760,"wires":[["a05a109d.d941e"],["60179268.5e494c"]],"inputLabels":["payload.value"],"outputLabels":["if >= 25","if <25"]},{"id":"60179268.5e494c","type":"change","z":"239412b3.3d4e9e","name":"false","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":780,"wires":[["5f0598a0.f19058"]]},{"id":"317abc59.55b7e4","type":"change","z":"239412b3.3d4e9e","name":"Move payload to payload.value","rules":[{"t":"move","p":"payload","pt":"msg","to":"payload.value","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":760,"wires":[["97bda989.492478","7eb473b.f7c178c"]]}]
1 Like

Hi,
thanks for getting back to me.

Yes I had tried the switch, and while it seems to be the right tool for the job initially, to me it's more of a demultiplexer in that it ports the message payload through to the output based on set condition, whereas I need to output a true if payload.value > 25 else false (I'm ignoring hysteresis for now).

I see in your second example that you then change the payload value from whatever temperature it is, to "true" but that still doesn't switch the digital output, which I strongly suspect isn't expecting a payload or message at all, just a simple true/false input. I have attached my code (including your mods) so you can see what I mean. I would have thought there would be a function block that simply outputs true for a given payload value/condition else false. A conditional expression block if you want to call it that.

[{"id":"e5970583.fa1da","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"72830e3e.1efb68","type":"unipi-input","z":"e5970583.fa1da","name":"","alias":"","devices":"temp","circuits":"288EA19E0A000087","property":"","seedev":"0","seecirc":"0","seeorig":"0","x":770,"y":340,"wires":[["d6930bf4.e963a8"]]},{"id":"9831ad8.ed811d","type":"websocket in","z":"e5970583.fa1da","name":"","server":"","client":"5bbc639a.67e854","x":500,"y":340,"wires":[["72830e3e.1efb68"]]},{"id":"75fc0754.7d9ec","type":"debug","z":"e5970583.fa1da","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1510,"y":280,"wires":[]},{"id":"d2507c52.4456b","type":"websocket out","z":"e5970583.fa1da","name":"","server":"","client":"6455e978.240c88","x":1860,"y":380,"wires":[]},{"id":"89fcd9a.b5108a8","type":"unipi-output","z":"e5970583.fa1da","name":"Relay 2","cmd":"set","alias":"","devices":"relay","circuits":"2","enableFil":"1","inputFil":"","relayFil":"","digoutFil":"","analoutFil":"","analinFil":"","ledFil":"","x":1520,"y":380,"wires":[["d2507c52.4456b"]]},{"id":"d6930bf4.e963a8","type":"switch","z":"e5970583.fa1da","name":"","property":"payload.value","propertyType":"msg","rules":[{"t":"gte","v":"25","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":990,"y":340,"wires":[["1bcc27f5.616848"],["601bdf54.2039d"]],"inputLabels":["payload.value"],"outputLabels":["if >= 25","if <25"]},{"id":"1bcc27f5.616848","type":"change","z":"e5970583.fa1da","name":"true","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"true","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":340,"wires":[["89fcd9a.b5108a8","75fc0754.7d9ec"]]},{"id":"601bdf54.2039d","type":"change","z":"e5970583.fa1da","name":"false","rules":[{"t":"set","p":"payload.value","pt":"msg","to":"false","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1230,"y":400,"wires":[["89fcd9a.b5108a8"]]},{"id":"5bbc639a.67e854","type":"websocket-client","path":"ws://127.0.0.1/ws","wholemsg":"false"},{"id":"6455e978.240c88","type":"websocket-client","z":"","path":"ws://127.0.0.1:8080/ws","tls":"","wholemsg":"false"}]

What values are you injecting to manually change the output state?

If you will eventually want to apply hysteresis then I suggest you look at node-red-contrib-ramp-thermostat which will provide the comparison with hysteresis for you. You can ignore the ramp component of the node.

Alternatively if you are trying to achieve temperature control then have a look at node-red-contrib-pid which will allow you to perform pid control which can be a lot better than thermostatic control.

However, if you just want to generate true or false (or 1 or 0 or whatever) based on a simple comparison then you can do it in a change node like this.
image

Finally, I suspect the problem with your flow is that you are setting the payload to the strings "true" and "false" rather than boolean true and false. In the Change nodes click the az dropdown and change them to boolean.

Thanks Colin!
I literally just found that myself seconds ago, and got it working. So exact same flow as you saw before, but now with the msg.payload being changed/converted to boolean true/false.

My question is, as far as doing this is concerned, is this the recommended method? IE take payload, feed it through a switch (where it outputs the same message based on a condition, in my case if payload.value > 25), then change payload to output boolean True and finally feed that into my digital output? If this is the standard means of setting up an overall conditional expression output (true/false) based on a particular payload value, fair enough, but I'm just concerned that I have gone about this in a roundabout manner.

At this point I don't need PID control (though I definitely would like to look into that in future, so thanks for the links). A simple thermostat style control is good enough for demonstration purposes for my students in terms of how to read data from a 1 wire device.

Thanks again for your assistance

OK, I have successfully implemented a 1-wire temperature sensor flow that utilises a DS18B20 temperature sensor and turns an output relay on a UniPi 1.1 on and off. The UniPi1.1 hardware is a Raspberry Pi based platform suitable for use with Node-Red. I am using the particular "flavour" of Node-Red as compiled by UniPi Technologies to suit their particular hardware. Refer to their documentation/website for more information.

The flow incorporates hysteresis control as well and can be set for any desired turn on and turn off temperature thresholds.

I read the message payload (payload.value) and change or filter out just the temperature, converting it to a floating point number before feeding said value into the hysteresis control.

Posting here as this may be of use to somebody else

[{"id":"7ee0b26b.7047fc","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"945492de.6ba248","type":"unipi-input","z":"7ee0b26b.7047fc","name":"","alias":"","devices":"temp","circuits":"288EA19E0A000087","property":"","seedev":"0","seecirc":"0","seeorig":"0","x":430,"y":280,"wires":[["eddc35f9.5bad5"]]},{"id":"eba23251.3f3088","type":"websocket in","z":"7ee0b26b.7047fc","name":"","server":"","client":"5bbc639a.67e854","x":200,"y":280,"wires":[["945492de.6ba248"]]},{"id":"c746613a.595e88","type":"debug","z":"7ee0b26b.7047fc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":990,"y":180,"wires":[]},{"id":"22bfcd0f.4af21a","type":"websocket out","z":"7ee0b26b.7047fc","name":"","server":"","client":"6455e978.240c88","x":940,"y":440,"wires":[]},{"id":"f84f5ef3.6d13d","type":"unipi-output","z":"7ee0b26b.7047fc","name":"Relay 2","cmd":"set","alias":"","devices":"relay","circuits":"2","enableFil":"1","inputFil":"","relayFil":"","digoutFil":"","analoutFil":"","analinFil":"","ledFil":"","x":700,"y":440,"wires":[["22bfcd0f.4af21a"]]},{"id":"eddc35f9.5bad5","type":"change","z":"7ee0b26b.7047fc","name":"Filter out temp","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload.value)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":280,"wires":[["c746613a.595e88","846810ec.6d8c88"]]},{"id":"846810ec.6d8c88","type":"hysteresis","z":"7ee0b26b.7047fc","name":"","ThresholdType":"fixed","ThresholdRising":"25","ThresholdFalling":"23","TopicThreshold":"","TopicCurrent":"","ThresholdDeltaRising":"","ThresholdDeltaFalling":"","InitialMessage":false,"OutRisingType":"bool","OutRisingValue":"true","OutFallingType":"bool","OutFallingValue":"false","OutTopicType":"top","OutTopicValue":"","x":520,"y":440,"wires":[["f84f5ef3.6d13d"]]},{"id":"5bbc639a.67e854","type":"websocket-client","path":"ws://127.0.0.1/ws","wholemsg":"false"},{"id":"6455e978.240c88","type":"websocket-client","z":"","path":"ws://127.0.0.1:8080/ws","tls":"","wholemsg":"false"}]

Here is a picture of the final flow, where the hysteresis threshold settings are clearly visible as being set to 23°C lower / 25°C Upper thresholds, and sensed temperature = 25.95°C

A final note is that you need to edit the UniPi Input function block and change the Unique serial number to suit your particular 1-wire sensor. In Node-Red this is pretty simple. In your browser navigate to your Node-Red web page, but WITHOUT the (:1880) port number (so only the IP address). this takes you to the web page that shows the status of all the digital and analogue inputs and outputs etc. Scroll down to the bottom (as per image below) and you should see that the sensor has been detected, along with its ID.

In my case I copied this value and pasted (in UniPi Input) circuits: 288EA19E0A000087

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