Fetching the state of a switch?

I have a function node that uses the state of a switch to control it's behavior. It stores the state of the switch in its context.
When I make certain changes, the node context gets lost and it doesn't match the state of the switch.

I think I might be able to manually force the switch to a known state via some code in the "on start" tab but anything that tries to send messages from that bit of code seems like a terrible hack and I don't like the idea of resetting the switch unexpectedly.

I plan to copy and paste this node to a few different places in my flow, and I want to be able to do so without making changes to it. Because of that, I don't want to use contexts outside of the node (if at all possible). It seems like it should be possible to send a message to the get its current state but I don't see anything in the help for the node. Is it possible to do this somehow?

You can try to set the Context to use the file system instead of memory, by editing your settings.js file.
More information on how to set that up in the Node-red documentation here

What sort of changes? If you mean that you want the context value to be remembered across re-deploy and restart then you could use persistent context. Have a look at the node red docs Working with Context to see how to do that.

What in really want to do is to make sure my internal context matches the state of the dashboard switch node. AFAIK the only way to get the state of the node is to change its state.
I’m looking for a way I can ask the dashboard switch node what it’s state is.

The technique to use here is to remember the state every time it changes, then you don't need to ask for it.

1 Like

The problem with that is that it still allows the state to get out of sync.
But it turns out that if " Pass through msg if payload matches new state:" is set, it will send an output message when it gets any input message and if the payload is null, I get the state of the switch back.

So I just set the "on start" section to send a message with a dummy topic which gets things set up initially.

I did create a dedicated output to do this so it complicates the function node slightly but I still have something I can just copy and paste without having to make any modifications to the code in the function node.

The concept of the dashboard switch and the importance of it's state has meaning only if the switch on the dashboard is just a switch. Meaning that somewhere is something the switch is switching. And that state is much more important. So the dashboard switch must be in sync with some physical stuff. But you haven't had any word of such. And about the the communication with it if exists.

It is common conceptual mistake to prioritize the states visually seen on dashboard over the states of objects in real world. And then the pass through comes in and makes things even more complicated an mostly broken.

The other mistake is often seen that the state is held/stored and then trusted from multiple places. Tehn the syncing comes more complex and most of times unreliable.

I'd recommend to take step back and describe the real situation with all the related elements and then may be there will be good advise how to find solution.


@hotNipi has got it exactly right. It is the state of the real world object being switched that is important, make sure that your internal representation of that is correct and then use that to set the state of the dashboard switch.

I found my solution. I guess the concept that the switch is a source of truth because it is not a reflection of a physical device is an uncommon idea. I'm toggling behavior in a function node.

I assume you mean "the switch is NOT a source of truth." The need for feedback is almost universal in control systems, and unfortunately, many home automation devices and a surprising number of industrial devices do not provide any built in and may make it difficult to retrofit.

I don't understand why this is so difficult to understand

[{"id":"460af321d51228ba","type":"ui_switch","z":"49a507155de1841b","name":"enable","label":"enable node","tooltip":"","group":"4cf693d802e2de2b","order":7,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"enable_node","topicType":"str","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":430,"y":200,"wires":[["90b195b334cac894"]],"inputLabels":["From_func"],"outputLabels":["From_UI"]},{"id":"90b195b334cac894","type":"function","z":"49a507155de1841b","name":"order data for logging","func":"let EnablePassthrough = context.get('EnablePassthrough');\nif (msg.topic == 'enable_node') {\n    node.warn (`setting switch state ${msg.payload}`);\n    context.set('EnablePassthrough', msg.payload);\n    return [null,null];\n}\n\nif (!EnablePassthrough) {\n    //logging is disabled, drop the message\n    node.warn (`dropping message`);\n    return [null,null];\n}\n\nreturn [null,msg];","outputs":2,"noerr":0,"initialize":"setTimeout(function(){\n    var msg = {topic:'QueryState'};\n    node.send([msg,null]);\n}, 150);\n\nreturn null;","finalize":"","libs":[],"x":460,"y":260,"wires":[["460af321d51228ba"],["fae036f555d8224d"]]},{"id":"943b658d17b366b0","type":"inject","z":"49a507155de1841b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":260,"wires":[["90b195b334cac894"]]},{"id":"fae036f555d8224d","type":"debug","z":"49a507155de1841b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":690,"y":260,"wires":[]},{"id":"4cf693d802e2de2b","type":"ui_group","name":"test","tab":"472d8fc.6c1387","order":2,"disp":true,"width":"6","collapse":false},{"id":"472d8fc.6c1387","type":"ui_tab","name":"sample","icon":"dashboard","disabled":false,"hidden":false}]

Apparently, most of us assumed that you were dealing with a difficult problem: how to keep the state of a dashboard switch synchronized with a real-world switch. If in fact all you want is to keep the state of an internal variable synchronized with a dashboard switch, there are many solutions, including yours, @Colin's (using persistent context), and others using retained mqtt messages.

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