Function true / false

Hello all

I am a newbie in the field of Node Red. Unfortunately I'm not a programming specialist but it's never too late to start :wink:

Maybe someone can give me some development help with a function (see below).

The output true and false (msgTag) is only active if msgExtAlarm is true, otherwise there should be no output (see table).

Thanks for your help.
Reto

flows (1).json (2.9 KB)

image

image

You need to get both values into one message/payload object before you can do your logic.

See this article in the cookbook for an example of how to join messages into one object.

The code in your function node will be executed whenever a message arrives from either of your switches.
So either msg.topic is set to "msgExtAlarm" and msg.payload is true or false
Or msg.topic is set to "msgTag" and msg.payload is true or false
But not both.
The function only knows about the data in the current message. It has forgotten what was in previous messages.

You either need to use a Join node to combine the two inputs into a single message or else store the value in context variables.

This is your function:

var ExtAlarm = { payload: msg.msgExtAlarm};
var Tag = { payload: msg.msgTag};
if (ExtAlarm == 'true' && Tag == 'true')
{msg.payload = true;}
else 
{msg.payload = false;}
return msg;

The first line says "Create a variable which is a Javascript object with the key "payload" and the value msg.msgExtAlarm."

But as far as I can see, msg.msgExtAlarm does not exist. "msgExtAlarm" is in msg.topic for one of the two inputs.

The switch nodes send boolean true or false but when you try and test them you are comparing with the string 'true' or 'false'. (maybe it will work).
The accepted way is to use if (something === true) {}.

You probably need to read some introductory material such as "working with messages" in the documentation.

You do not need to use a function node or any code, Node-red is after all a low code platform

You will how ever need to store the values in context storage, or join as Steve and jbudd suggest

Here are a couple of simple example that uses no code at all, hope it helps

Context

[{"id":"f971e58e.b036b","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":2600,"wires":[["98177fb51c59bb54"]]},{"id":"98177fb51c59bb54","type":"ui_switch","z":"c791cbc0.84f648","name":"","label":"msgExtAlarm","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"msgExtAlarm","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"x":170,"y":2680,"wires":[["e6bdb98b.f4711"]]},{"id":"e6bdb98b.f4711","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"msgExtAlarm","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":2680,"wires":[[]]},{"id":"3328b9e8.3c8fe6","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":90,"y":2640,"wires":[["98177fb51c59bb54"]]},{"id":"f5d3db124553d447","type":"ui_switch","z":"c791cbc0.84f648","name":"","label":"msgTag","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"msgTag","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"x":190,"y":2740,"wires":[["fafe87e5.98e018"]]},{"id":"46917346.746304","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":2780,"wires":[["f5d3db124553d447"]]},{"id":"2d94cb4b.126ff4","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":93.33332824707031,"y":2822.666748046875,"wires":[["f5d3db124553d447"]]},{"id":"fafe87e5.98e018","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"msgTag","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":2740,"wires":[["510108db.7e421"]]},{"id":"510108db.7e421","type":"switch","z":"c791cbc0.84f648","name":"","property":"msgExtAlarm","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":2740,"wires":[["4e9152f9b0cb337a"]]},{"id":"4e9152f9b0cb337a","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":2660,"wires":[]},{"id":"a131b547.834c88","type":"ui_group","name":"Status","tab":"c3fb2694fa75ff26","order":1,"disp":true,"width":"6","collapse":true},{"id":"c3fb2694fa75ff26","type":"ui_tab","name":"Test-Dashboard","icon":"dashboard","order":14,"disabled":false,"hidden":false}]

Join

[{"id":"12cf4b83.c6835c","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":2680,"wires":[["cf3e2872.601528"]]},{"id":"cf3e2872.601528","type":"ui_switch","z":"c791cbc0.84f648","name":"","label":"msgExtAlarm","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"msgExtAlarm","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"x":190,"y":2760,"wires":[["ae95641.a031618"]]},{"id":"8bbdd832.46d2d","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":90,"y":2720,"wires":[["cf3e2872.601528"]]},{"id":"ae95641.a031618","type":"join","z":"c791cbc0.84f648","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":350,"y":2800,"wires":[["9edf3b0c.c295e8"]]},{"id":"52979040.be1c38","type":"ui_switch","z":"c791cbc0.84f648","name":"","label":"msgTag","tooltip":"","group":"a131b547.834c88","order":5,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"msgTag","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":true,"x":210,"y":2820,"wires":[["ae95641.a031618"]]},{"id":"9edf3b0c.c295e8","type":"switch","z":"c791cbc0.84f648","name":"","property":"payload.msgExtAlarm","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":480,"y":2800,"wires":[["9e5496c3.b49808"]]},{"id":"f1ef94d9.c4a42","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":90,"y":2860,"wires":[["52979040.be1c38"]]},{"id":"7bd77ff9.be3b48","type":"inject","z":"c791cbc0.84f648","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":93.33332824707031,"y":2902.666748046875,"wires":[["52979040.be1c38"]]},{"id":"9e5496c3.b49808","type":"switch","z":"c791cbc0.84f648","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"msgTag","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":620,"y":2800,"wires":[["effa027d.d6edc"]]},{"id":"effa027d.d6edc","type":"change","z":"c791cbc0.84f648","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.msgTag","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":2800,"wires":[["2f0fefc9.1bd018"]]},{"id":"2f0fefc9.1bd018","type":"debug","z":"c791cbc0.84f648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":810,"y":2740,"wires":[]},{"id":"a131b547.834c88","type":"ui_group","name":"Status","tab":"c3fb2694fa75ff26","order":1,"disp":true,"width":"6","collapse":true},{"id":"c3fb2694fa75ff26","type":"ui_tab","name":"Test-Dashboard","icon":"dashboard","order":14,"disabled":false,"hidden":false}]

Hello, thanks all, I continue testing :wink:

No, never mind, I see it!

Do I need to cache the data? Couldn't I also query all possibilities with if -> then in one function.
Aren't functions always written in Java script?

The solution (E1cid) without function is for me also an option....

I hope I find time today to test further.

It's well worth watching the official node-red introduction videos

I also like these videos from notenoughtech

Hi
Thanks for the links of the videos. I have checked the solution of "E1cid" and it seems to work. I am testing the solution live today.

Thanks both for the help. Have a nice day.
Reto

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