Should I check the state of entity before turning off?

Hi.
I want one button to turn off all lights, some plugs and A/C. So I have an automation witch turns off group.lights… and plugs. But I am not sure with A/C.
Should script firstly check the state of a/c and if it is on - turn it off; and if it is off - do nothing?
Is it “dirty” just to turn off everything anyways? Or Home Assistant checks it for me and will not send that command?
If I should firstly check the state and then turn it off, how can I do that in “action”?

I feel it depends on how many devices and, possibly, what technology is involved.

For example, if you want to turn off ten devices that communicate via a wireless mesh protocol (like zwave and zigbee), Home Assistant will send ten commands that, depending on the complexity of the mesh network, take a finite amount of time to propagate through the system.

During this short but busy time, when ten commands and acknowledgements are coursing their way over the network, you might experience a brief delay in responsiveness if, at that very moment, you wanted to control a light. Those other ten commands are ‘in process’ and your command will have to wait its turn.

Obviously if all ten lights were already off, then sending ten commands to turn them off was wasted effort and only served to inconvenience you at that moment in time. However, you can see that, in practical terms, the probability of this activity affecting you (what are the odds of you wishing to control a light during this exact moment in time) is low. Basically, it’s something to keep in the back of your mind in the event you notice sluggishness in response to your command and then you remember it’s busy processing ten ‘turn off’ commands.

Long story short, I wouldn’t bother unless I noticed performance problems. However, if you want maximum efficiency at all times then, yes, check what’s already off and only turn off what’s currently on. To do that, you’ll have to use a template, in an automation or Template Switch, that determines what’s on and only sends the turn_off command to those devices.

To get you started, put this into the Template Editor and experiment with it. It calculates the number of lights that are on.

{{ states.light | selectattr('state', 'eq', 'on') | list | count }}

PS
If only one device is involved … I would just turn it off, regardless if it is already off, and avoid the additional complication involved with creating an automation or template switch or whatever.

@Argo, @123 I think this advice would only apply to entities attached to zwave, wifi etc devices if my understanding is correct. If the “entity” is just something like an input_boolean within HA that doesn’t control a physical device it shouldn’t make any difference at all. For example, you can turn off every single automation at once and it won’t make a difference if some were already off or not.

There are times when checking the state of an entity is required, such as the pause command for a media player which will throw a warning in the logs if the media player wasn’t on (active session).

I agree.

Like I said, I feel it depends on quantity of devices and the technology involved. Turning off 30 zwave/zigbee/wi-fi devices, that are already off, is a wasted effort that might (that’s operative word here, might) only serve to delay other zwave/zigbee/wi-fi commands at the same moment in time.

Alternately, there the scenario you described where the device might respond differently if it receives the same command twice. For example, some devices may only be able to toggle their state upon receiving a command therefore there’s no way to command it to a specific state (on/off, play/pause) just to toggle its state. You definitely wouldn’t want to indiscriminately send it a toggle command to turn off if it’s already off.

Long story short, the answer depends on the situation. :slight_smile:

Thank you for answers. I got the idea :slight_smile:
Tested my few zigbee and z-wave devices and I am ok with tiny delay. Thank you again for help