Thanks so much. Do you know if there is a way I can use your code directly in a TTS message so I can add it to an automation?
- service: tts.google_say
entity_id: media_player.lounge_home
data:
message: ??
Thanks so much. Do you know if there is a way I can use your code directly in a TTS message so I can add it to an automation?
- service: tts.google_say
entity_id: media_player.lounge_home
data:
message: ??
- service: tts.google_say
entity_id: media_player.lounge_home
data_template:
message: >
{% set switches = [states.switch.plug_lamp, states.switch.server_plug_old] | selectattr('state','eq','off') | list %}
{% if (switches | count) == 2 %}
All switches are off.
{% else %}
{{ switches | map(attribute='attributes.friendly_name') | join(' ') }} is off.
{% endif %}
If thereâs a possibility for both switches to be on
then you will need to modify the templateâs logic (handle the case when the count is zero). Otherwise, the template will produce no device name and say
" is off."
Thanks again! The template will eventually be used on four gates and there could be a possibility that two could be open at the same time. Iâm just testing it on my two switches as they are inside and easy to manipulate for testing.
got me thinking more wanted to know how to do it
{%- set switches = ['light.hallway', 'light.office'] %}
{{ states.light | selectattr('state','!=','on') | selectattr('entity_id', 'in', switches) | map(attribute='name') | join(', ') }}
found out name does displays the name
then came back here and @123 has the answer
tested
{%- set switches = ['light.hallway', 'light.office'] %}
{{ states.light | selectattr('state','!=','on') | selectattr('entity_id', 'in', switches) | map(attribute='attributes.friendly_name') | join(', ') }}
all you have to is make the switches array bigger
{%- set switches = [âswitch.gate1â, âswitch.gate2â, âswitch.gate3â, âswitch.gate4â] %}
if gate1 and gate3 are off it will say
GATE1 GATE3 is off
Any chance you could re-look at the logic? If both switches are off, it reports correctly but if one is turned on, it currently tells me the one that is off but I need to know the one that is on?
{% set switches = [states.switch.plug_lamp, states.switch.server_plug_old] %}
{% if (switches | selectattr('state','eq','off') | list | count) == 2 %}
All switches are off.
{% else %}
{{ switches | selectattr('state','eq','on') | list | map(attribute='attributes.friendly_name') | join(', ') }}
{% endif %}
Thank you so much. Here is my final code.
tts_which_gate_is_open:
alias: 'Speak which gate is open'
sequence:
- service: tts.google_say
entity_id: media_player.lounge_home
data_template:
message: >
{% set gates = [states.binary_sensor.door_window_sensor_courtyard, states.binary_sensor.door_window_sensor_path_gate,
states.binary_sensor.door_window_sensor_pool_gate, states.binary_sensor.door_window_sensor_steel_gate] %}
{% if (gates | selectattr('state','eq','off') | list | count) == 4 %}
All gates are closed
{% else %}
The following gates are open. The {{ gates | selectattr('state','eq','on') | list | map(attribute='attributes.friendly_name') | join(' and ') }}
{% endif %}
Hereâs a minor refinement that adjusts the spoken phrase according to how many gates open. The changes are in the {% else %}
section.
One gate:
The following gate is open. The Pool Gate.
More than one gate:
The following gates are open. The Pool Gate and Steel Gate.
data_template:
message: >
{% set gates = [states.binary_sensor.door_window_sensor_courtyard, states.binary_sensor.door_window_sensor_path_gate,
states.binary_sensor.door_window_sensor_pool_gate, states.binary_sensor.door_window_sensor_steel_gate] %}
{% if (gates | selectattr('state','eq','off') | list | count) == 4 %}
All gates are closed
{% else %}
{% set open_gates = gates | selectattr('state','eq','on') | list %}
{% if open_gates | count > 1 %}
{% set x = 'gates are' %}
{% set y = ' and ' %}
{% else %}
{% set x = 'gate is' %}
{% set y = '' %}
{% endif %}
The following {{ x }} open. The {{ open_gates | map(attribute='attributes.friendly_name') | join(y) }}.
{% endif %}
Outstanding @123! Thank you. Your commitment to grammar is unparalleled and appreciated. 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).
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.
{% 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'}}
As always, mind blown. Thank you! Iâll give that a go.