Automation syntax question with the trigger entity

I’m trying to wrap my mind around the correct syntax for this automation.
Basically, any time an entity in the “media_player” domain turns off, I want to reset it’s volume to ‘0.5’.
The reason being is that I have lots of smart speakers and I want to reset their volumes to this specific level after any activity.

Thanks in advance.

Updated below to what worked.

alias: Reset Speaker Volumes
description: ''
trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: "{{ (trigger.event.data.entity_id.split('.'))[0] == 'media_player' and trigger.event.data.new_state.state == 'off' }}"
action:
  - service: media_player.volume_set
    data:
      entity_id: '{{ trigger.event.data.entity_id }}'
      volume_level: 0.5
mode: single

Your template is not in the correct format, and I recommend reading this.

This is the correct syntax for that template condition:

value_template: "{{ trigger.event.data.new_state.state == 'off' }}"

But I don’t think you’ll be able to do what you want because domain isn’t part of the state_changed event structure. You can check in Developer Tools->Events if you listen for state_changed. Instead, you’ll probably need to just trigger on every state_changed event and use a condition to filter out only media_player entities that were just turned off:

trigger:
  - platform: event
    event_type: state_changed
condition:
  - condition: template
    value_template: "{{ (trigger.event.data.entity_id.split('.'))[0] == 'media_player' and trigger.event.data.new_state.state == 'off' }}"
action:
  - service: media_player.volume_set
    data:
      entity_id: "{{ trigger.event.data.entity_id }}"
      volume_level: 0.5
mode: single

Also review this for what trigger variables are available for each trigger platform. You’re mixing up event triggers and trigger variables for a state trigger.

1 Like

Thanks for the direction. Ill look it over and give it a shot.

Question for you.
How would you do the same but rather than the split with the comparison operator, using match and the is_state() method?
Something like this.

value_template: "{{ (trigger.event.data.entity_id) is match('^media_player', ignorecase=True) and is_state(trigger.event.data.new_state.state, 'off') }}"

Is this possible?
This method could prove useful with regex matching for other automations.

1 Like

You can try it :slight_smile: . I would expect it to work as well, but parsing regex is a lot more processor-intensive than a split().

Either I can’t get the syntax correct or I suspect I’m running into an issue with limited templates. I couldn’t find definite answers of what qualifies as a “limited template”.

Kind of confusing when the below recommendation is highlighted on that page.

It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

Basically a “limited template” is a template that cannot contain any functions.

That might be a bit of an oversimplification but (at this point in time) it’s effectively true.

Not to sound defeatist but many things you would normally want to do with a template, can’t be done with a “limited template” (which makes the word “template”, in “limited template”, a stretch but there it is).

This post might shed some more light on it:

1 Like