Enable and disable individual UI nodes based on external state

Not sure if this more like a general question or Dashboard specific, and it's probably also really stupid, but here goes anyway: I am doing my first Node-RED project, and I would like to be able to enable and disable invidiual UI nodes (for instance Text and Gauges based on MQTT broadcasts), depending on whether the device that is linked to them is on or off.

I achieved what I want in principle by passing to the UI nodes msg.enabled true/false, however where I am getting really stumped is: I want the UI nodes to show the information from the MQTT broadcasts, but in order to enable and disable them, I have to link something like a switch node that sends the msg.enabled status - but this also overrides the msg.payload from the main MQTT input.

What would be the "right" Node-RED way to implement something like this? I.e. UI elements that show information from a node, but also get their enabled/disabled status from another node, without overriding the main payload.

If you insert a Change node into the flow between the MQTT node and the ui node, and configure the Change to set msg.enabled then that will not affect msg.payload.

Thanks, and yes, that part I got - my problem was then how do I trigger the Change node from outside the MQTT->Change->UI-Node chain, without inserting a new msg.payload in the chain.

The only solution I found so far is to store the state of the device in a global or flow variable, and do a Switch + Change node combination that checks the value of the flow variable and then sets the msg.enabled as required, but this seems somewhat counterintuitive and also has the distinct disadvantage that I can't actually trigger it when the value changes, have to rely on the MQTT node to update...

I think I misunderstood the problem. You want to enable or disable the ui node without changing what it is already displaying. Is that right?

If so then this should do it

[{"id":"56435afa.3ae99c","type":"ui_text","z":"bdd7be38.d3b55","group":"a8e4a3.d31edb6","order":4,"width":0,"height":0,"name":"","label":"ENABLED?","format":"{{msg.payload}}","layout":"row-spread","x":470,"y":2320,"wires":[]},{"id":"441e1771.e9ba9","type":"inject","z":"bdd7be38.d3b55","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"value","payload":"","payloadType":"date","x":140,"y":2220,"wires":[["4baf59f6.2a492"]]},{"id":"ebba6d6d.4c3ee8","type":"inject","z":"bdd7be38.d3b55","name":"Enable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"state","payload":"true","payloadType":"bool","x":130,"y":2300,"wires":[["4baf59f6.2a492"]]},{"id":"f262a093.2db73","type":"inject","z":"bdd7be38.d3b55","name":"Disable","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"state","payload":"false","payloadType":"bool","x":130,"y":2340,"wires":[["4baf59f6.2a492"]]},{"id":"4baf59f6.2a492","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":2260,"wires":[["a31f5ff.a4caa2"]]},{"id":"a31f5ff.a4caa2","type":"function","z":"bdd7be38.d3b55","name":"Combine state and value","func":"if (typeof msg.payload.state != \"undefined\") {\n    msg.enabled = msg.payload.state\n}\nmsg.payload = msg.payload.value\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":2260,"wires":[["56435afa.3ae99c"]]},{"id":"a8e4a3.d31edb6","type":"ui_group","name":"Chart","tab":"8032fe72.980b98","order":3,"disp":false,"width":"6","collapse":false},{"id":"8032fe72.980b98","type":"ui_tab","name":"PnH","icon":"dashboard","order":1,"disabled":false,"hidden":false}]
1 Like

Yes, that would be the ideal situation. I could also live with inserting a null value when disabling the UI node, but it would be really nice if I could at least trigger this change when the actual device goes online or offline, instead of only when that particular MQTT node gets an update.

But you brought me to another idea - I could obviously also publish to the MQTT topic, when the device state changes.

The flow I just posted does that.

Yeah, sorry, I was replying to the previous message - thanks, your solution is elegant, seems to do exactly what I wanted.

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