If-set-else-set node

Hello there,
sorry for such easy question, probably I missed something, but I can't find a node for following thing.

I have system (Loxone) which feeds by flow by some values, for example "0" and "1" as a state of the button. And I need to convert these 0/1 to "on" / "off" string values and pass it as MQTT value to Zigbee2Mqtt system.

So, I can create "function" block and code this simple function in JS. Something like

if input=0 then output="off"
else if input="1" then output="on"
else ....

But because this will be used on several places, I would like to use some pre-build node. Is there something for this?

Because I can use "switch" block but then I have to also use some two other blocks (probably change") to modify the payload for the correct value.

But I would like to use only one element to do this thing. It's very inconvenient to create three nodes for every transformation. I would like to see something like this:

Where "TRANSFORM" node is a combination of switch and change node.

Is there any way how to do that?

Thanks

I think what you need is a subflow.

Select the nodes to be "reused" then "selection to subflow"...

image

Then you can have multiple instances of the subflow.

This might be of some help...

1 Like

Thanks for the reply. I'm aware of the subflows, but I believed that such a basic feature has to be already implemented as a node.

It not, I will use subflows exactly as you mention.

Yes the change node.

[{"id":"309e2100.627d4","type":"inject","z":"1838876b.1af279","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":120,"wires":[["bd001232.d968e"]]},{"id":"a1a6e224.201248","type":"inject","z":"1838876b.1af279","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":160,"wires":[["bd001232.d968e"]]},{"id":"bd001232.d968e","type":"change","z":"1838876b.1af279","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"1","fromt":"num","to":"on","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"0","fromt":"num","to":"off","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["554c64d4.9fa4ac"]]},{"id":"554c64d4.9fa4ac","type":"debug","z":"1838876b.1af279","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":550,"y":120,"wires":[]}]
1 Like

Im not certain what you really want. I suspected a subflow was what you wanted.

Alternatively, there is a node that operates like a subroutine - it is called node-red-contrib-components - perhaps that might be preferable?

1 Like

Thank you! This is exactly what I looking for.

I didn't know I can use change node in this way. Thank you.

This is also interesting for more complex tasks. Thanks!

Take a look at JSONata expressions to, It is very powerfull.

1 Like

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