Enable a flow help

Hello, new user here and pretty much 0 experience with javascript.
Weirdly i managed to install rpi os strecth, mqtt and nr to a pi zero w and flashed tasmota to one of my smart switches without any issue. Even did a few simple flows on it.
As a plc programmer nodered seemed a very easy approach to experiment with iot, until it wasn't.
I don't understand what is the problem here, could anyone help me out please?
I am trying to enable and disable on demand with a ui switch the automatic on/off at sunrise/set on the relay output (of the iot switch). Thanks in advance.

In this state, the flow variable is still true, why?
Is my application doable in this sturcture?
I tried to experiment with the boolean libraries also but no success (kind of ironic).
Can you point me to some documentation with the "topic" thing? I thought "topics" are mqtt specific things, but i see them as a parameter in many nodes.
Thanks in advance and have a nice day.

As the payload is 1 and you are comparing with double equal == then 1 is == to true.

This is a feature/quirk of JavaScript.

In non MQTT areas, it is very useful for 'tagging' a Payload. For example, if you send 2 payloads of 'true' down the same wire, which one is from your garage switch and which one came from a dashboard button? The topic provides context. It's not required that you use msg.topic (it could be msg.myTag) but it provides a familiar feeling of "description" to use msg.topic.

Hello,
thanks for both answers. The ui switch is turned off, shouldn't that reset the state of the flow variable to "false"?

Yes, but I cant see how have things configured from a screenshot.

Select the nodes of interest and export them ctrl+e

Be sure to always paste code snippets / flow exports between backticks
```
like this
```
otherwise it will not be importable


also, what value is your flow variable (look in the side-bar under "context data")

