Can you reference the trigger.id within a value_template?

For clarity, I would like to write automation triggers like the following to check for sensors that haven’t updated in the last X number of minutes. To save rewriting the sensor name, I would like to use the ‘id’ to reference it both in the trigger and again in the action section to send a message specifying which sensor is triggering.

id: "1711601431740"
  alias: No Recent Sensor Update
  description: ""
  trigger:
     - platform: template
       id: sensor.ambientweather_f007th_1_15_temperature
       value_template: "{{ ((as_timestamp(now()) - as_timestamp(states('trigger.id').last_updated)) / 60) > 30 }}"
   ...
   ...
    - platform: template
      id: sensor.ambientweather_f007th_2_17_temperature
      value_template: "{{ (as_timestamp(now()) - as_timestamp(states('trigger.id').last_updated)) / 60) > 60 }}"
...
...
  condition: []
  action:
   - service: notify.user_email
        metadata: {}
        data:
          title: Sensor Offline/Unavailable
          message: "{{ trigger.id }}"
   - delay: 02:00:00
  mode: single

However, the above code fails.
So is there a way that I can reference the trigger.id within the trigger as well as the usual usage of referencing it later in the action section?

No, that’s not a thing. The variable trigger doesn’t have a value until the automation is triggered… but the automation will never be triggered, because trigger doesn’t have a value.

1 Like

Thanks.
Is there any simple way to set the sensor name to a variable within each platform stanza so I only have to reference it once?

Just use State triggers with a duration… a State trigger without a defined to, from, or attribute will restart the duration “countdown” at the same time that the last_updated attribute is set.

Also, don’t use a 2 hour delay, instead use a condition to throttle the notifications.

- id: "1711601431740"
  alias: No Recent Sensor Update
  description: ""
  trigger:
    - platform: state
      entity_id: sensor.ambientweather_f007th_1_15_temperature
      for: "00:30:00"
    - platform: state
      entity_id: sensor.ambientweather_f007th_2_17_temperature
      for: "01:00:00"
  condition:
    - condition: template
      value_template: "{{ this.attributes.last_triggered | default(as_datetime(0), 1) < now() - timedelta(hours=2) }}"
  action:
    - service: notify.user_email
      metadata: {}
      data:
        title: Sensor Offline/Unavailable
        message: "{{ trigger.entity_id }}"
  mode: single

Much like I advised the OP first time around :smiley:

1 Like

Indeed, but the method posted by @Didgeridrew as well as the one you posted in the other thread will trigger if the state (e.g., temperature) hasn’t changed even if it has been updated.

I want to send a message only if last_updated hasn’t changed which is why I went down the trigger.id path that you suggested.

But that method requires me to enter the sensor id multiple times (first as the value of id: and then again in the value_template) which can lead to transcription errorrs.

That is why I was hoping I could treat the id: assignment as a variable that I could then reference in the value_template itself since presumably it is stored somewhere even if it is not (yet) triggered.

Alternatively, I was hoping there was a way I could set the sensor id to a variable that I would then reference multiple times as needed (i.e, to set id: and again in the value_template)

Alternatively is there a way I can create an array of sensor id’s that the value_template would loop through and then set the trigger_id to the one(s) that triggered.

You are misunderstanding what the last_updated property actually represents.

Please help me understand. I thought last_updated means that the state has been updated even if not changed. So if MQTT, sends an update every 60 seconds, last_updated will show the last time an update was sent even if the state value didn’t change.

Does it mean something else?

image

Ahh I see.
I guess I want state.last_reported – thanks for clarifying.

So the point remains can I do this mentioning the sensor id name only once either by referencing the id: or by setting a variable.

No… it’s the same circular logic issue, you can’t use a variable without a value to derive a value in an expression. You will have to put the entity ID in both the id field and the template’s expression.

You can use an array and get the triggering entity, but it’s a 1-shot per trigger… like other triggers it only fires when the rendered value goes from “false” to “true”, so the first entity whose last_reported exceeds the limit will trigger it but any after that will not until the value returns below the threshold.

And if you wanted to set up multiple threshold triggers for different durations, you could move the list of entity IDs into a trigger variable and reference that in each template trigger.

- id: "1711601431740"
  alias: No Recent Sensor Update
  description: ""
  trigger_variables:
    ent_list:
      - sensor.ambientweather_f007th_1_15_temperature
      - sensor.ambientweather_f007th_2_17_temperature
  trigger:
    - alias: "Trigger on first down for 30 min"
      platform: template
      value_template: |
        {% set exp_list = expand(ent_list) %}
        {{ exp_list | selectattr('last_reported', 'lt', now()-timedelta(hours=0.5)) | list | count > 0 }}
    - alias: "Trigger on first down for 1 hour"
      platform: template
      value_template: |
        {% set exp_list = expand(ent_list) %}
        {{ exp_list | selectattr('last_reported', 'lt', now()-timedelta(hours=1)) | list | count > 0 }}
  condition:
    - condition: template
      value_template: "{{ this.attributes.last_triggered | default(as_datetime(0), 1) < now() - timedelta(hours=2) }}"
  action:
    - service: notify.user_email
      metadata: {}
      data:
        title: Sensor Offline/Unavailable
        message: "{{ trigger.entity_id }}"
  mode: single