Automation with multiple triggers not working

Yeah, it’s probably fine. You just can’t use the automation editor for that automation anymore because it doesn’t support nested conditions. Or is a nested condition. And is only supported because the default non-nested conditions mean ‘and’.

1 Like

Here’s another approach:

  • It uses a single Template Trigger.
  • The condition checks if the sun is below_horizon.
  • The condition also checks if the light is currently off (to avoid needlessly turning it on).
- id: '1562767530621'
  alias: 'Living room - TV source dim Lights'
  trigger:
    platform: template
    value_template: >
      {% set src = state_attr('media_player.living_room_tv', 'source') %}
      {{ src in ['Netflix','PS4','Plex'] }}
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
    - condition: state
      entity_id: light.wall_lights
      state: 'off'
  action:
    service: light.turn_on
    data:
      entity_id: light.wall_lights
      brightness: 75
      transition: 3

It should work with the Automation Editor (because the conditions use a logical and).


EDIT
Replaced:
{{ src == 'Netflix' or src == 'PS4' or src == 'Plex' }}
with:
{{ src in ['Netflix','PS4','Plex'] }}
as per Marius’ suggestion (see below).

2 Likes

nice! make it even shorter with:

{{ src in ['Netflix','PS4','Plex'] }}

as a personal preference (to be able to check as much as possible in the template editor) a suggestion to write the conditions as:

condition:
  - condition: template
    value_template: >
      {{is_state('sun.sun','below_horizon')}}
  - condition: template
    value_template: >
      {{is_state('light.wall_lights','off')}}
2 Likes

why not

condition:
  - condition: template
    value_template: >
      {{is_state('sun.sun','below_horizon') and is_state('light.wall_lights','off')}}

sure, but didn’t want to diverge to much from the original suggestion :wink:

it’s a bit of give and take with these conditions for my taste. I like it short and simple, but also like to make it easily maintainable and re-usable using logical ordering.

In this particular case, I would have kept them separated.

Hey, this is not working well.
It was triggered ones randomly while I was changing sources (Netflix to Plex), and never again.

This is what I use:

- id: '1562767530621'
  alias: Living room - TV source dim Lights
  trigger:
    platform: template
    value_template: >
      {% set src = state_attr('media_player.living_room_tv', 'source') %}
      {{ src in ['Netflix','PS4','Plex'] }}
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    service: light.turn_on
    data:
      entity_id: light.wall_lights
      brightness: 75
      transition: 3

Please describe the precise steps you performed to test the automation.

The automation is triggered whenever the media_player’s source attribute changes state and only if it changes to Netflix, PS4, or Plex (the names have to match exactly as shown).

you might have to change to trigger state, and use the sources as condition. I fear the template trigger is always true when changing sources, so doesnt change and hence doesnt trigger the automation.

changing to state should take care of that, and the condition will make sure it only changes when the sources are met.

- id: '1562767530621'
  alias: Living room - TV source dim Lights
  trigger:
    platform: state
    entity_id: media_player.living_room_tv
  condition:
    - condition: template
      value_template: >
        {% set src = state_attr('media_player.living_room_tv', 'source') %}
        {{ src in ['Netflix','PS4','Plex'] }}
      
    - condition: template
      value_template: >
        {{is_state('sun.sun','below_horizon')}}
    - condition: template
      value_template: >
        {{is_state('light.wall_lights','off')}}
  action:
    service: light.turn_on
    data:
      entity_id: light.wall_lights
      brightness: 75
      transition: 3

In theory, Home Assistant will create a listener for media_player.living_room_tv and monitor its source attribute for state-changes. Switching from Netflix to Plex is a state-change.

I believe you have media_players (I don’t), can you test the template trigger with your system to confirm it works (or does not work)?

Yes, I agree with you. Or put differently: you’re right :wink:
Will try tonight when I get back to my HA setups.

Theoretically both should work. I think…

Has the OP reported back yet?

Not yet :slight_smile:
I’ll check your code when I get back home.

@123
The names of the sources are exactly the same as in the automation.
This is what I did:

The TV is on other source not listed in the automation, and the lights are off.
I’ve change the source to Netflix with the TV remote and the automation didn’t triggered.
Than I changed it to Plex, the automation was triggered and the lights are on at 30%.
I’ve turned off the lights and tried again.
The automation didn’t triggered anymore while changing the sources.

OK, it’s not behaving like one would expect the template trigger to work.

As an experiment, replace my single template trigger with your original three template triggers (leave the condition as it is now). Let’s see if that fixes it.

  trigger:
  - platform: template
    value_template: "{{ is_state_attr('media_player.living_room_tv', 'source',
      'Netflix') }}"
  - platform: template
    value_template: "{{ is_state_attr('media_player.living_room_tv', 'source',
      'PS4') }}"
  - platform: template
    value_template: "{{ is_state_attr('media_player.living_room_tv', 'source',
      'Plex') }}"

If that doesn’t work properly, try Marius’ suggestion where it triggers on any change in the media_player’s state and then uses the condition to check if it’s the state-changes we want.

Theoretically, all three version should work equally well. If some work better than others then there’s a lesson to be learned here about which techniques are preferable.

1 Like

I’ve tested this:

automation:
  - alias: Spotify test 1
    trigger:
      platform: template
      value_template: >
        {% set src = state_attr('media_player.spotify', 'source') %}
        {{ src in ['Hall','Office','Woonkamer Speaker','Master bedroom Speaker',
                   'Chromecast Auditorium','HvB MacBook Air'] }}
    condition: []
    action:
      service: notify.notify
      data_template:
        title: Spotify trigger template
        message: >
         Spotify changed from {{trigger.from_state.attributes.source}} to {{trigger.to_state.attributes.source}}

  - alias: Spotify test 2
    trigger:
      platform: state
      entity_id: media_player.spotify
    condition:
      condition: template
      value_template: >
        {% set src = state_attr('media_player.spotify', 'source') %}
        {{ src in ['Hall','Office','Woonkamer Speaker','Master bedroom Speaker',
                   'Chromecast Auditorium','HvB MacBook Air'] }}
    action:
      service: notify.notify
      data_template:
        title: Spotify condition template
        message: >
         Spotify changed from {{trigger.from_state.attributes.source}} to {{trigger.to_state.attributes.source}}

both work:-)

I’ll post a screenshot from my iPhone , which displays a from_source (Telefoon) not listed here. It won’t trigger when the to_source is not listed.

Note: upon restart only the condition template automation fires.

update

It also keeps firing even without changing sources, indicating it triggers on other state/attribute changes…

this leads to a quick conclusion the trigger template automation is preferable :wink:

59

1 Like

Thanks for testing it! That’s encouraging news. Not sure why the same template design didn’t work for Delicon.

It’s quite possible that the media player he is using doesn’t update immediately. I have a few media players that only update on 30 second mark (or so) which leads to delays and odd behavior if you aren’t patient.

EDIT: The media players that I have that act like this are the custom_component alexa media players. HA polls the Echo for changes. The Echo doesn’t report to HA, if that makes sense.

Hey, I’ve tried this and it’s working almost every time,
but only when the source is changing from source-not-in-the-list to source-in-the-list.
When I’m changing only the sources in the list nothing happens.

trigger:
    platform: template
    value_template: >
        {% set src = state_attr('media_player.living_room_tv', 'source') %}
        {{ src in ['Netflix','PS4','Plex'] }}

EDIT: this is working every time, but… :slight_smile:
If I turn off the lights when I’m watching Netflix and just change the volume/play/pause, the light will turn on to 30%.

trigger:
  - entity_id: media_player.living_room_tv
    platform: state
condition:
  - condition: template
    value_template: >
      {% set src = state_attr('media_player.living_room_tv', 'source') %}
      {{ src in ['Netflix','PS4','Plex'] }}
  - condition: state
    entity_id: sun.sun
    state: below_horizon

I just confirmed your results.

For the experiment, I used the following input_select:

input_select:
    name: Items
    options:
      - 'off'
      - item1
      - item2
      - item3
      - item4
      - item5
    initial: 'off'

to trigger to this automation:

- alias: 'state changing test'
  trigger:
    platform: template
    value_template: >
      {% set src = states('input_select.items') %}
      {{ src in ['item1', 'item2', 'item3', 'item4'] }}
  action:
  - service: system_log.write
    data_template:
      level: warning
      message: "From: {{trigger.from_state.state}} To: {{trigger.to_state.state}}"

You’ll notice the template’s list does not contain off and item5.

  1. When I changed the input_select from off to item1, the automation was triggered.
  2. When I changed it from item1 to item2, it did not trigger the automation.
  3. When I changed it to item5 and then to item1, the automation was triggered.

Like you discovered, changing between any of the states shown in the list will not trigger the automation. It will only trigger if the previous state was not in the list.

That does not work the way I expected! I learned something new today. :slight_smile:

I believe this is not what I experience, if I read you correctly? Are you saying the automation only triggers on a source not listed?
Wonder how that is possible, since I see it trigger on all changes. It is however the thought that made me place my initial thoughts here: Automation with multiple triggers not working - #19 by Mariusthvdb

Please note that I referenced the not-listed-source as an exception after my testing automations, but there that makes perfect sense. Automation with multiple triggers not working - #24 by Mariusthvdb

I’m saying in order to trigger, it has to change from an unlisted source to a listed source.

In my experiment it failed to trigger when it changed from a listed source to another listed source

Given the following:

  • The input_select’s options: ['off', 'item1', 'item2', 'item3', 'item4', 'item5']
  • Template’s list: ['item1', 'item2', 'item3', 'item4']

Changing from:

  • off to item1trigger
  • item1 to item2 → no trigger
  • item2 to item3 → no trigger
  • item3 to item5 → no trigger
  • item5 to item1trigger

This is the behavior you predicted when you said:

yes, thats what I feared, but didnt establish in my testing. Cant replicate right now, since my googlehomes act up, so will have to get back on that later.

However, since the condition template automation worked fine too, with a few considerations, I suggest using that after all. Adding this generic safeguard condition, to prevent it firing on non state changes, attribute changes when in the same state etc etc.

      - condition: template
        value_template: >
          {{ trigger.to_state.state is not none and
             trigger.from_state.state is not none and
             trigger.to_state.state != trigger.from_state.state }}