Help please with to.state triggers

Hi Guys, hopefully someone can help me with a problem.
I want to trigger a notification that the alarm system is arming. At the beginning of the transition from “disarmed” to “armed_home”.

I think I can do it based on the trigger_to state. I know the alarm entity changes its “to” state on trigger, the actual entity state does not change until the arming_time has expired.

But attempts so far have failed.

Here is my current attempt,

 - alias: Notify alarm is arming 
    trigger: 
      - platform: state
        entity_id: alarm_control_panel.alarm
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.state == 'armed_home' }}
    action:
      - service: script.sonos_say
        data:
          sonos_entity: media_player.living_room_sonos 
          volume: 0.2
          message: 'Attention, the alarm is arming you have 30 seconds to leave'
          delay: '00:00:05'

Do you mean like this?

 - alias: Notify alarm is arming 
    trigger: 
      - platform: state
        entity_id: alarm_control_panel.alarm
        from: 'disarmed'
        to: 'armed_home'
    action:
      - service: script.sonos_say
        data:
          sonos_entity: media_player.living_room_sonos 
          volume: 0.2
          message: 'Attention, the alarm is arming you have 30 seconds to leave'
          delay: '00:00:05'

Not quite, as I understand the process, it goes from “Disarmed” to “Armed_Home” after a period set by an attribute “Arming_Delay”. In my case its 120 seconds.

So I want the announcement to occur at the time the to_state is changed at the beginning, what you have is triggered at the end of the arming delay when the entity actually does change to “Armed”.

What you’re trying to do isn’t achievable with the Trigger State Object (trigger.to_state, trigger.from_state). The Trigger State Object reports an entity’s state at the moment it triggered the automation. For example, a light is now on and and it was previously off. It’s not designed to tell you that the light is now off but will be on in 120 seconds. For that matter, neither are from and to for a State Trigger.

When you arm the physical security system, what is reported by alarm_control_panel.alarm at that precise moment? Does it report armed_home immediately or 120 seconds later?

If it reports armed_home immediately then it can assume that from the moment it happened there still remains a 120-second grace period (exit delay) until the physical alarm system is truly armed. This situation can be detected by the State trigger example I posted.

If it reports armed_home 120 seconds later, there’s nothing that can be done to warn the user in advance. By the time alarm_control_panel.alarm is aware of it, it’s a done deal.

Thanks Taras, I was basing my code on this article, https://www.home-assistant.io/docs/automation/templating/

It describes attributes that define the from.state and to.state which was what I was attempting to leverage.

Appreciate your input.

I don’t use any alarm_control_panel integration, but I just happened to be helping someone else with similar questions, so I had been looking at the code.

I think what you looking for is triggering on the state of arming.

EDIT, as in:

- trigger:
  - platform: state
    entity_id: alarm_control_panel.PANEL
    to: arming
  ...

Hi Phil,
My reading of the guide is that the entity state is never “Arming”.

*When the alarm is armed, its state first goes to **arming** for a number of seconds equal to the destination state’s **arming_time** , and then transitions to one of the “armed” states. Note that **code_template** never receives “arming” in the **to_state** variable; instead, **to_state** contains the state which the user has requested. However, **from_state** *can* contain “arming”.

But on re-reading that paragraph, I may have misunderstood and it does change to “Arming” I will go and do some testing.

Thanks Guys

Yeah, don’t confuse to_state in this description with trigger.to_state. Those are two different things. Per that description – “When the alarm is armed, its state first goes to arming for a number of seconds equal to the destination state’s arming_time” – and per the code I read, I’m pretty sure it changes to arming.

I will do some further testing and report back for the record. But the purpose of my trying to use the to_state, is “Arming” is common to all of the state changes e.g.

Disarmed > Arming > Armed_home
Disarmed > Arming > Armed_night
Disarmed > Arming > Armed_away

In this instance I only want the announcement if the state change is from Disarmed to Armed_home. Because I want to warn the person who triggered the alarm they have a period of time before the alarm is set.

Thanks again

Your tests should include determining what are the possible states prior to arming.

disarmed is clearly one possible previous state. If find there are no others then you can safely use a State Trigger with to: 'arming' and dispense with from. However, you won’t know the final state (armed_whatever) until it occurs. That’s what makes it challenging to achieve your goal.

Depending on your alarm_control_panel integration, you might be able to do this:

- trigger:
  - platform: state
    entity_id: alarm_control_panel.PANEL
    to: arming
  condition:
  - condition: template
    value_template: >
      {{ is_state_attr('alarm_control_panel.PANEL', 'next_state', 'armed_home') }}
  action:
  ...

EDIT: Actually, I think this might even work, too:

- trigger:
  - platform: state
    entity_id: alarm_control_panel.PANEL
    to: arming
  condition:
  - condition: template
    value_template: "{{ trigger.to_state.attributes.next_state == 'armed_home' }}"
  action:
  ...
1 Like

Thanks for your help, I will spend Saturday morning playing with it.

Interesting, I will incorporate into my testing, thanks Phil.

Hi Phil, the code below it worked superbly. Thanks to you and @123 Taras for your assistance. Now my sonos system announces that peeps have x seconds to vacate the building before the alarm is armed. armed_home is the only one in which there is a delay, hence the need to be specific.

