Time passed since start of automation

Hello!

I have an automation with an action-part like:

- service: climate.set_temperature
    data:
      temperature: 18
      hvac_mode: heat
    target: 
      entity_id: climate.thermostat

- wait_for_trigger:
  - platform: template
    value_template: '{{ not is_state(sensor_entity, alert_state)}}'

- service: climate.set_temperature
    data:
      temperature: 20
      hvac_mode: heat
    target: 
      entity_id: climate.thermostat

- service: notify.signal_to_family
    data:
      message: 'It took {{ measured_time }} minutes to complete.'

How can I meassure how long it took from start of the automation to the notification to include this information in the notification-message?

Getting this is a bit advanced and needs templating work.
Are you sure you want to go there?
Basically you will need to capture the time of the trigger which you can get the variable name for using trigger attribute or looking in the trace to get the variable names and then subtract the now() when it is done.
It’s something I’m NOT good at so I’m not going to do it for you.
You should look for people doing similar things in other community posts. There is also this that might help you but you will still need to feed it the information.
GitHub - Petro31/easy-time-jinja: Easy Time calculations for Home Assistant templates.

Also this isn’t a Blueprint question at all, so changing the topic to templates or automations might get you more eyes on the problem.

Thanks for your reply. I think I found the solution:

variables:
  _start: '{{ as_timestamp(now()) }}'

trigger:
...

action:
... [doing stuff]

  - service: notify.signal_family
data:
  message: 'It took {{ ((as_timestamp(now()) - _start) / 60) | round(0) }} minutes to complete.'

You are right, it’s not a blueprint-specific question, I changed the topic accordingly.

1 Like

Have you found a solution?

The solution is right above your post. You are setting a variable

{{ as_timestamp(now()) }}

and then can calculate later the time (for example in rounded to full minutes):

{{ ((as_timestamp(now()) - _start) / 60) | round(0) }}