Help with alarm automation

Hello @sheminasalam ,
Sorry, but removing the “0” is not the solution, because it’s the part of the entity name, automatically created because it’s the zigbee network id of the device :frowning:
really thank you for your help!
Simon

Did you try with 0 in the entity_id? Is it working for you?

Nope, same error:

TemplateSyntaxError: expected token ',', got 'x00158d00047e76e4_contact'

I dunno why :frowning:

Without “0” it doesn’t work, because the entity id doesn’t exist

Try replacing e.g. states.binary_sensor.x00158d00047e76e4_contact with states('binary_sensor.0x00158d00047e76e4_contact')?

Edit: for this to work with the selectattr proposal, we need to return a list of state objects, not just on / off strings. Need to instead use states.binary_sensor.['0x00158d00047e76e4_contact'] syntax which gets around the sensor name starting with a number and still returns a state object. See my post below.

Docs.

1 Like

what @Troon said should work. Was typing the same. Has updated the code, Please try it but i havent checked it. If that doesn’t work please try changing the entity_id from integration>entities

Uhm, script accepted, but the sensors seems all closed, but they aren’t.

allarme_verifica_portefinestre:
  alias: Allarme Verifica stato sensori Porte e Finestre
  sequence:
    - service: notify.xefilbot
      data:
        title: '*Avviso Porte/Finestre Aperte*'
        message: >-
          {% set doors = [ states('binary_sensor.0x00158d00047e76e4_contact'),
          states('binary_sensor.0x00158d000477376f_contact'),
          states('binary_sensor.0x00158d00047d6600_contact'),
          states('binary_sensor.0x00158d00047d65df_contact'),
          states('binary_sensor.0x00158d0004a0257c_contact'),
          states('binary_sensor.0x00158d00047d65fc_contact'),
          states('binary_sensor.0x00158d0004521ef0_contact'),
          states('binary_sensor.0x00158d00049fd978_contact'),
          states('binary_sensor.0x00158d00047e9d51_contact'),
          states('binary_sensor.0x00158d00047ebb8e_contact')] | selectattr('state',
          'eq', 'on') | map(attribute='name') | list %} {% set qty = doors | count
          %} {% if qty == 0 %} All exterior doors are closed. {% else %} Following
          doors are open:
                  -{{doors | join('
                  -')}}
          {% endif %}
mode: single

Try this syntax, then, as the prior version just gives a list of on and off strings, which doesn’t work with the filters:

alias: Allarme Verifica stato sensori Porte e Finestre
sequence:
  - service: notify.xefilbot
    data:
      title: '*Avviso Porte/Finestre Aperte*'
      message: >-
        {% set doors = [
        states.binary_sensor['0x00158d00047e76e4_contact'],
        states.binary_sensor['0x00158d000477376f_contact'],
        states.binary_sensor['0x00158d00047d6600_contact'],
        states.binary_sensor['0x00158d00047d65df_contact'],
        states.binary_sensor['0x00158d0004a0257c_contact'],
        states.binary_sensor['0x00158d00047d65fc_contact'],
        states.binary_sensor['0x00158d0004521ef0_contact'],
        states.binary_sensor['0x00158d00049fd978_contact'],
        states.binary_sensor['0x00158d00047e9d51_contact'],
        states.binary_sensor['0x00158d00047ebb8e_contact']] | selectattr('state',
        'eq', 'on') | map(attribute='name') | list %} {% set qty = doors | count
        %} {% if qty == 0 %} All exterior doors are closed. {% else %} Following
        doors are open:
                -{{doors | join('
                -')}}
        {% endif %}
mode: single

Hello @Troon !
Works as template (only the message part), but not as script. In that case it recognized all doors closed even if one is open :frowning:
Thanks!
Simon

Please post your current automation in full so I can see where you’ve got to.

Hello @Troon , of course:

allarme_verifica_portefinestre:
  alias: Allarme Verifica stato sensori Porte e Finestre
  sequence:
    - service: notify.xefilbot
      data:
        title: '*Avviso Porte/Finestre Aperte*'
        message: >-
        {% set doors = [
        states.binary_sensor['0x00158d00047e76e4_contact'],
        states.binary_sensor['0x00158d000477376f_contact'],
        states.binary_sensor['0x00158d00047d6600_contact'],
        states.binary_sensor['0x00158d00047d65df_contact'],
        states.binary_sensor['0x00158d0004a0257c_contact'],
        states.binary_sensor['0x00158d00047d65fc_contact'],
        states.binary_sensor['0x00158d0004521ef0_contact'],
        states.binary_sensor['0x00158d00049fd978_contact'],
        states.binary_sensor['0x00158d00047e9d51_contact'],
        states.binary_sensor['0x00158d00047ebb8e_contact']] | selectattr('state',
        'eq', 'on') | map(attribute='name') | list %} {% set qty = doors | count
        %} {% if qty == 0 %} All exterior doors are closed. {% else %} Following doors are open:
 - {{doors | join('
                -')}}
        {% endif %}
mode: single

Not sure what more I can suggest. I set up exactly the same script via the UI (just tidied a bit):

alias: Allarme Verifica stato sensori Porte e Finestre
sequence:
  - service: notify.admin   <-- note different for my system
    data:
      title: Avviso Porte/Finestre Aperte
      message: >-
          {% set doors = [ 
                   states.binary_sensor['leaf_connected'],
                   states.binary_sensor['motoe2_4g_lte_is_charging'],
                   states.binary_sensor['leaf_charging']
                 ] | selectattr('state', 'eq', 'off')
                   | map(attribute="name")
                   | list
          %}
          {% set qty = doors | count %}
          {% if qty == 0 %}
            All exterior doors are closed.
          {% else %}
            Following doors are open:
            - {{ doors | join('\n- ') }}
          {% endif %}

dropping in some binary sensors that I have on my system and my own notification, and the notification came through identifying the two sensors of the three that are in the off state.

I note when I look at the created scripts.yaml, the system has formatted the message differently:

allarme_verifica_stato_sensori_porte_e_finestre:
  alias: Allarme Verifica stato sensori Porte e Finestre
  sequence:
  - service: notify.admin
    data:
      title: Avviso Porte/Finestre Aperte
      message: "{% set doors = [ \n         states.binary_sensor['leaf_connected'],\n\
        \         states.binary_sensor['motoe2_4g_lte_is_charging'],\n         states.binary_sensor['leaf_charging']\n\
        \       ] | selectattr('state', 'eq', 'off')\n         | map(attribute=\"\
        name\")\n         | list\n%} {% set qty = doors | count %} {% if qty == 0\
        \ %}\n  All exterior doors are closed.\n{% else %}\n  Following doors are\
        \ open:\n  - {{ doors | join('\\n- ') }}\n{% endif %}"

Perhaps delete your YAML and paste it into the UI script editor instead: set the name then switch to YAML mode and paste in this, which is just your code tidied.

alias: Allarme Verifica stato sensori Porte e Finestre
sequence:
  - service: notify.xefilbot
    data:
      title: '*Avviso Porte/Finestre Aperte*'
      message: >-
        {% set doors = [
                        states.binary_sensor['0x00158d00047e76e4_contact'],
                        states.binary_sensor['0x00158d000477376f_contact'],
                        states.binary_sensor['0x00158d00047d6600_contact'],
                        states.binary_sensor['0x00158d00047d65df_contact'],
                        states.binary_sensor['0x00158d0004a0257c_contact'],
                        states.binary_sensor['0x00158d00047d65fc_contact'],
                        states.binary_sensor['0x00158d0004521ef0_contact'],
                        states.binary_sensor['0x00158d00049fd978_contact'],
                        states.binary_sensor['0x00158d00047e9d51_contact'],
                        states.binary_sensor['0x00158d00047ebb8e_contact']
                       ] | selectattr('state', 'eq', 'on')
                         | map(attribute='name')
                         | list %}
        {% set qty = doors | count %}
        {% if qty == 0 %}
          All exterior doors are closed.
        {% else %}
          Following doors are open:
          - {{ doors | join('\n- ') }}
        {% endif %}

What does this show in Developer Tools / Template?

{{ states.binary_sensor['0x00158d00047e76e4_contact']['name'] }}

Hello @Troon ,
TRying to follow your hints i get:

Message malformed: extra keys not allowed @ data['sequence'][0]['sequence']

Why? Just copied and pasted after deleting the script in the yaml, then added new one, only name set and switched to yaml editor.

The {{ states.binary_sensor['0x00158d00047e76e4_contact']['name'] }} answers me with the name of the entity correctly.

Sorry @Troon my fault. I was editing in YAML, but not the whole script, only the service data part. I had never seen that it’s possible to edit manually a part of the script as well.
This version seems to work, veryvery thank you!!!
Let me play a litte with this script.
Simon

1 Like

Ok all is working now, thanks to @Troon and @sheminasalam for the help!!

:slight_smile:

1 Like