Thanks for the quick reply. That looks promising but the gate state may have changed from closed to open say 30 minutes ago so I’m unsure if the trigger.to_state.attributes.friendly_name will work? I’ve not used that before so I don’t know.
I already have a TTS notification automation that notifies me when the gates open but I want to now push a button and essentially ask which ones are open/on ignoring the ones that are closed/off.
I don’t want an automation instead having someone push an icon to request gate status reading out only the open ones. So it’s more of an instant check i.e. What is the state now and if closed, do nothing and if open, tell me which one.
the only way I can think of is
input_boolean that the turn on when you touch the icon on the screen which then exe the automation and the last action will be to turn off the input_boolean.
Thanks, appreciate it. I’ve taken parts of a template that @petro and @123 help create here and come up with this to test my theory.
{%- if states.switch.plug_lamp.state != 'on' and
states. switch.server_plug_old.state != 'on' %}
"All switches are off"
{%- else %}
{%- set switches = ['switch.plug_lamp', 'switch.server_plug_old'] %}
{{ states.switch | selectattr('state','!=','on') | selectattr('entity_id', 'in', switches) | map(attribute='entity_id') | join(', ') }}
{%- endif %}
If both switches are off, the result is “All switches are off”. If one switch is on, it displays the entity ID of the one that is off which is what I need. I do however require the friendly name but I tried replacing both instances of entity_id with friendly_name but that did not work as nothing is displayed when I do that.
The first line creates a list called switches containing the devices that are currently off.
The second line counts the number of items in the list. If the result equals 2, that means both devices are off and so it prints the message ‘All switches are off’.
- 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.
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?
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.