Trigger ID: Last Triggered

Hello!

Is there a way to check the last time an automation was triggered but by a specific trigger ID? Meaning not only when an automation was last triggered but also what trigger (based on trigger ID) triggered that automation.

Thank you all!

From what I understand, and I might be wrong, details within the trigger when it is fired - are not saved in history. However, since I had the issue with my motion detector turning a light back on as soon as someone turned it off and then moved to leave the room, I started saving the last time the light was turned off MANUALLY, and then checked that within my logic that is fired often when motion is sensed (in other words, not to turn a light back on if it was manually turned off within the last 5 minutes). Here is the code I used - so something in the “trigger.” values might be of use to you in your research - it would be onerous - but you could actually store the appropriate “trigger.” value - to then check it later when needed in your other logic?

(FYI I was using an inpiut_text instead of an input_datetime due to a bug with input_datetime which I think has since been fixed).

alias: >-
  Dining Room Chandelier Turned Off -> If Done Manually -> Set Last Manual Off
  Timestamp
description: >-
  If the dining room chandelier is turned off (manually),  then make sure the
  "last_manual_off-dining_room" timestamp is updated
trigger:
  - platform: state
    entity_id:
      - light.dining_room_chandelier
    to: "off"
condition:
  - "{{ trigger.to_state.context.id != none }}"
  - "{{ trigger.to_state.context.parent_id == none }}"
  - "{{ trigger.to_state.context.user_id == none }}"
action:
  - service: input_text.set_value
    data:
      value: "{{ as_timestamp(now()) }}"
    target:
      entity_id: input_text.last_manual_off_dining_room
mode: parallel
max: 1000
1 Like

Reviving this thread as I have the exact same use case as @TheLaw. Hoping this can be done via template vs saving things manually?

There’s nothing stored against the automation itself, so you’d have to do it “manually” which isn’t that hard. If you specify an ID for each trigger:

action:
  - service: input_text.set_value
    target:
      entity_id: input_text.my_automation_last_trigger_id
    data:
      value: "{{ trigger.id }}"

As it’s a rarely-needed feature, it wouldn’t make sense to bulk up storage requirements to store it by default.

Not sure what this would achieve that Automation Traces doesn’t already give you, though?

Also note that the last_triggered attribute of an automation is a misnomer: it is updated only if the action block is reached. A trigger that is blocked by a condition will not drive an update to that timestamp.

2 Likes