Add variable for trigger attribute

when triggering on an attribute there is no variable for that attribute we can use in the rest of the automation.

we need to reference the triggering attribute only spelling it out the variable verbosely:

  - id: sync_verwarming_voorkamer_preset
    trigger:
      platform: state
      entity_id: climate.voorkamer
      attribute: preset_mode
    condition:
      >
        {{trigger.to_state.attributes.preset_mode !=
          states('input_select.voorkamer_preset','option')|slugify}}

It would be very nice if we could use trigger.attribute or some version of that?

  - id: sync_verwarming_voorkamer_preset
    trigger:
      platform: state
      entity_id: climate.voorkamer
      attribute: preset_mode
    condition:
      >
        {{trigger.attribute !=
          states('input_select.voorkamer_preset','option')|slugify}}

since that attribute would be the unique trigger for the automation, using trigger.attribute would also be unique, and couldnt be confused with any other attribute of the state object.

currently the trigger.to_state part of that refers to the trigger (obviously), but then we need to spell out the attribute.

this FR would solve that mixed referencing syntax, and make it a lot easier to copy the automation body for different attributes elsewhere

to_state and from_state contain the entity’s complete State Object before and after the state-change, respectively.

According to your proposal, trigger.attribute would contain the monitored attribute’s new value. In other words, it duplicates trigger.to_state.attributes.foo but only if the State Trigger is configured to monitor an attribute named foo.

trigger.attribute seems like it has a very narrow use-case and is more “mixed syntax” than the status quo.

If the goal is to simplify the variable’s name, then I suggest that trigger.value could be used to represent the new value regardless if the State Trigger is configured to monitor the entity’s state or one of its attributes. In other words, trigger.value is the same as trigger.to_state.attributes.foo or trigger.to_state.state, depending on what the State Trigger is configured to monitor.

1 Like

sure, that would be even better, great proposal.

must confess I havent tested that, so maybe it already does, let me try

nope, it doesnt:

Template variable warning: 'dict object' has no attribute 'value' when rendering '{{trigger.value}}'

@petro suggested the following ‘band-aid’

trigger:
- id: x
...
variables:
  triggers:
    x: attribute1
  attribute: >
    {{ triggers.get(trigger.id) }}

translated to my usecase:

  - id: sync_verwarming_voorkamer_preset_test
    trigger:
      platform: state
      entity_id: climate.voorkamer
      attribute: preset_mode
      variables:
        attribute: >
          {{trigger.to_state.attributes.preset_mode}}
    action:
      service: notify.mobile_app_marijn
      data:
        message: >
          {{attribute}}

which illustrates what I am after, and allows the variable to be used throughout the automation for the time being…