I have four Xiaomi zigbee gates and occasionally one of the family members will not close a gate properly. I have an icon on my 10" display that flashes a red gate icon when any gate opens (group.garden_gates) but I want to be able to publish a topic when the flashing red gate button is pushed that invokes a Google Home TTS notification that advises which gate is left open.
So I need some help with the data template for the speech engine that will only read out the gate/s that are open/on. Is that doable?
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 %}