Trigger on media_player state changed

alias: New automation
description: ""
triggers:
  - trigger: state
    entity_id:
      - media_player.speaker_1
      - media_player.speaker_2
      - media_player.speaker_3
      - media_player.speaker_4
      - media_player.speaker_5
    to:
      - unavailable
      - unknown
      - idle
conditions: []
actions:
  - action: persistent_notification.create
    metadata: {}
    data:
      message: >-
        Entity {{ trigger.entity_id }} changed state to {{
        trigger.to_state.state }}
mode: single

Please guide how can I make the above automation more generalised by not hard-coding the media_player names. My goal is to create a notification if any of the player i.e. media_player.speaker_1 to media_player.speaker_5 changes is state to idle, unknown or unavailable.

Kindly guide.

Change your entity_id: to this:

entity_id: "{{ states.media_player | map(attribute='entity_id') | list }}"

Message malformed: Entity {{ states.media_player | map(attribute=‘entity_id’) | list }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]

Getting this error

This is a test automation showing how it can work:

alias: test
description: ""
triggers:
  - trigger: state
    entity_id:
      - switch.lamp_switch
    to: "on"
conditions: []
actions:
  - action:switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: |
        {{ states.switch | map(attribute='entity_id') | list }}
mode: single
entity_id: |
        {{ states.switch | map(attribute='entity_id') | list }}

This part works fine in actions. But not in triggers

My bad, try:

trigger:
  - platform: state
    entity_id: "{{ states.media_player | map(attribute='entity_id') | list | join(', ') }}"

Still getting the same error. Can you please post the whole yaml

Hi,

I was using dev tools to trial some templates. You might be able to do it with a template trigger. If I have time, I’ll take a look later.

Plz check whenever you have time.

state triggers do not support templates.

So what is your goal? If you won’t tell, no-one can help you.

Probably should have been ‘… to create a notification…’

Sorry it was a typo. My goal is to create a notification if any of the player i.e. media_player.speaker_1 to media_player.speaker_5 changes is state to idle, unknown or unavailable.

Yes… it was a typo…:face_with_hand_over_mouth:

What is the correct way?

Please guide

Hello Vandana Dass,

You are also going to have problems when putting stuff relying on the state machine into the trigger. Look up limited templates.

I would put the affected media players in a group maybe and test the group?