I created the following automation in order to group all the media player notifications. but something doesn’t work. could anyone take a look? thank you
- id: Home player playing notification
alias: 'Home player playing notification'
trigger:
- platform: state
entity_id: media_player.plex_kodi_add_on_localhost
to: 'playing'
- platform: state
entity_id: media_player.plex_tv_ue48ju7500
to: 'playing'
- platform: state
entity_id: media_player.plex_plex_01ad68da_eea9_43a2_8af2_7901942af3fc
to: 'playing'
- platform: state
entity_id: 'media_player.plex_plex_f872035d665e6f7f_com_plexapp_android'
to: 'playing'
- platform: state
entity_id: media_player.apple_tv
to: 'playing'
- platform: state
entity_id: media_player.kodi_i7
to: 'playing'
- platform: state
entity_id: media_player.cucina_home_mini
to: 'playing'
- platform: state
entity_id: media_player.spotify
to: 'playing'
- platform: state
entity_id: 'media_player.2o_echo_dot_di_matteo'
to: 'playing'
- platform: state
entity_id: media_player.multisound
to: 'playing'
condition: []
action:
- service: notify.ios_iphone_matteo
data_template:
title: "Smart Home Alerts"
message: >-
{%- if states('trigger.to_state.state') == 'playing' -%}
Media Player Playing {{ state_attr('trigger.entity_id', 'media_content_type') }} : {{ state_attr('trigger.entity_id', 'media_title') }
{% else %}
Media Player Stop
{% endif %}
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘}’) for dictionary value @ data[‘action’][0][‘data_template’][‘message’]. Got None. (See /config/configuration.yaml, line 1241). Please check the docs at https://home-assistant.io/components/automation/
ok i corrected. but unfortunately does not show the media player content.
notification occurs only when it stops playing.
I don’t know if the use of is correct
Media Player Playing {{ state_attr(‘trigger.entity_id’, ‘media_content_type’) }} : {{ state_attr(‘trigger.entity_id’, ‘media_title’) }
Your syntax looks correct but I’m not 100% sure. All I can suggest is to play some media and look at the attributes of the media player in the developer tools states menu.
Start playing a media file.
Go to the dev tools states menu.
Look for the media player.
Check the attributes of that media player.
Use those attributes with trigger.to_state.attributes...
@tom_l Work but work only for the first song… for the other there isn’t notification
I think you should compare the change in the media from the previous one to the current one that is playing because the state always remains on playing
- id: Home player playing notification
alias: 'Home player playing notification'
trigger:
- platform: state
entity_id: media_player.plex_kodi_add_on_localhost
to: 'playing'
- platform: state
entity_id: media_player.plex_tv_ue48ju7500
to: 'playing'
- platform: state
entity_id: media_player.plex_plex_01ad68da_eea9_43a2_8af2_7901942af3fc
to: 'playing'
- platform: state
entity_id: 'media_player.plex_plex_f872035d665e6f7f_com_plexapp_android'
to: 'playing'
- platform: state
entity_id: media_player.apple_tv
to: 'playing'
- platform: state
entity_id: media_player.kodi_i7
to: 'playing'
- platform: state
entity_id: media_player.cucina_home_mini
to: 'playing'
- platform: state
entity_id: media_player.spotify
to: 'playing'
- platform: state
entity_id: 'media_player.2o_echo_dot_di_matteo'
to: 'playing'
- platform: state
entity_id: media_player.multisound
to: 'playing'
condition: []
action:
- service: notify.ios_iphone_matteo
data_template:
title: "Smart Home Alerts"
message: >-
{%- if states('trigger.to_state.state') == 'playing' -%}
{{ trigger.to_state.attributes.friendly_name }} is playing {{ trigger.to_state.attributes.media_title }}
{% else %}
{{ trigger.to_state.attributes.friendly_name }} is playing {{ trigger.to_state.attributes.media_title }}
{% endif %}
You guys keep putting quotes around the trigger object making the if statement pointless. You then pass the string to the state machine function. That’s also a no-no because you already have the state. But if you are trying to be safe about it, you need to pass the entity_id to the states method. If you are trying to make this ‘safe’, use the following code. But to be honest, your triggers are always ‘playing’ so you don’t really need an if statement.
This will trigger on any state change. If you are playing, it will output the media. Otherwise it will just say that you’re off, paused or stopped.
alias: 'Home player playing notification'
trigger:
- platform: state
entity_id: media_player.plex_kodi_add_on_localhost
- platform: state
entity_id: media_player.plex_tv_ue48ju7500
- platform: state
entity_id: media_player.plex_plex_01ad68da_eea9_43a2_8af2_7901942af3fc
- platform: state
entity_id: media_player.plex_plex_f872035d665e6f7f_com_plexapp_android
- platform: state
entity_id: media_player.apple_tv
- platform: state
entity_id: media_player.kodi_i7
- platform: state
entity_id: media_player.cucina_home_mini
- platform: state
entity_id: media_player.spotify
- platform: state
entity_id: media_player.2o_echo_dot_di_matteo
- platform: state
entity_id: media_player.multisound
action:
- service: notify.ios_iphone_matteo
data_template:
title: "Smart Home Alerts"
message: >
{% if states(trigger.entity_id) == 'playing' -%}
{{ state_attr(trigger.entity_id,'friendly_name') }} is playing {{ state_attr(trigger.entity_id,'media_title') }}
{% else %}
{{ state_attr(trigger.entity_id,'friendly_name') }} is {{ states(trigger.entity_id) }}
{% endif %}
Just dealing with all playing triggers, this is all you need:
- id: Home player playing notification
alias: 'Home player playing notification'
trigger:
- platform: state
entity_id: media_player.plex_kodi_add_on_localhost
to: 'playing'
- platform: state
entity_id: media_player.plex_tv_ue48ju7500
to: 'playing'
- platform: state
entity_id: media_player.plex_plex_01ad68da_eea9_43a2_8af2_7901942af3fc
to: 'playing'
- platform: state
entity_id: media_player.plex_plex_f872035d665e6f7f_com_plexapp_android
to: 'playing'
- platform: state
entity_id: media_player.apple_tv
to: 'playing'
- platform: state
entity_id: media_player.kodi_i7
to: 'playing'
- platform: state
entity_id: media_player.cucina_home_mini
to: 'playing'
- platform: state
entity_id: media_player.spotify
to: 'playing'
- platform: state
entity_id: media_player.2o_echo_dot_di_matteo
to: 'playing'
- platform: state
entity_id: media_player.multisound
to: 'playing'
condition: []
action:
- service: notify.ios_iphone_matteo
data_template:
title: "Smart Home Alerts"
message: >
{{ state_attr(trigger.entity_id,'friendly_name') }} is playing {{ state_attr(trigger.entity_id,'media_title') }}
Notice how in both instances trigger objects in the templates are not wrapped in quotes. That’s your root cause for all the issues in this thread.