Time between two events (on/off)

I’d like to get a notification when my vacuum cleaner is finished. The message should contain information about how long the vacuum cleaner was cleaning.

- alias: vacuum time notification
  trigger:
    platform: state
    entity_id: vacuum.deebot
    from: 'on'
    to: 'off'
  action:
    - service: notify.device1
      data:
        message: "time: {{ relative_time(states.vacuum.deebot.last_changed) }}"

I think this won’t work, because the last change will always be right now. How is it possible to measure the time between the last on and off event? @petro could you help (again).

You should be able to use the trigger state object to access the state prior to the trigger. So something like trigger.from_state.last_changed I think.

But that only works if there are only two possible states, off and on. If that is true, you also don’t need the from.

Unfortunately this did not work. Tried this in my automation:

message: "time: {{ trigger.from_state.last_changed }}"

Error rendering template for call_service at pos 1: UndefinedError: 'trigger' is undefined

Also it’s hard to play with this because I can’t use templates to debug. Hope there is another way.

That’s because you can’t use templates everywhere. In this case you need to use a data_template. There are several examples in the template page I linked earlier. Each integration will also usually mention something:

Even with putting in data_template I get this error again.

Is it really possible to use this:
trigger.from_state.last_changed

Because in the examples there is no single one that tells you can put .last_called at the end.

What error did you get, the same one? That error message indicated that the trigger variable is not available, not last_changed. Maybe post the updated automation you have now?

The very first example in the automation templating page I linked uses trigger.from_state in the action. I can’t think of why last_changed wouldn’t be available either, it’s part of the same state object. You could try a test and see if trigger.from_state.state is working, just like in the example.

Yes, I get the same error.

I tried it again with trigger.from_state.state but still the same error. Could you try it yourself with some automation?
I also tried trigger.from_state and still the same error.

This is the automation

- alias: vacuum time notification
  trigger:
    platform: state
    entity_id: vacuum.deebot
    to: 'off'
  action:
    - service: notify.device1
      data:
        message: "time: {{ trigger.from_state.state }}"

I found the mistake I made. I triggered the automation using the Trigger button of the automation pop up. Of course that was stupid.

Now I triggered it with setting the device status of the entity and I get an output.

With:

time: {{ trigger.to_state.last_changed - trigger.from_state.last_changed }}

I get something like

time: 0:00:08.829251

I’d like to cut off the decimals. Best way would be to convert it in minutes. Seconds are not important for this.

For example

0:05:03.45665 should be shown as 5 min

{{ (trigger.to_state.last_changed - trigger.from_state.last_changed).seconds | round(0) }}

Thanks.

Tried to use .minutes instead of .seconds, but that did not work. So this is what I use now and it works:

time: {{ ((trigger.to_state.last_changed - trigger.from_state.last_changed).seconds / 60) | round(0) }} min

Thank you both!

that won’t work because timedelta objects do not have a minutes property. They only have days and seconds. FYI a subtracting 2 datetime objects returns a timedelta object. These are documented in python documents, not ansible/jinja2.

Thanks for clearing that up.