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.
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:
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.
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 }}"
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.