Automation that triggers on attribute change

Hi. I’m new to HA and very new to automtation, but I’ve created this simple automation that I think should work, but somehow does not. Anyone that could have a quick look at it and see if there is something I’m missing?

alias: Freya Alarm på
  trigger:
  - platform: state
    entity_id:
    - sensor.freya_uke
    attribute: day
    to: '0'
  action:
  - service: sonos.update_alarm
    data:
      entity_id: media_player.freya
      alarm_id: 18

Check to see if the output of the attribute is a string or number. While states are always strings, attributes can be other data types like integers, floats, lists, and dictionaries. State triggers are very literal. Because of this there is a difference between ‘0’ and 0, and strings will not cause a trigger when a number is what’s expected.

Hi Drew. Thanks for replying. It seems you might be correct. I removed the to: completely and it worked.
I’ve no changed the ‘0’ to a 0, and will find out on the next change if that works.

The ‘0’ was created by the visual editor, and if I change it to 0 in the ymal code the visual editor reports an error:

So I guess there are som imperfections in the visual editor.

If you can’t get it to work as a State trigger you should be able to do it as a Teplate trigger.

alias: Freya Alarm på
  trigger:
  - platform: template
    value_template: "{{ is_state_attr('sensor.freya_uke', 'day', 0) }}"
  action:
  - service: sonos.update_alarm
    data:
      entity_id: media_player.freya
      alarm_id: 18

Ok. I’ve never used Template trigger before, but I’ll look into that.

I can now happily report that it’s working. I looked into Templet triggers, but ended up using numeric_state trigger.It ended up looking like this:

alias: Freya Alarm på
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.freya_uke
    attribute: days
    below: 14
    above: 12
action:
  - service: sonos.update_alarm
    data:
      entity_id: media_player.freya
      alarm_id: 18
      enabled: true
  - service: notify.notificationgroup
    data:
      message: Frey kommer! Aktiverer alarm
mode: queued