Help with alarm automation

Hi all,
I’ve configured a manual alarm entity and, following the official doc, I’ve tried to trigger the alarm opening a contact sensor, but it doesn’t work.
The alarm state:

alarm_control_panel.home_alarm
state: armed_away

The sensor:

binary_sensor.0x00158d00047e76e4_contact
state: off
battery: 100
contact: true
linkquality: 147
temperature: 23
voltage: 3005
friendly_name: Studio Finestra contact
device_class: door

Configuring the automation via GUI, it creates:

- id: '1618324704445'
  alias: Allarme - Trigger via sensori su armed_away
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d00047e76e4_contact
    attribute: contact
    to: 'false'
  condition:
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: armed_away
  action:
  - service: alarm_control_panel.alarm_trigger
    data: {}
    target:
      entity_id: alarm_control_panel.home_alarm
  mode: single

Opening and closing the door, it changes state to “on” and “off” and attribute contact from “true” to “false”, but nothing gets triggered.

Ideas why?

Thanks!

Simon

Instead of using the state attribute contact did you try using just the state from on to off.

Uh, so it works… just so easy? Good!
Why it’s not working on the attribute?

maybe its because the state change could be a boolean and not string. To check boolean you dont need the inverted comas.

  - platform: state
    entity_id: binary_sensor.0x00158d00047e76e4_contact
    attribute: contact
    to: false

Not sure if this would work but you can check it out.

Ok, I’ll give it a try, but having this already functional, that is already enough.
I’ve another question, if you can help me. In case I’ll open a new thread :slight_smile:
Following the guide, I’ve tested correctly the notification via telegram and it works well sending a message if alarm is triggered. Until here ok, but I would like to add into the message which sensor was triggered. How could I do that? I’ve many sensors, so an example with two sensors would help me to extend it. Then, the same way I’ll extend then the message to send me one when I activate the alarm to notify me if a door is still open, but the logic is exactly the same.
Any help?
Thanks!
simon

Always happy to help. You can try something like this.

- alias: Door and Window Monitor
  mode: queued
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.alarm_14_office_window
        - binary_sensor.another_window
        - binary_sensor.some_other_window
        - binary_sensor.front_door
        - binary_sensor.another_door
  action:
    - service: notify.telegram_carlton
      data:
        title: Alert
        message: The {{ trigger.to_state.name }} was {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}

I hope you will get the idea from this configuration. This is a step above what you have asked. I shared it because it might be good for you to have some more info. If you want any help do ask.

{{ trigger.to_state.name }} gives name of the binary sensor triggered.
trigger.to_state.state gives the state of the triggered binary sensor.

In your case if you are looking for getting the notification of just the on state, you can change the trigger accordingly and also the payload.

Thanks @sheminasalam very appreciated!
btw in this example (I’ve not tested) the trigger has no state change to “on”, is that correct? How would the trigger be executed?
Then, how to trigger the execution only if the alarm_control_panel.home_alarm is in triggered state? If I would add the triggered state as condition, I fear it would not work, because the triggered state becomes after the pending period defined by the delay_time, or not?
Thanks, Simon

There is no state change trigger because I wanted the automation to be triggered with any state change. So from and to are kept empty and in such configuration, the automation is triggered when there is any change of state. But in your case you can add multiple triggers like the below to trigger the automation with each sensor.

  trigger:
  - platform: state
    entity_id: binary_sensor.sensor1
    to: 'off'
  - platform: state
    entity_id: binary_sensor.sensor2
    to: 'off'

As far as I can see the condition should work. If there is a delay as you have said then we can add the condition and the delay in the actions of the automation like this.

action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: armed_away

Uhm @sheminasalam that’s not working :frowning:

This is the automation created:

- id: '1618330144790'
  alias: Allarme - Notifica in caso di trigger avanzata
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d00047e76e4_contact
  condition:
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: triggered
  action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 21
      milliseconds: 0
  - service: notify.xefilbot
    data:
      title: Allarme
      message: The {{ trigger.to_state.name }} was {{ 'opened' if trigger.to_state.state
        == 'on' else 'closed' }}
  mode: single

And this is the alarm definition:

alarm_control_panel:
  - platform: manual
    name: Home Alarm
    code: 1234
    arming_time: 30
    delay_time: 20
    trigger_time: 4
    disarmed:
      trigger_time: 0
    armed_home:
      arming_time: 0
      delay_time: 0

As soon I open the window in armed_away, the alarm switches to pending for 20 seconds and then to triggered for 4 seconds. In this 4 seconds it executed the notification, but not the above, but this one_

- id: '1618326225210'
  alias: Allarme - Notifica in caso di trigger
  description: ''
  trigger:
  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: triggered
  condition: []
  action:
  - service: notify.xefilbot
    data:
      title: Allarme
      message: Allarme triggerato
  mode: single

