I’ve put temperature and energy sensors in my hot tub and have an automation that triggers every time the heater turns off where the action outputs to a file CSV data for inputting into Excel where I want to plot energy used since last trigger vs temperature delta (and maybe some function including the wind speed later).
I’m currently calculating the time since last trigger in Excel by subtracting the previous row’s time. The graph I get is OK so far with the limited amount of delta T collected so far, but if I have the lid off for a while the points are way off the straight line I’m looking for so I want to delete such points. But I cannot do that while I calculate the time since last trigger the way I do.
So my question is how can I put the ‘Time Since Last Triggered’ into my CSV output?
I’ve dabbled with variants of
Ha. Thanks Tom. Yes it works in the editor. I’ll stick it in my automation now and wait till it next fires (every 2-3 hours at todays ambient).
So the .timestamp forces now() to return seconds since some epoch as does the as_timestamp(…). Dividing by 3600 turns it into hours.
I just forced the action in the automation to run and got 1.4 micro hours (5ms) for time since last_triggered! It gives a sensible time in the Template editor.
Looks like the last_triggered time gets updated before the actions are run so this approach will not give me what I want.
Any other suggestion? I could store the last_triggered time in an input_number I guess, but that’s inelegant.
Got it working but had to resort to using an input_number to store the last time triggered.
To store the input_number as seconds since some epoch date I used:
I feel the result is to be expected given that you just manually triggered the automation.
last_triggered represents the time when the automation started executing its action (it has already successfully processed its trigger and action).
If the automation references its last_triggered value in its trigger or condition it will be, what you expect, namely the last time it triggered (i.e. not just a moment ago but some time in the past). However, upon executing its action the value of last_triggered is updated to the latest trigger time (i.e merely a moment ago).
If you want something to report the elapsed hours since the automation executed, you can use a Template Sensor.
template:
- sensor:
- name: Hot Tub Elapsed Hours
state: "{{ (now() - state_attr('automation.write_hot_tub_log','last_triggered')).total_seconds() / 3600 }}"
However, if your goal is to have an action in automation.write_hot_tub_log reference its own previous value of last_triggered then you can’t use a Template Sensor (because it will get updated the moment the automation’s last_triggered is updated).