Switch status - how to know

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

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

What exactly are you trying to achieve?

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

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

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

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?

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?

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

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.

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

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 for an example of how to join messages into one object.

Ok, thank you ...

I found how to read the status ...

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

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