So only this last notification is executed, not the previous one suggested by you. The last one but doesn’t contain the contact sensor name and status of course, because not involved.

Ideas?

Please try this code.

- id: '1618330144790'
  alias: Allarme - Notifica in caso di trigger avanzata
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d00047e76e4_contact
  condition: []
  action:
  - wait_for_trigger:
      - platform: state
        entity_id: alarm_control_panel.home_alarm
        to: triggered
    continue_on_timeout: false
    timeout: '00:00:25'
  - service: notify.xefilbot
    data:
      title: Allarme
      message: The {{ trigger.to_state.name }} was {{ 'opened' if trigger.to_state.state
        == 'on' else 'closed' }}
  mode: single

I’ll give it later a try. Is it possible to define it via gui? I’ll check it.
Thanks a lot! I’ll let you know!
Simon

You can copy the code as yaml and go to configurations>automations>Allarme - Notifica in caso di trigger avanzata to see the code in GUI. Happy to help.

Hello @sheminasalam ,
All worked perfectly. Seems It works even if the following happens, which is what desired:
1 alarm in armed_away
2 the door gets opened
3 after 20 seconds, a notification is sent
Result: OK!

1 alarm in armed_away
2 the door gets opened
3 before the 20 seconds, I enter the PIN code to disable the alarm. The notification is NOT sent
Result: OK - PERFECT!

Now, I know I’m moving a step forward on the main quesion, but just in case, I can open a new thread as well.
The next desired step is: let me think I arm the alarm. During this action I would like to get notified immediatly if there are door sensors open to remember me to close everything. I would put all this into a script, so that I coult execute it from the GUI if needed, and executed as well during the arming mode. The problem is, I’ve no idea how to send into a message the status of different sensors based on their state. I know it’s not far away from what above, I think.

Ideas?

Many thanks!!

Simon

First thing you have to do is to group all such sensor into one group by copying this to config.

group:
  alarm_sensors:
    name: Alarm Sensors
    entities:
      - binary_sensor.door1
      - binary_sensor.door2
      - binary_sensor.window1
      - binary_sensor.window2

Then you need to create a script like this.

alias: Alarm Sensor Open Alert
sequence:
  - condition: state
    entity_id: group.alarm_sensors
    state: 'on'
  - service: notify.xefilbot
    data:
      title: Doors/Windows Open
      message: >-
        The following doors/windows are open {% if
        states.binary_sensor.door1.state == 'on'%}Door1, {% endif %}{% if
        states.binary_sensor.door2.state == 'on'%}Door2, {% endif %}{% if
        states.binary_sensor.window1.state == 'on'%}Window1, {% endif %}{% if
        states.binary_sensor.window2.state == 'on'%}Window2, {% endif %}
mode: single

Now you can add a dashboard entity to call this script to manually operate it.

Then with automating this with alarm arming.

alias: Alarm Sensor Check
description: ''
mode: single
trigger:
  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: armed_away
  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: armed_home
condition: []
action:
  - service: script.alarm_sensor_open_alert

Please make sure the entity_ids are changed

Hello @sheminasalam !!
Thanks a lot for your help, really!
BTW The message of the first script has something wrong. I paste it here as insert into scripts:

allarme_verifica_portefinestre:
  alias: Allarme Verifica stato sensori Porte e Finestre
  sequence:
  - condition: state
    entity_id: group.all_door_entities
    state: 'on'
  - service: notify.xefilbot
    data:
      title: '*Avviso Porte/Finestre Aperte*'
      message: >-
        The following doors/windows are open {% if
        states.binary_sensor.0x00158d00047e76e4_contact.state == 'on'%}Finestra Studio, {% endif %}{% if
        states.binary_sensor.0x00158d000477376f_contact.state == 'on'%}Porta Finestra Studio, {% endif %}

If In the message line i insert only a word, it appears in the HA GUI, otherwise with the following it dosn’t appeas at all. Ideas why? Something there is wrong formatted.

Simon

There are syntax issues in the script you provided, please try this, just copy paste and see

alias: Allarme Verifica stato sensori Porte e Finestre
sequence:
  - condition: state
    entity_id: group.all_door_entities
    state: 'on'
  - service: notify.xefilbot
    data:
      title: '*Avviso Porte/Finestre Aperte*'
      message: >-
        The following doors/windows are open {% if
        is_state('binary_sensor.0x00158d00047e76e4_contact', 'on') %}Finestra
        Studio, {% endif %}{% if
        is_state('binary_sensor.0x00158d000477376f_contact', 'on') %}Porta
        Finestra Studio, {% endif %}
mode: single

