How not to complicate automation for lights?

In general terms, I had a switch and a light.
With the switch, I would like to control this light.
To make it not too simple, this light can be turned on by other ways and when it is turned on, I would like to set the switch to the appropriate state.
And this is where the automation starts to get complicated, because it either starts to loop, or every time you have to check whether the state is already set or not.

What is the best way to approach this?

alias: Włącznik Salon
description: ""
trigger:
  - platform: device
    type: changed_states
    device_id: a72ccdeec00700acbf1ccd34d271daf6
    entity_id: d62f8856751f080ecf8b897205ffdfac
    domain: switch
    id: SalonW
  - platform: state
    entity_id:
      - light.salon
    id: SalonS
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - SalonW
        sequence:
          - if:
              - condition: template
                value_template: "{{ trigger.to_state.state == 'on' }}"
            then:
              - service: light.turn_on
                metadata: {}
                data:
                  transition: 3
                  kelvin: 2500
                  brightness_pct: 50
                target:
                  entity_id:
                    - light.salon
            else:
              - service: light.turn_off
                metadata: {}
                data:
                  transition: 3
                target:
                  entity_id:
                    - light.salon
      - conditions:
          - condition: trigger
            id:
              - SalonS
        sequence:
          - if:
              - condition: template
                value_template: "{{ trigger.to_state.state == 'on' }}"
            then:
              - type: turn_on
                device_id: a72ccdeec00700acbf1ccd34d271daf6
                entity_id: d62f8856751f080ecf8b897205ffdfac
                domain: switch
            else:
              - type: turn_off
                device_id: a72ccdeec00700acbf1ccd34d271daf6
                entity_id: d62f8856751f080ecf8b897205ffdfac
                domain: switch
mode: single

Conceptually simplest: two automations, and ditch those horrible device triggers and actions. I haven’t included the light transitions here, but you can easily add that back in to the first automation once you’re sure it’s working.

alias: Switch to light
trigger:
  - platform: state
    entity_id: switch.SWITCH_ID
    to:
      - 'on'
      - 'off'
condition:
  - "{{ trigger.to_state.state != states('light.salon') }}"
action:
  - service: light.turn_{{ trigger.to_state.state }}
    entity_id: light.salon

alias: Light to switch
trigger:
  - platform: state
    entity_id: light.salon
    to:
      - 'on'
      - 'off'
condition:
  - "{{ trigger.to_state.state != states('switch.SWITCH_ID') }}"
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.SWITCH_ID

When you operate the switch:

  • both entities are off, switch gets turned on
  • first automation is triggered
  • condition passes as switch is on and light is off
  • light gets turned on
  • second automation triggered
  • condition fails as both entities are on.

When you then operate the light:

  • both entities are on, light gets turned off
  • second automation triggered
  • condition passes (light off, switch on)
  • switch gets turned off
  • first automation triggers
  • condition fails as both entities are off.

Unfortunately, splitting into 2 will not be a good solution for me (I have 3 switches, each with 3 buttons. That would make 18 automations together :slight_smile: ) But thanks for the tip with the conditions. This will solve the looping problem.

1 Like

Glad I could help.

Why is that a problem? You have a complex system there, so the solution is either going to be complicated or lengthy. I’d go for simple and repetitive every time.

2 Likes

Maybe not a problem, but for purely aesthetic reasons, I assumed that for each room there is one automation for lighting and one for heating, rather than dozens of separate ones (which really deal with one task).

Maybe if the automations could be grouped, it would be more convenient for me?

p.s. filtering by area does not work at all, filtering by device does not see devices used in templates

A long-term feature request.

If you give each automation an id, you can assign them to an area in the UI; and you can group them conceptually by using a prefix on the name. Example:

- alias: Aquarium - pump restart
  id: f142aac8-a937-4796-b5fb-a268d9b73974
  trigger:
    - platform: state
[...]

image

I can see the “issue” with a bunch of “simpler” automations, but in the end that’s as you say an aesthetic problem, in the file-struckture, which btw noone sees :slightly_smiling_face:
i also try to keep my automations as simple as possible , for ones, it’s more easy to troubleshoot, secondly it’s easier to add / make changes in a simple, rather than in a complex.

i.e Lets say you want to add another task/action when it’s the switch which is the trigger, rather than the light ( when turn on the light by the switch, do something else also, i.e send a notification, or trigger a template-sensor( light turned on by switch ), whatever ) or you want to add a motion-sensor to turn on the light (for 1 minute) as lead-light (i.e in and out of the room) etc.

there are already several of these


image