What's the difference between these yaml anchors

I agree with @petro that this should be one automation, but you can use anchors to do this. Untested, but something like this (if not exactly)…

- alias: Send notification when alarm is Disarmed
  trigger:
    platform: state
    entity_id: alarm_control_panel.ha_rpi4_alarm
    to: disarmed
  <<: &notify_alarm 
    condition:
      condition: state
      entity_id: input_boolean.notify_alarm
      state: 'on'
    action:
      service: notify.notify
      data:
        title: >
          ALARM: {{trigger.to_state.state))
        message: "{{as_timestamp(now())|timestamp_custom('%X')}}: The alarm is {{trigger.to_state.state}}" 

- alias: Send notification when alarm is in arming status
  trigger:
    platform: state
    entity_id: alarm_control_panel.ha_rpi4_alarm
    to: arming
  <<: *notify_alarm
2 Likes

If that works, TIL

Edited it slightly, but I’m pretty sure that will do it.

@Mariusthvdb - Just tested it in a yaml to json converter, and then just to be sure took the resulting json and put that in a json to yaml converter and I’m happy that the code in my post 3 up from this one is correct.

yes, thanks!
so this means that depending on the indentation you can simply anchor anything. great. cut yet again a few lines, and make the editing so much less error prone. Thanks Marc.

    <<: &notify_alarm
    condition:
      condition: state
      entity_id: input_boolean.notify_alarm
      state: 'on'
    action:
      service: notify.notify
      data:
        title: >
          {% set mode = trigger.to_state.state|replace('_',' ')|capitalize %}
          ALARM: {{mode}}
        message: >
          {% set mode = trigger.to_state.state|replace('_',' ')|capitalize %}
          {{as_timestamp(now())|timestamp_custom('%X')}}: The alarm is in '{{mode}}' mode

could we use the new variables here? for the mode?

like:

<<: &notify_alarm
condition:
  condition: state
  entity_id: input_boolean.notify_alarm
  state: 'on'
action:
  variables:
    mode: >
      trigger.to_state.state|replace('_',' ')|capitalize
     
  service: notify.notify
  data:
    title: >
      ALARM: {{variables.mode}}
    message: >
      {{as_timestamp(now())|timestamp_custom('%X')}}: The alarm is in '{{variables.mode}}' mode
1 Like

Not quite anything, but yeah indentation is the key to it.

As for the variables I don’t know, haven’t had chance to play with them yet.

ha, cool, will do some testing. reading this https://www.home-assistant.io/blog/2020/09/17/release-115/#variables I shouldn’t use the keyword variables in the final template I suppose, and just use the {{mode}}.

Which would be nice, and as it is now, only taking declaring the variable outside the 2, title and message.

edit

forget what I posted above, it had the wrong indentation below the anchor definition.

this works (config checker says ‘configuration valid’)

  - alias: Send notification when alarm is Disarmed
    trigger:
      platform: state
      entity_id: alarm_control_panel.ha_rpi4_alarm
      to: disarmed
    <<: &notify_alarm
      condition:
        condition: state
        entity_id: input_boolean.notify_alarm
        state: 'on'
      action:
        - variables:
            mode: >
              {{trigger.to_state.state|replace('_',' ')|capitalize}}
        - service: notify.notify
          data:
            title: >
              ALARM: {{mode}}
            message: >
              {{as_timestamp(now())|timestamp_custom('%X')}}: The alarm is in '{{mode}}' mode

apparently, setting a variable is seen as a separate action in the action block, hence needs a dash.
can confirm this works nicely!

1 Like