Good morning @sheminasalam !
Ok, I’ve tried yours and it works, very well!
Last step could be to notify that there are no sensors open in case they are all closed, what is the best way? Well, I would give it a try, using the (never used, I admit) Template Developer tool.
I’ll give it a try by myself first, hope I’ll able :wink:
Simon

Actually you dont need to use any templates for that just use the choose type action like below.

alias: Allarme Verifica stato sensori Porte e Finestre
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: group.all_door_entities
            state: 'on'
        sequence:
          - service: notify.xefilbot
            data:
              title: '*Avviso Porte/Finestre Aperte*'
              message: >-
                The following doors/windows are open {% if
                is_state('binary_sensor.0x00158d00047e76e4_contact', 'on')
                %}Finestra Studio, {% endif %}{% if
                is_state('binary_sensor.0x00158d000477376f_contact', 'on')
                %}Porta Finestra Studio, {% endif %}
      - conditions:
          - condition: state
            entity_id: group.all_door_entities
            state: 'off'
        sequence:
          - service: notify.xefilbot
            data:
              title: '*Avviso Porte/Finestre Aperte*'
              message: All the windows and doors are closed
    default: []
mode: single

:innocent:

Oh great @sheminasalam ! I was trying to use (with a little success, yeah) some variables into the template, but yours is more clean. BTW A question. In your example, the output goes on a single line. If I try to put all in a list, it has empty spaces where the contact is closed. How avoid? The script is working btw.
Here what I mean:

Template example:

        Following doors are open:
        {% if is_state('binary_sensor.0x00158d00047e76e4_contact', 'on') %}- Studio Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d000477376f_contact', 'on') %}- Studio Porta Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d00047d6600_contact', 'on') %}- Porta ingresso{% endif %}
        {% if is_state('binary_sensor.0x00158d00047d65df_contact', 'on') %}- Soggiorno Porta Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d0004a0257c_contact', 'on') %}- Cucina Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d00047d65fc_contact', 'on') %}- Bagno Doccia Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d0004521ef0_contact', 'on') %}- Bagno Vasca Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d00049fd978_contact', 'on') %}- Camera Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d00047e9d51_contact', 'on') %}- Camera Porta Finestra{% endif %}
        {% if is_state('binary_sensor.0x00158d00047ebb8e_contact', 'on') %}- Stanza di Giulia Porta Finestra{% endif %}

Output, considering two sensors open

Following doors are open:
        
        
        
        
        
        - Bagno Doccia Finestra
        - Bagno Vasca Finestra

Expected:

Following doors are open:
        - Bagno Doccia Finestra
        - Bagno Vasca Finestra

:slight_smile: how to format?

In addition, trying the whole script, it is not editable in the GUI and the output of the open sensors gets empty. Something is not executed :frowning:

allarme_verifica_portefinestre:
  alias: Allarme Verifica stato sensori Porte e Finestre
    sequence:
      - choose:
        - conditions:
          - condition: state
            entity_id: group.all_door_entities
            state: 'on'
        sequence:
          - service: notify.xefilbot
            data:
              title: '*Avviso Porte/Finestre Aperte*'
              message: >-
                Le seguenti porta/finestre sono aperte:
                {% if is_state('binary_sensor.0x00158d00047e76e4_contact', 'on') %}- Studio Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d000477376f_contact', 'on') %}- Studio Porta Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d00047d6600_contact', 'on') %}- Porta ingresso{% endif %}
                {% if is_state('binary_sensor.0x00158d00047d65df_contact', 'on') %}- Soggiorno Porta Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d0004a0257c_contact', 'on') %}- Cucina Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d00047d65fc_contact', 'on') %}- Bagno Doccia Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d0004521ef0_contact', 'on') %}- Bagno Vasca Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d00049fd978_contact', 'on') %}- Camera Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d00047e9d51_contact', 'on') %}- Camera Porta Finestra{% endif %}
                {% if is_state('binary_sensor.0x00158d00047ebb8e_contact', 'on') %}- Stanza Porta Finestra{% endif %}
        - conditions:
          - condition: state
            entity_id: group.all_door_entities
            state: 'off'
        sequence:
          - service: notify.xefilbot
            data:
              title: '*Avviso Porte/Finestre Aperte*'
              message: Tutte le Porte/Finestre sono chiuse
    default: []
mode: single

And last but not least, where to buy you a beer/the/coffee?? :wink:

Simon

Actually if you looking to list the entities we need to take a different approach with templates. But there is a problem with the entity_ids. For some reason the template is not accepting ) as the first digit of entity_ids like binary_sensor.0x00158d000477376f_contact but only when we remove the 0 from the entity_id like this binary_sensor.x00158d000477376f_contact the template works. Please try if this is the same for you also. In the below code I have removed 0 for every entity_id. If this is the same for you please remove the first zero from the entity_ids and try.

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

No need my friend :innocent: :innocent:,Happy that you offered…I like solving such things and learning something new so I am happy to help.