# Switch status - how to know

**URL:** <https://discourse.nodered.org/t/switch-status-how-to-know/58300>\
**Category:** General\
**Created:** [14 February 2022 09:31 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300 "2022-02-14T09:31:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 09:31 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/1 "2022-02-14T09:31:17Z")

</div>

Hi,  
to command my switches I use zigbee2mqtt and since I don’t know if I do well, ask advice ....

For each switch I have this function ....  
 ![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/1/e17a7573e668c089eaeba827ff776e347e30feb4.png)

```auto
if (msg.payload.state == "ON")
{
    global.set("var_swlucesalotto", 1)
    node.status({fill: "green", text: "ON"})
    msg.labelcolor = "#0CEFDC"
}
else
{
    global.set("var_swlucesalotto", 0)
    node.status({fill: "grey", text: "OFF"})
    msg.labelcolor = "#A4A4A4"
}
return msg;

```

with which I save the status into a variable .....

When I must turn-on / turn-off the switch I read the variable before;

I'm right or Is useless?

and can I avoid the variable and to know the switch's status by mqtt?

What I try to do is to decrease the number of variables in my flow and decrease the operations of reading and writing ....

Thank you for help .....

---

<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:** [14 February 2022 09:52 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/2 "2022-02-14T09:52:13Z")

</div>

What exactly are you trying to achieve?

> [@Giamma](#):
>
> When I must turn-on / turn-off the switch I read the variable befo

How is the user telling the system to turn it on/off?

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 09:57 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/3 "2022-02-14T09:57:57Z")

</div>

For example when I left the house and press the button AWAY,  
I read every variables and if the value is "ON" I send the command turn-off ...

```auto
msg.payload = {"state": "OFF"};
msg.topic = "zigbee2mqtt/LuceGarage/set";
return msg;

```

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [14 February 2022 10:04 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/4 "2022-02-14T10:04:17Z")

</div>

> [@Giamma](#):
>
> press the button AWAY,

This is the first mention of a button, as you talk about switches in your first post...

Is there a button that changes its state according to its previous state?

---

<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:** [14 February 2022 10:08 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/5 "2022-02-14T10:08:32Z")

</div>

Rather than having a button, would it be better to have a switch which always shows the current state and you click it to change from on to off?

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 10:21 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/6 "2022-02-14T10:21:17Z")

</div>

I try to explain better ....  
For each light in my house I have physical smart switch Vimar .....  
Another example, in my kitchen I have 2 light, 1 tv, 1 coffee machine and 1 tablet ....  
At lunchtime, on the tablet dashboard I press the button "LUNCH TIME" and with a function I turn-on tv, coffee machine and before to turn-on the two light I read the variables; if the variables are "OFF" I send the command "ON".

Well, I wanted to know if there is a way to do without the variables ......

---

<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:** [14 February 2022 10:23 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/7 "2022-02-14T10:23:26Z")

</div>

So if the light is off you turn it on. What do you do if the light is already on? If you do nothing then just send the On command anyway, it should not matter if the light is already on.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 10:32 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/8 "2022-02-14T10:32:57Z")

</div>

> [@Colin](#):
>
> it should not matter if the light is already on

I think so too and I wanted to know your experience and there is a way to know the switch's status by a mqtt request .....

---

<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:** [14 February 2022 10:36 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/9 "2022-02-14T10:36:23Z")

</div>

I use Join nodes for tackling problems like this, rather than flow/global context. You can feed the current state into a Join node, and also the On/OFF request and then in the output of the Join node you have the current state and the request. See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 10:44 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/10 "2022-02-14T10:44:54Z")

</div>

Ok, thank you ...

---

<div class="post-metadata">

**Author:** ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)\
**Post date:** [14 February 2022 13:01 UTC](https://discourse.nodered.org/t/switch-status-how-to-know/58300/11 "2022-02-14T13:01:45Z")

</div>

I found how to read the status ...

```auto
msg.payload = {"state": ""};
msg.topic = "zigbee2mqtt/LuceGarage/get";
return msg;

```

I will see how to integrate it in my flow ....