[{"id":"f3ab7248.bc71d","type":"ui_switch","z":"ae6391de.2859e","name":"","label":"Switch","tooltip":"","group":"6c7c8e8a.63dc8","order":3,"width":"0","height":"0","passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":130,"y":220,"wires":[["d0c88e92.b9309"]]},{"id":"d0c88e92.b9309","type":"Tasmota Switch","z":"ae6391de.2859e","broker":"a36e2f8a.1c6a6","device":"Meteo","name":"Meteo","outputs":1,"uidisabler":false,"fullTopic":"","cmndPrefix":"","statPrefix":"","telePrefix":"","x":830,"y":220,"wires":[[]]},{"id":"f6ca4949.1b5c08","type":"sunrise","z":"ae6391de.2859e","name":"","lat":"46.7212","lon":"25.5855","start":"sunrise","end":"sunsetStart","soff":0,"eoff":0,"x":210,"y":700,"wires":[["78f9ccca.80c4f4","afef7f25.85ea8"],[]]},{"id":"d7b26b1.6c62098","type":"ui_switch","z":"ae6391de.2859e","name":"","label":"Enable Sunrise","tooltip":"","group":"6c7c8e8a.63dc8","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"payload","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":100,"y":360,"wires":[["ca6a4694.9aa9f8","6bd72a92.80d0b4","c0d8e45a.b9f3e8"]]},{"id":"ba405204.4c60a","type":"debug","z":"ae6391de.2859e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":420,"wires":[]},{"id":"fc51004c.ba5fd","type":"debug","z":"ae6391de.2859e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":670,"y":700,"wires":[]},{"id":"78f9ccca.80c4f4","type":"switch","z":"ae6391de.2859e","name":"","property":"Enable","propertyType":"flow","rules":[{"t":"eq","v":"payload","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":700,"wires":[["fc51004c.ba5fd","d0c88e92.b9309"]]},{"id":"ca6a4694.9aa9f8","type":"change","z":"ae6391de.2859e","name":"","rules":[{"t":"set","p":"Enable","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":420,"wires":[["ba405204.4c60a"]]},{"id":"afef7f25.85ea8","type":"debug","z":"ae6391de.2859e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":670,"y":640,"wires":[]},{"id":"6bd72a92.80d0b4","type":"debug","z":"ae6391de.2859e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":360,"wires":[]},{"id":"17b2cb83.3725b4","type":"debug","z":"ae6391de.2859e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":480,"wires":[]},{"id":"10079b84.5312b4","type":"change","z":"ae6391de.2859e","name":"","rules":[{"t":"set","p":"Enable","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":480,"wires":[["17b2cb83.3725b4"]]},{"id":"c0d8e45a.b9f3e8","type":"Invert","z":"ae6391de.2859e","name":"Invert","x":290,"y":480,"wires":[["10079b84.5312b4"]]},{"id":"6c7c8e8a.63dc8","type":"ui_group","name":"Meteo","tab":"ee8fb9fa.4adfe8","order":1,"disp":true,"width":"7","collapse":false},{"id":"a36e2f8a.1c6a6","type":"mqtt-broker","name":"","broker":"192.168.101.3","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"ee8fb9fa.4adfe8","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

the flow variable (flow.Enable) follows the state of the ui switch (Enable Sunrise) true/false.

now (on the screenshot), the lowest msg.payload debug node value should be 0 if i'd know this thing better.

Here is a simplified version (I use cron-plus for schedules/solar events - not whatever sunrise node you use - but that should not matter)

image

The premise is...

  1. The UI Switch should send true or false (the topic should not be msg.payload)
    image

  2. Set flow.Enable to the value of the UI switch
    image

  3. test the flow.Enable flag - if true the message will pass
    image

[{"id":"d7b26b1.6c62098","type":"ui_switch","z":"83a23089.dc697","name":"","label":"Enable Sunrise","tooltip":"","group":"6c7c8e8a.63dc8","order":4,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"SunriseEnable","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":440,"y":800,"wires":[["ca6a4694.9aa9f8"]]},{"id":"ba405204.4c60a","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":800,"wires":[]},{"id":"fc51004c.ba5fd","type":"debug","z":"83a23089.dc697","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":880,"wires":[]},{"id":"78f9ccca.80c4f4","type":"switch","z":"83a23089.dc697","name":"","property":"Enable","propertyType":"flow","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":650,"y":880,"wires":[["fc51004c.ba5fd"]]},{"id":"ca6a4694.9aa9f8","type":"change","z":"83a23089.dc697","name":"","rules":[{"t":"set","p":"Enable","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":800,"wires":[["ba405204.4c60a"]]},{"id":"674324d.1f71cdc","type":"cronplus","z":"83a23089.dc697","name":"","outputField":"payload","timeZone":"","persistDynamic":false,"commandResponseMsgOutput":"output1","outputs":1,"options":[{"name":"sunrise","topic":"sunrise","payloadType":"default","payload":"","expressionType":"solar","expression":"0 * * * * * *","location":"","offset":"0","solarType":"selected","solarEvents":"sunrise"}],"x":440,"y":880,"wires":[["78f9ccca.80c4f4"]]},{"id":"6c7c8e8a.63dc8","type":"ui_group","name":"Meteo","tab":"ee8fb9fa.4adfe8","order":1,"disp":true,"width":"7","collapse":false},{"id":"ee8fb9fa.4adfe8","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Thank you kindly for the answer, I see you showed the topic related part too, i get the concept now.
Can you please check if you turn off the Enable Sunrise ui switch while you get a true (or however it works) from the cron-plus, does the debug node connected to the switch will change to false?
Because mine doesn't and i had this same issue with the original flow.
Thank you.

the debug node will show the last payload (in my demo case, it shows the default payload cron-plus sends - but that payload could be whatever you want)

It works as I suspect you want it to (block messages when enable == false)

I6U5ffSIRB

Here, i modified the payload of cron-plus to show you the payload can be whatever you want it to be.

UUnXKX2sR5

I connected the switch node output to a relay (hardware) output on a board, you guys probably call it gpio.
While the sunrise event gives a "true" state and i turn off the Enable Sunrise ui switch i would like to deactivate the relay.
Is it possible in node red?

Yes. Link the "enable" ui switch to the gpio node.
NOTE: You will need to change the true false to 1 and 0 before you send it.

a function node can do that easily...

if(msg.payload == true) {
    msg.payload = 1;
} else {
    msg.payload = 0;
}
return msg;

ui_switch --> function --> gpio

And where do i connect the sunrise/set node?

I'm getting lost.

What is your goal - can you spell it out in some form of pseudo logic.

e.g...

  1. if flow.enable is true, allow sunrise message to turn ON and OFF GPIO 1
  2. if flow.enable is false, prevent sunrise message from affecting GPIO 1
  3. when flow.enable is changed from true to false, switch OFF GPIO

etc.

ok, I added the if condition you gave, at the output of the switch node and then connected the output of the function node to the hardware output and it's working as i wanted originally. (i marked the post with the screenshot as the solution)
many thanks, looks like this won't roll without learning some js.

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