- trigger:
  - platform: state
    entity_id: alarm_control_panel.PANEL
    to: arming
  condition:
  - condition: template
    value_template: "{{ trigger.to_state.attributes.next_state == 'armed_home' }}"
  action:

Thanks a lot, that exactly what I looked for!

That does not work for me. The only entity listed for the alarm is “alarm_control_panel.home_alarm”. When I use that as the entity in a trigger type “state” and use “arming” in the “to:” field, the automation is not triggered when I start the arming process even thought the alarm shows “Arming” for 15 seconds before progressing to either armed_away or armed_home. Everything works fine when using the “alarm_control_panel.home_alarm” as an entity in an automation to trigger a beep from my speaker when the “alarm_control_panel.home_alarm” state changes to “pending” as a result of the alarm being triggered while in the “armed-away” state.

Below is what I have in my configuration.yaml file regarding the alarm

  - platform: manual
    name: Home_Alarm
    code: !secret alarm_code
    arming_time: 15
    delay_time: 15

Below is the automation I am trying to use to warn me that a door is open. I receive the message if I execute the automation, so I know the code to notify me works; however, when I arm the system when the “entity_id: binary_sensor.backdoor_is_open” state is “on” (I verify this in developer tools) the automation is not triggered:

  - platform: state
    to: arming
    entity_id: alarm_control_panel.home_alarm
condition:
  - condition: state
    entity_id: binary_sensor.backdoor_is_open
    state: 'on'
action:
  - service: notify.mobile_app_pixel_3a_xl
    data:
      message: A door is open

I have even removed the condition and the automation still will not run when it should. As a result, the problem is in the trigger.

This is an automation where I use the stat “pending” to trigger a sound to remind the user that they need to disarm the alarm or it will go off (this automation works fine):

  - platform: state
    entity_id: alarm_control_panel.home_alarm
    to: pending
    from: armed_away
condition: []
action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.sonoff_alarm

I need this ability to send a message while the alarm is “arming” to warn the user that a door or window is open. Any help on this would be appreciated.

Hi Icotten
I am not sure if I am seeing all of your code, but hopefully showing my relevant code may help you.
Configuration.yaml

alarm_control_panel:
  - platform: manual
    name: '87'
    code: !secret alarm_code
    code_arm_required: false
    ################# Arm again after triggering
    disarm_after_trigger: false
    delay_time: 0
    arming_time: 0
    ################# 
    disarmed:
      trigger_time: 0
      delay_time: 0
  ################## Arm while peeps are at home manually
    armed_home:
      arming_time: 30
      delay_time: 90
      trigger_time: 60
    ################# Arm while peeps are away from home 
    armed_away:
      delay_time: 0
      trigger_time: 60

The major difference is in your code I don’t see the “alarm_control_panel:” at the beginning.

With regard to your automation it appears to be missing a ‘trigger:’ command.

So your code should look like:

trigger: # missing from code provided
  - platform: state
    to: arming # I think this should be the end state. 
    entity_id: alarm_control_panel.home_alarm
condition:
  - condition: state
    entity_id: binary_sensor.backdoor_is_open # I assume this is not the real name of your sensor
    state: 'on'
action:
  - service: notify.mobile_app_pixel_3a_xl
    data:
      message: A door is open

Something I struggled with is the state of the code template. I don’t think it ever goes to “arming”, only from “disarmed” to one of the armed states, hence the use of a snippet of code to be next state in my code:

condition:
  - condition: template
    value_template: "{{ trigger.to_state.attributes.next_state == 'armed_home' }}"

So you could make it:

trigger: # missing from code provided
  - platform: state
    entity_id: alarm_control_panel.home_alarm
condition:
  - condition: template
    value_template: "{{ trigger.to_state.attributes.next_state == 'armed_home' }}"
  - condition: state
    entity_id: binary_sensor.backdoor_is_open # I assume this is not the real name of your sensor
    state: 'on'
action:
  - service: notify.mobile_app_pixel_3a_xl
    data:
      message: A door is open

Hope this helps.

1 Like

Thanks. I tried your code with and without a second condition just to make sure that the second condition was not the issue and it did not run. Can you have a trigger where the platform is “state” without a “to:” value?

By the way, the alarm works fine otherwise.

Hi,
Yes you can have a trigger without defining the state. Any change in state will then trigger the automation. You can then define the conditions under which the automation proceeds. Check out the HA guide https://www.home-assistant.io/docs/automation/trigger/

Have you tried it without any conditions? if it executes without conditions then its likely the conditions are a problem. If it will not trigger, even without conditions then the entity or the action is the problem. I don’t know the service you have, I assume its a script, does this work if executed in the developer tools? If yes check your entities.
Once you have it working without conditions, re-introduce the conditions. I recommend using the developer tools to check you have correct states, log and logbook can provide useful information for problem resolution.

1 Like

Thanks for the tip that you can have a trigger without defining a “to:” condition.

This works now.

description: ''
trigger:
  - platform: state
    entity_id: alarm_control_panel.home_alarm
condition:
  - condition: state
    entity_id: alarm_control_panel.home_alarm
    state: arming
  - condition: state
    entity_id: binary_sensor.backdoor_is_open
    state: 'on'
action:
  - service: notify.mobile_app_pixel_3a_xl
    data:
      message: A door is open
mode: single