Don't switch a switch unless required

Good day, a lot of my automations include: trigger: something. condition: switch=off, action: switch:=on

But this gets very verbose if I want three switches because I need three identical automations because of the switch check condition.

Would it be possible for home-assistant to not switch a switch if the switch already has that state? So basically a check on quite a low level.

This would automations with lots of switches (or even switching a group) much more elegant.

PS. I have to do the checking, because for example zwave switches (but I can imagine a lot of bus-system switches) take a bit of time and bandwidth to do the switch, so switching 20 items very often is undesired.
PS2. This item is ofcourse valid for all states, not just on/off
PS3. This only makes sense for switches with a reported state, not for an assumed state

You could do this with templates, and pass only the switches that are off to the switch.turn_on service.

How would such a template look like? For example: turn off all lights in group living_room. With living room being 10 items.

That’s not something I’m an expert at, I’d suggest asking on Discord where some of the gurus of templates lurk.

It’s probably something like this:

action:
  - service: switch.turn_on
    data_template:
      entity_id: >- 
        {% for entity_id in states.group.my_switches.attributes.entity_id -%}
          {%- if states(entity_id) == 'off' %}
          {%- if loop.first %} {% else %}, {% endif -%}{{ entity_id }}{% endif -%}
        {%- endfor %}

I did ask on discord, I had a discussion there.

But as you already can see from your answer, everything gets very verbose. And different solutions for a group (you loop over the group) or just a bunch of switches.

Basic outcome on discord: everything can be scripted if you try (and type) long enough. But I did not get a counter argument why this should not be implemented.

Basically to keep the files clean and really reduce implementation time for new automations. Like you said: your not an expert at templating, neither am I. So everything just takes a long time now to implement.

Perhaps this is something better suited to AppDaemon?

Do I get you right that you want something like: switch.turn_on_if_off

Would be nice, though.

do you receive errors if switching on when already on?

I ask because in several scripts and scenes I use, i set the specific needs for that scene, especially lights and switches turning on and off respectively.

Selecting another scene, obviously with other switch and light settings might have identical settings for several entities. A same switch or light must be turned on or off.

That never raises an error, and seems to go fine. switching a switch that is already on simply does nothing, turning on a light already on is default behavior when changing attributes settings for that light, so isnt an issue either?

fwiw, my switches are Zwave’s also, but on a dedicated Hub I read over mqtt and set using command_line api calls.

@nickrout I am exactly trying to make things less complicated. Just a simple: if door open, lights on. (for example). Without the need for if light is not on conditions.

@eXtatic No, I don’t want that, what is wrong with the current switch.turn_on? It just make sense if it does that check ifself

@Mariusthvdb No everything works fine! It just takes long if you have a lot of switches you want to switch. Because every command is still send over the zwave network. This is what I am trying to prevent.