Attribute access

Is there any difference to the three triggers below, with how the elevation is accessed? As far as I can tell, all 3 are trying to access the elevation attribute, why is there three different ways to do this? Is there an advantage that any of them? Obviously the top one is the cleanest and more clear, but I don’t see other people access attributes like this in hardly any examples, is there a reason for that? And between the later two, is there any difference?

The way the UI accesses the elevation attribute (what ends up in the YAML if I access the elevation attribute in the UI for creating an automation):

  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: -4.0

From the HA documentation:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: -4.0

From this blog post of the person that apparently added this elevation functionality:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: -4.0

Triggers have changed and improved over time. Originally there was no way to access an attribute in an entity for a trigger. So templates were used. Recently this functionality was added.

One of them has a disadvantage (see below).

It’s relatively new. Introduced in v0.115.

Yes.

This won’t generate errors if the entity is not ready (e.g. after a restart):

"{{ state_attr('sun.sun', 'elevation') }}"

This will cause an error.

"{{ state.attributes.elevation }}"

See the warning here: https://www.home-assistant.io/docs/configuration/templating/#states

1 Like

Great, thanks for that info @tom_l! Very helpful. One follow-up, will the first method cause an error if the entity is not ready?

attribute: elevation

Good question. I don’t know for sure but I doubt it.