Notification ios

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

For a start you need an else statement to handle what happens when the trigger to state is not ‘playing’.

i check this solution

don’t work…

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/

You’re missing a } at the end of your playing message.

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.

i don’t know if trigger.entity_id is correct and media_content_type’

try

{{ trigger.to_state.attributes.media_content_type }}

If media_content_type is the correct attribute. See my post above about using the dev tools states menu to determine if this is correct.

is correct but does not even show the source of the trigger trigger.entity_id

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 :frowning:

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

What if you remove the to: 'playing' from your triggers. Does that trigger on change of track?

e.g.

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
...etc

continues to notify the state is playing always even if the media doesn’t play

Yes that’s why you have this:

{%- if states('trigger.to_state.state') == 'playing' -%}

how could I solve, so having notifications only when the media sounds and no notification is there no activity?

Maybe like this (untested):

  action:
    - service_template: >
        {%- if states('trigger.to_state.state') == 'playing' -%}
          notify.ios_iphone_matteo
          data:
            title: "Smart Home Alerts"
            message: {{ trigger.to_state.attributes.friendly_name }} is playing {{ trigger.to_state.attributes.media_title }}
        {% else %}
          script.a_script_that_does_nothing
        {% endif %}

You will have to also write a script that does nothing, e.g. it just calls a 1 sec delay.

service _template and after script?
Not work

A script is a service. If it does not work what error is being generated.

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.