How many hours since automation last ran

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

{{now() - state_attr('automation.write_hot_tub_log','last_triggered')}}

in the Template editor but cant get it to work such that the output is hours in decimal.
Hoping an expert will know the answer. Thanks.

1 Like

I’m no “has-been drip under pressure” (an ex-spurt) but this should work:

{{ ( now().timestamp() - as_timestamp(state_attr('automation.write_hot_tub_log','last_triggered')) ) / 3600 }}
4 Likes

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.

Correct.

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:

  action: 
  - service: input_number.set_value
    data_template:
      value: '{{as_timestamp(state_attr(''automation.write_hot_tub_log'',''last_triggered''))}}'
    target:
      entity_id: input_number.hot_tub_last_time
  mode: single

Then to retrieve the value and convert to hours since the automation was last run, and include it in a CSV message with other stuff, I used:

  action:
  - service: notify.hottublog
    data_template:
      message: '
        ...
        ,{{(now().timestamp() - states(''input_number.hot_tub_last_time'') | float) / 3600 }}
        ...'
1 Like

The result of that subtraction is a timedelta object.

A timedelta object has a total_seconds() method that reports its value in seconds. Divide it by 3600 to get hours (as a floating point value).

{{ (now() - state_attr('automation.write_hot_tub_log','last_triggered')).total_seconds() / 3600 }}

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).


EDIT

FWIW, this might be of interest to you:

1 Like