Automation not running on state attribute change - what am I doing wrong?

Hi,

I am trying to alert on a state attribute change from an Envisalink alarm. The entity is sensor.home_alarm_keypad and here are the attributes:

alarm: false
alarm_in_memory: false
armed_away: false
ac_present: true
armed_bypass: false
chime: false
armed_zero_entry_delay: false
alarm_fire_zone: false
trouble: false
bat_trouble: false
ready: true
fire: false
armed_stay: false
alpha: '****DISARMED****  Ready to Arm  '
beep: 'off'
exit_delay: false
entry_delay: false
last_disarmed_by_user: ''
last_armed_by_user: ''
armed: false
friendly_name: Home Alarm Keypad
icon: mdi:alarm

My automation is as follows:

trigger:
  - platform: state
    entity_id: sensor.home_alarm_keypad
    attribute: ready
    for:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
    to: 'true'
condition: []
action:
  - service: notify.pushover
    data:
      title: Alarm Testing
      message: Testing
      data:
        sound: tugboat
mode: single

Obviously this should alert when the alarm “ready” state switches to “true”. However, this automation never runs. No matter what I do, it never seems to alert and so clearly I am doing something wrong. I am at a loss as dev tools clearly show the attributes two states as “true” and “false”.

What am I missing?

TIA!

Did you remove the automation’s first line (containing alias and/or id) or is that exactly how you have it in Home Assistant?

I created the automation using the gui and this is the output from “edit in yaml” from the gui editor. I believe that it hides the id. I can pull the complete data from automations.yaml if that would be helpful.

That’s interesting because, on my system, this is how an automation appears in the Automation Editor when in YAML mode.

Not sure why there’s a difference but let’s assume that it isn’t responsible for the issue.

Maybe dumb question, but are you positive the automation never ran, i.e. is its “last triggered” set to “Never”?

My apologies, you are right. I missed copying the first two lines. Here you go:

alias: Alarm - State Testing
description: ''
trigger:
  - platform: state
    entity_id: sensor.home_alarm_keypad
    attribute: ready
    for:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
    from: 'false'
    to: 'true'
condition: []
action:
  - service: notify.pushover
    data:
      title: Alarm Testing
      message: Testing
      data:
        sound: tugboat
mode: single

As an experiment, remove the lines related to for and to. Repeat your test. If it triggers and you receive a notification then we know the issue is caused by how Home Assistant is interpreting true. It’s possible that there’s some confusion in whether it’s being handled as a string or boolean.

That was it! You nailed it. I removed the time limit and from “true” to “false” and voila. Thank you. The complete yaml is below.

One question, the code below alerts on when the attribute as true. How do I invert this to only alert if the attribute is false?

alias: Alarm - State Testing
description: ''
trigger:
  - platform: state
    entity_id: sensor.home_alarm_keypad
    attribute: ready
condition: []
action:
  - service: notify.pushover
    data:
      title: Alarm Testing
      message: Testing
      data:
        sound: tugboat
mode: single

No dumb question. I know about the automation never runs because I saw the last run time as multiple days ago and, I know that a trigger condition happened today.

You might try your original automation with

    to: 'True'

with a capital.

I think python could convert a “json” true/false to a pythonish “True/False”.

‘True’ does not work. Weird.

Actually, without a to or from, it will trigger for any change in the attribute’s value.

The purpose of the experiment was simply to confirm that the value true is being misinterpreted.

Put this back in and see if it still works. Note that there are no quotes wrapping true. If the Automation Editor automatically adds them then the experiment will prove nothing. You may need to edit it in YAML mode to ensure true has no quotes.

    to: true
    for: '00:00:10'

The theory here is that the attribute’s value is a boolean true but the trigger’s to: 'true' is looking for a string true. We want the trigger to look for a boolean true.

1 Like

Yes! That worked. I think that the problem is that the GUI wants to wrap the true in quotes and so it converts it to text and so is always false. Manually editing in yaml fixes this, but I think that it will be broken again if I open it in the GUI.

Thank you!

2 Likes

You should know that the Automation Editor is like training wheels on a bicycle. It’s helpful initially but, after your skill level increases, it tends to get in your way.

The Automation Editor has several quirks and one of the more irksome is its insistence to format YAML according to its own inflexible set of rules. Wait until you see what it does to complex, multiline templates. Consider it an easy introduction to composing automations but merely a stepping stone to using a text editor that permits far more flexibility.

Thank you again. Out of curiosity, what tool do you use to edit automations? Obvious, I have HA file editor, but I am wondering if you use a 3rd party option like VSCode.

TIA!

I use VS Code.