Help creating a template that will determine what gate is left open

Outstanding @123! Thank you. Your commitment to grammar is unparalleled and appreciated. :grin: Mods done!

Any chance you could hope over here and comment? Trying to get my head around sending TTS messages to devices that arenā€™t playing music using placeholders like '{{volume}}' and '{{tts}}' so I can refer to one script all the time but Iā€™ve never done that before. Wife is going spare when my HA notifications interrupt her music.

I donā€™t have any media players which makes it impossible for me to test templates for them (or understand why a group of media players doesnā€™t report being on when one of them is playing). I have helped other people with their media player issues but I had a difficult time because I was mostly ā€˜flying blindā€™. All this to say that I canā€™t be very effective in this case.

Thanks, understood. I got the following to work but not quite sure about the '{{volume}}' and '{{tts}}' stuff but Iā€™ll refer the other thread to see if I can work that out.

{%- if states.media_player.kitchen_home.state != 'playing' and
    states.media_player.insignia_speaker.state != 'playing' and                
states.media_player.kitchen_home.state != 'playing' %}
                  media_player.upstairs_homes
              {%- else %}
                  {%- set players = ['media_player.kitchen_home', 'media_player.insignia_speaker', 'media_player.lounge_home'] %}
                  {{ states.media_player | selectattr('state','!=','playing') | selectattr('entity_id', 'in', players) | map(attribute='entity_id') | join(', ') }}
              {%- endif %}

This probably isnā€™t powerful enough for what you want but something I use to prevent my media players from trying to push out TTS messages at the wrong time is to include the following in my automation just before the TTS action:

wait_template: "{{ is_state('media_player.lounge_mini', 'off' or 'idle') }}"

which then makes the TTS wait until the media player isnā€™t actively playing something already

Thanks Dave. I need the notifications to happen instantly and just to ignore the GH devices that are actually playing music. Iā€™m half way there so Iā€™ll keep chipping on.

Letā€™s assume you have a group containing all your media_players. To get a list of which ones in the group are currently not playing, you can use this template:

{{ expand('group.media_players') | selectattr('state','!=','playing') | map(attribute='entity_id') | join(', ') }}

The expand function was introduced in a recent version of Home Assistant (so it wonā€™t work if you are using an old version).

2 Likes

Thanks, but doesnā€™t appear to work. Running 0.98.5. Running the code below in the template editor when one of the two speakers in the group is playing produces media_player.upstairs_homes. I would have thought this should have reported media_player.lounge_home in my case, the speaker that was not playing.

{{ expand('media_player.upstairs_homes') | selectattr('state','!=','playing') | map(attribute='entity_id') | join(', ') }}

In my use scenario though it might not be needed. The only time we invoke a group media call is for TTS notifications so it is unlikely, at least in our house, that the group would be playing music. If we do listen to music it tends to be just the GH in the zone weā€™re in.

The expand function is for expanding groups, not entities. Your template is attempting to expand an entity.

 expand('media_player.upstairs_homes')  <--- invalid

My bad! Thanks for the clarification. I think that will come in real handy for future automations.

Any chance you could tweak your code for my date_time sensor that notifies me of the minutes my devices are on?

I have this to use in a TTS data_template notification but;

{{ (as_timestamp(now())
              - state_attr('input_datetime.soldering_iron_start', 'timestamp'))
             |timestamp_custom('%M', false) | int }} minutes

If the value = 1, then the last word should ā€˜minuteā€™ not ā€˜minutesā€™

Getting pedantic I know but Iā€™m a stickler for detail. :crazy_face:

{% set x = (as_timestamp(now())
              - state_attr('input_datetime.soldering_iron_start', 'timestamp'))
             |timestamp_custom('%M', false) | int %}
{{x}} minute{{'' if x == 1 else 's'}}
3 Likes

As always, mind blown. Thank you! Iā€™ll give that a go.