Please help fix my trigger id automation

Hi everybody,

below is my automation. I assumed it would do the following: if switch.kueche_wandschalter_center would be turned on, it would turn switch.abzug on, and if the switch would be turned off, it would turn off the switch.

However, this automation only works if switch.kueche_wandschalter_center and switch.abzug are currently off. If so, it will turn on the abzug switch when the wandschalter one has been turned on. However, nothing will happen when turning it off.

automation:
  - alias: "KĂĽche Abzug Wandschalter"
    trigger:
      - platform: state
        id: "an"
        entity_id: switch.kueche_wandschalter_center
        to: "on"
      - platform: state
        id: "aus"
        entity_id: switch.kueche_wandschalter_center
        to: "off"
    action:
      - condition: trigger
        id: "an"
      - service: switch.turn_on
        entity_id: switch.abzug
      - condition: trigger
        id: "aus"
      - service: switch.turn_off
        entity_id: switch.abzug

Thank you in advance for your input :slight_smile:

automation actions are executed sequentially. in your case it checks that the “to: on” trigger was the actual trigger that started the automation.

if true it performs the turn on action. if false then the automation stops right there. It never even gets to the second condition evaluation.

it’s the way automations have always worked.

to get around that situation you need to use the “choose:” options and split the trigger conditions between them.

1 Like

Thank you @finity

In case anybody else cares, this seems to be the solution (haven’t been able to test it yet due to not being physically there to actually push the button, but it doesn’t throw any more errors this way)

automation:
  - alias: "KĂĽche Abzug Wandschalter"
    trigger:
      - platform: state
        entity_id: switch.kueche_wandschalter_center
    action:
      - choose:
        - alias: "Abzug An"
          conditions:
            - condition: state
              entity_id: switch.kueche_wandschalter_center
              state: "on"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.abzug
      - choose:
        - alias: "Abzug Aus"
          conditions:
            condition: state
            entity_id: switch.kueche_wandschalter_center
            state: "off"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.abzug

Someone will come along shortly to point this out anyway I have no doubt, but this can be radically simplified by having a single trigger for the switch, with nothing entered for the state.

The service call to mirror the state of the switch can then be templated:

automation:
  - alias: "Kuche Abzug Wandschalter"
    trigger:
      - platform: state
        entity_id: switch.kueche_wandschalter_center
    action:
      - sequence:
          - service: switch_turn{{ 'on' if is_state('switch.kueche_wandschalter_center','on') else 'off' }}
            target:
              entity_id: switch.abzug

The automation can be reduced to this:

  - alias: "Kuche Abzug Wandschalter"
    trigger:
      - platform: state
        entity_id: switch.kueche_wandschalter_center
    action:
      - service: 'switch.turn_{{ trigger.to_state.state }}'
        target:
          entity_id: switch.abzug
2 Likes

and for completeness you didn’t implement the “choose:” option correctly as well.

you only need one choose and both conditions follow under it.

    action:
      - choose:
        - alias: "Abzug An"
          conditions:
            - condition: state
              entity_id: switch.kueche_wandschalter_center
              state: "on"
          sequence:
            - service: switch.turn_on
              target:
                entity_id: switch.abzug
        - alias: "Abzug Aus"
          conditions:
            condition: state
            entity_id: switch.kueche_wandschalter_center
            state: "off"
          sequence:
            - service: switch.turn_off
              target:
                entity_id: switch.abzug

but using the other suggestions is better anyway.

Yes! I didn’t know what exactly to search for in order to find this part ( - service: 'switch.turn_{{ trigger.to_state.state }}'), but it was my initial thought as well - just didn’t know how to “phrase” it. This is perfect.

Someone really needs to start a simple blog with all these “shortcuts” on them, because the official documentation, doesn’t really make it obvious just what can actually be templated, WITH examples.

I have a Growing notepad of snippets of code from @123 just over the last 3 weeks!

1 Like

That is a great suggestion! Do you have your snippets up somewhere? I have some automations that I don’t feel are the most elegant and could likely be improved and shortened with shortcuts like these.

They are just in Google Keep right now, so I could easily find them again as I keep my laptop in the bedroom now, and use a Chromebook when I am downstairs during the day.

Long ago, the service option didn’t support templates and you had to use a different option called service_template. It was deprecated after service was enhanced to support templates.

You could say that this example uses "service with a template" or simply a “templated service call” (although the word “templated” is technical jargon).

2 Likes

Most of my automations were built long ago as well. I didn’t have any experience with trigger ids or choose until today. But Home Assistant (or the developers) keep amazing me with things like this fix you provided. That combined with trigger.to(...) makes it possible to keep things short and make them easily reusable.

1 Like