Automation with multiple triggers not working

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 }}