Use Switch to show current state

Hello everyone! I'm new to NodeRED and I'm having trouble to use the switch node to show the current state of my variables.

Here is the deal, I'm trying to automate the lights of a warehouse controlled by a PLC with a HMI. If someone turn on/off the light on the PLC's HMI, I want my switch node to change to the on or off position accordingly.
I'm able to turn on/off the lights using nodeRED just fine, so this part is ok.

I used a input node to get the events from my Python MQTT publisher then I have my switch and then an output node sending the commands to my Python Subscriber.
I have a topic to the events, where I suppose the events of the HMI will be sent to NodeRED and I have another topic to commands, where NodeRED can send the commands to turn on/off the lights and whatosever.

Oh! I'm using nodeRED hosted at IBM Cloud.

I appreciate if someone could shed some light!

Thanks a lot!

Is there a topic you can subscribe to to get the current state of the light? If so then pick that up and if necessary use a Switch (not ui-switch) and Change nodes to set the payload to exactly the same values that the ui-switch is configured to output and feed that into the ui-switch. Make sure message pass through is not enabled in the ui node or you will get an mqtt loop.

Hello Colin! Thanks for answering me!
This makes perfectly sense! Yes, I have a topic to get the current state, it's called 'switchevt'. My Input node is getting this string {"lamp1":"1","lamp2":"0","comp1":"0"} , saying that my lamp1 is on.
So I've used the Switch node (the yellow one) and set the property to msg.payload and then set 3 outputs:
contains: lamp1 -> 1
contains: lamp2 ->2
contaisn: comp1 ->3

Each output of the Switch node is connected to a Change node, but could you help me to create the rule?
The output of my UI-Switch nodes are:
Switch 1 On payload {"lamp1":"1"}
Switch 1 On payload {"lamp1":"0"}

Switch 2 On payload {"lamp1":"1"}
Switch 2 On payload {"lamp2":"0"}

Switch 3 On payload {"comp1":"1"}
Switch 3 On payload {"comp1":"0"}

If you set the output of the mqtt node to Parsed Json then it should output a javascript object rather than a string. Do that first and feed it into a debug node and check that you see a javascript object

{
  lamp1: 0,
  lamp2: 1,
  comp1: 3
}

Are the ui-switch outputs strings or objects?

[Edit] Can you also confirm whether in the debug node from the mqtt node the values are numbers or strings after setting the output to parsed json (so lamp1: 0 or lamp1: "0")

The Format field of my IBM's MQTT input node is json, so should I change it to parsed json? after I did this it stopped to display msgs in the debug node.

This is the message with the output IBM MQTT input node format set to 'json'

17/08/2020 13:20:14node: da3f0196.49508iot-2/type/alexnote/id/001/evt/switchevt/fmt/json : msg.payload : Object

{ lamp1: "1", lamp2: "0", comp1: "0" }

Sorry, I am missing something. In the first post you said the output of the MQTT node was a string, but now you show it as an object. You want whatever setting shows it as an object in the debug node.

Can you explain what you mean by IBM's MQTT node? Are you not using the standard node-red mqtt nodes?

Sorry, I may have mixed some concepts. I assumed that if it's between quotes it's a string. I can see now that is a wrong assumption.

Since my nodeRED is hosted at IBM's, I'm using their own MQTT input and output nodes.

I have to dash away for a bit but something like this should work. When I said you wanted a switch and change nodes I assumed each lamp came through in its own message. Since they are combined together then the structure needs to be slightly different. The inject node simulates your mqtt input and the debug nodes are the ui-switch nodes. Check that the debug nodes give you what you want before connecting to the switches. Don't forget to make sure pass through is cleared or will lock up node red in an infinite loop.

[{"id":"a2f90798.023d6","type":"inject","z":"bdd7be38.d3b55","name":"MQTT In Simulator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"lamp1\": \"1\", \"lamp2\": \"0\"}","payloadType":"json","x":220,"y":2480,"wires":[["d26a8d85.a1d52","db4e8c44.c1ed5"]]},{"id":"5fc6ea56.c3521c","type":"debug","z":"bdd7be38.d3b55","name":"Lamp1 ui-switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":460,"y":2600,"wires":[]},{"id":"d26a8d85.a1d52","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"lamp1\": payload.lamp1}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":2480,"wires":[["e7ddc17c.5518c8"]]},{"id":"e7ddc17c.5518c8","type":"json","z":"bdd7be38.d3b55","name":"","property":"payload","action":"","pretty":false,"x":450,"y":2540,"wires":[["5fc6ea56.c3521c"]]},{"id":"db4e8c44.c1ed5","type":"change","z":"bdd7be38.d3b55","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\"lamp2\": payload.lamp2}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":2660,"wires":[["25516e04.2af6ba"]]},{"id":"25516e04.2af6ba","type":"json","z":"bdd7be38.d3b55","name":"","property":"payload","action":"","pretty":false,"x":370,"y":2720,"wires":[["a6a80a6d.36a28"]]},{"id":"a6a80a6d.36a28","type":"debug","z":"bdd7be38.d3b55","name":"Lamp2 ui-switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":380,"y":2780,"wires":[]}]
1 Like

Very nice flow! Almost worked! When I click the switch, it goes to the on position and the light turns on, but after some seconds the switch goes to the off position, even though the light still on.
Also it still not changing the state to on/off when I turn on/off with the HMI. :pensive:

What is coming from the mqtt that sets the switch off?

I don't understand what you mean by that.
I forgot to say that the switch node should be set to show the state of the input, does that help?

Sorry, let me try explain this a little better...I got a LCD screen with the light controls, there I can turn on and off the lights. This LCD screen is connected to a controller, called PLC, my Python Script reads the memory of that PLC and then publishes the data in json format to the topic 'switchevt'.
Like this: {'lamp1':lamp1,'lamp2':lamp2,'comp1':comp1}
Being the lamp1, lamp2 and comp1 (without quotes) the values 1 or 0 corresponding to on or off.

The IBM MQTT Input node was connected to the UI-Switches and after that connected to a IBM MQTT Output node. Every time that I flip the switch node on NodeRED it sends a json {"lamp1":1}, like this, to the topic 'switchcmd'

What do you see from the mqtt node when you turn on/off with the HMI? Screenshot please.

W10.00 is the lamp1 memory and W10.01 is lamp2 and W10.02 correspond to comp1

Show us the config of the switch node please.

@alexmacabu, out of interest, did you know there is now an OMRON fins contrib node?

(Disclaimer, I wrote this)

I was gonna ask earlier when I seen the OMRON robot in your picture if you were using OMRON PLC.

Just curious as to why you have delegated the plc Comms to python.

I think is best if I show you guys the video...
Since I have 3 switches, I'm doing a tryal and error approach, so I set 3 different configurations, one for each switch.

Sorry! I uploaded the video with my background music playing! :sweat:

Just paste one of them please. In don't want to mess about with videos. Have you checked you have configured it as I suggested?

Hi Steve! I didn't knew that! Great! Very interesting and I sure will use it on later projects!
The thing is, I developed a Python script to communicate with PLC's using OPC UA Protocol and then publish it on the NodeRED hosted on IBM's Cloud using MQTT, just for the kicks of it.

But here at my work place we got only Omron PLCs so I had to adapt my code to communicate using FINS instead of OPC UA. Either way I would have to use Python because my NodeRED server is running on a Cloud infraestructure.

But in next projects I will sure use a local NodeRED server, so this FINS connection node will come in handy! Thanks a lot for the tip!

Sorry! Here is the screenshots, I tried this 3 different configurations, but none of them work as I expected.