Automation with multiple triggers not working

I made this automation to trigger when any of the TV sources is selected to dim the wall lights, but it doesn’t get triggered. Whats wrong with it?

- id: '1562767530621'
  alias: Living room - TV source dim Lights
  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'') }}'
  condition:
  - after: sunset
    before: sunrise
    condition: sun
  action:
  - data:
      brightness: 75
      entity_id: light.wall_lights
      transition: 3
    service: light.turn_on

You have a whole bunch of two single quotes '' instead of double quotes ".

  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") }}'

I see that, but that was added when I created the automation from the HA front page.
I’ll see if this works. Thanks.

  condition:
  - after: sunset
    before: sunrise
    condition: sun

I don’t think that condition can ever evaluate as true. On any given day, it cannot be both before sunrise and after sunset.

I think you need to use an “or” condition in there with two separate conditions.

1 Like

That’s not the period e.g after 20:00 and before 05:00?
I don’t think I need an OR for that.

Nope, even the documentation says so. Here’s the example it gives to solve the issue:

condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise - equivalent to a state condition on `sun.sun` of `below_horizon`
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
2 Likes

So, on any given day, can it be both after 20:00 and before 05:00? Rhetorical question - of course it can’t. I am almost 100% certain that I have read somewhere that is how it works.

I understand your logic (if it is after 20:00 then it is before 05:00 the next day), but I am fairly certain that in this case it is per day, so it can’t be both.

Give it a try and find out.

… and it looks like @petro beat me to the punch and has found it and given you the actual condition you need to use.

Kudos to @petro - he knew where it was.

This shows: ‘Unsupported condition: or’ in the automation editor.

You can’t use the automation editor for or conditions, you need to make it via yaml.

That’s what I did.
I only opened the automation editor to check if everything is ok.
The config validation is ok.

I’ll check it if is working fine when I’ll get home.

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)?