I just found out that while inside the automation A1, the value states.automation.A1.attributes.last_triggered is the time the automation just now was triggered and not when it was triggered the previous time. In other words: the attribute is set when the automation is started and not when it is finished.
Was there a change or was it always like that?
How can I find out inside an automation when it was triggered before?
Always like that, but it happens when the actions start.
This info is available in the condition. As soon as the action starts, the triggered get’s populated.
For the most part, you shouldn’t even need to do this math with last triggered anymore. What is your end goal here.
I have sensors triggering automations and I want to calculate the time between the trigger events.
for what purpose though? Just because you want to see or is there another reason…
One sensor is a rain sensor and it always triggers after each 8ml of rain. And I want to calculate ml/hour.
what does the state of this sensor have when it triggers?
the sensor goes on-off-on-off-on-off
You could probably do this with a template sensor:
sensor:
- platform: template
sensors:
last_off:
value_template: >
{% if is_states('sensor.xxx', 'off') %}
{{ now().timestamp() }}
{% else %}
{{ states('sensor.time_delta') }}
{% endif %}
Then make a template sensor for the calculation:
sensor:
- platform: template
sensors:
ml_per_hour:
value_template: >
{% if is_states('sensor.xxx', 'on') %}
{% set t = now().timestamp() - states('sensor.last_off') | float %}
{{ 8 /( t / 3600) }}
{% endif %}
or you can use that calc in a automation.
ok thanks a lot!
I checked my data and I found that this behaviour came with a new home assistant version. Before, the last_triggered attribute was not set when the action was running.
No, my last_triggered conditions have been working since about 0.98 and still do on 0.115.3
For conditions, nothing has changed. In the condition, last_triggered refers to the previous run. What changed is in the action. In the action, last_triggered refers to the actual run.
I’m just not sure.
To me “last triggered” means that one of the triggers has fired and ALL of the conditions have passed. (this is pretty much what petro said above) so my understanding seems to cover the current situation AND (I thought) the previous situation as well.
I’m not quite sure why you’d want to test last triggered in the action section. (my guess under all circumstances would be that this would be useless, unless you wanted to know how long “this automation” has been running ???)
Your usage case must be exceptional
Each change of a binary sensor triggers an automation. The use case is for example to calculate how long a door was open or how much water flows per hour.
So door, you’d need two datetime’s one with date the other (without (unless you may have door open > 23:59) set the with date one when door opens. When closes compare and set the without using a comparison.
The flow per hour, just use an automation on a time pattern /0 minutes and 2x input time (last and now) to calc difference, then transfer now to last
That’s why people call them helpers
Hope that helps
I have an automation with multiple triggers, that I only want to execute once a day, by whichever trigger comes in earlier. Currently I set helper boolean to ‘True’ in the action part of that automation. Do I understand this correctly, that I can remove the helper boolean in favor of checking the ‘last triggered’, as this attribute will only be set, not when the automation is triggered, but only, if the automation is triggered and all conditions are fullfilled.
In other words looking at the ‘last triggered’ attribute will give confirmations that the action section was triggered and in case I do not have additional conditional check in that area I can assume that all actions are performed).
EDIT: To make this more tangible, I’ll add my example:
- alias: Shutters - Close All
id: 'shutters_close_all'
trigger:
- platform: numeric_state
entity_id: sun.sun
value_template: "{{ state_attr('sun.sun', 'elevation') }}"
below: -6 # 'night' condition: from dusk to dawn, in typical locations
- platform: time
at: '18:00:00'
condition:
- condition: template
value_template: "{{ state_attr('sun.sun', 'elevation') <= -6 }}"
- condition: time
after: '17:59:59'
### Additional template condition to check if this automation already ran today
- condition: state
entity_id: input_boolean.disable_shutters
state: 'off'
action:
- service: cover.close_cover
entity_id:
- cover.all_shutters
I have an automation with multiple triggers, that I only want to execute once a day, by whichever trigger comes in earlier.
With last_triggered, you could check if your automation already ran in the last 12 hours.
Looking at your example I do not understand why you use these conditions? Do you know that triggers are connected with “OR”, while conditions are connected by “AND”?
I would use a delay-action to wait 12 hours:
– delay: “12:00:00”
Exactly. The shutters should be closed at night (depending on the sun’s elevation) , however in Winter this event comes to early, which is why I introduced the time trigger.
A artificial delay is probably only a workaround as this would be interested for example, if home assistant is restarted. Also the check for last triggered would be evaluated against the opposite automation that opens the shutters, at a given time, if nobody is home or when the first person wakes up.
So I would like to prevent that, opening the shutters is triggered by a false detection of a person waking up at let’s say 23:00:00, by checking if the opening shutters automation has already been executed today.
Sorry for maybe not having provided the full picture in advance.