# Use Switch to show current state

**URL:** <https://discourse.nodered.org/t/use-switch-to-show-current-state/31665>\
**Category:** Dashboard\
**Created:** [17 August 2020 14:31 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665 "2020-08-17T14:31:30Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 14:31 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/1 "2020-08-17T14:31:30Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 14:47 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/2 "2020-08-17T14:47:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 15:56 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/3 "2020-08-17T15:56:41Z")

</div>

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"}

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 16:07 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/4 "2020-08-17T16:07:31Z")

</div>

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

```auto
{
  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"`)

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 16:21 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/5 "2020-08-17T16:21:29Z")

</div>

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:14[node: da3f0196.49508](https://node-red-setsystem.mybluemix.net/red/#)iot-2/type/alexnote/id/001/evt/switchevt/fmt/json : msg.payload : Object

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 16:27 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/6 "2020-08-17T16:27:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 16:33 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/7 "2020-08-17T16:33:23Z")

</div>

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.

 ![ibm_mqttinput](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c1d6d9ca6845be76927f0c44adac8d25a9a9ab1.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 16:42 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/8 "2020-08-17T16:42:40Z")

</div>

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.

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 17:07 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/9 "2020-08-17T17:07:55Z")

</div>

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. 😔

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 17:47 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/10 "2020-08-17T17:47:44Z")

</div>

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

> [@alexmacabu](#):
>
> Also it still not changing the state to on/off when I turn on/off with the HMI.

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?

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 18:20 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/11 "2020-08-17T18:20:32Z")

</div>

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'

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 19:34 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/12 "2020-08-17T19:34:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 19:39 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/13 "2020-08-17T19:39:17Z")

</div>

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

 ![SS_nodered1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/d/6dff6e253d72a9529cc9eab757ba8a1e92f1b175.jpeg)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 19:46 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/14 "2020-08-17T19:46:16Z")

</div>

Show us the config of the switch node please.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 August 2020 19:54 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/15 "2020-08-17T19:54:24Z")

</div>

@alexmacabu, out of interest, did you know there is now an [OMRON fins contrib node](https://flows.nodered.org/node/node-red-contrib-omron-fins)?

(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.

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 19:57 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/16 "2020-08-17T19:57:43Z")

</div>

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.

[![]( " - YouTube") ](https://www.youtube.com/watch?v=YPE4L0srbyE)

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 19:59 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/17 "2020-08-17T19:59:02Z")

</div>

Sorry! I uploaded the video with my background music playing! 😓

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 August 2020 20:04 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/18 "2020-08-17T20:04:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 20:06 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/19 "2020-08-17T20:06:59Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![alexmacabu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/alexmacabu/32/27328_2.png) [@alexmacabu](https://discourse.nodered.org/u/alexmacabu)\
**Post date:** [17 August 2020 20:11 UTC](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665/20 "2020-08-17T20:11:48Z")

</div>

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

 ![SS_nodered2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f792450f358443c8c574d62c553358f6f14f4c5d.jpeg) ![SS_nodered3](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/f/0fe9812aabedf4605713dff67fe8376799137b97.jpeg)

[Next page](https://discourse.nodered.org/t/use-switch-to-show-current-state/31665.md?page=2)
