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

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?

I Have this

this automation send a message when a door is open / close

- id: 'notify stephan phone Door Change'
  alias: "notify Stephan phone Door Change"
  initial_state: true
  trigger:
  - entity_id: 
    - binary_sensor.garage_side_door
    - binary_sensor.garage_door
    - binary_sensor.front_door
    - binary_sensor.cupboard_door
    platform: state
  action:
  - data_template:
      title: "Home Assistant"
      message: "{{ trigger.to_state.attributes.friendly_name }} was {% if trigger.to_state.state == 'on' %} Open {% else %} Closed {% endif %}"
    service: notify.stephan_phone

think you are looking for

{{ trigger.to_state.attributes.friendly_name }} 
1 Like

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.

that will my my case display the name in your case should say the friendly_name

what about

use the for:

    for: '00:10:00'
    platform: state
    to: 'on'

when its been on for 10mins it will do the action bit

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.

that my 5 cents

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.

i would play with

selectattr(‘entity_id’, ‘in’, switches) to selectattr(‘friendly_name’, ‘in’, switches)

or / and
map(attribute=‘entity_id’) to map(attribute=‘friendly_name’)

and pray

Yep, tried that. Got no output when doing that.

{% 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(' ') }}
{% endif %}

Thanks @123! I was just typing out the following but yours is a lot cleaner. What does the count == 2 do?

{%- 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='attributes.friendly_name') | join(', ') }}
              {%- endif %}

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

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

1 Like

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(', ') }}

@xbmcnut

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

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