Is there a way to define a template sensor (without an automation) that can tell me how long a door remained open? it will be updated for the next event (open/close cycle).
So for example:
Door was opened last for 23 minutes. for the next cycle it can be less or more, will update when there is a new event. While the door is open and not yet close, the sensor may still have the last value.
I can do it with a couple of automations and input.tezt to use as variables, but i am wondering if there is a more direct way to do it with a template sensor. Or with the history_stats integration
thanks, i tried it, but it doesn’t work if the door is open for less than one minute, i’d like to get number of seconds. But the utcnow function updates once per minute
They don’t retain the value. If you want to do it this way you have to use a helper. I don’t think your logic will work because once a minute when the door is open it will reset start or the helper to now(). 1 minute is the default. If you want, you can use a time pattern as a trigger and set it to X seconds.
If you want seconds in what I gave you, remove the / 60 and you will have seconds. It will still only update once a minute without a trigger.
I created two sensors to do the trick and it works:
template:
- trigger:
- platform: state
entity_id: binary_sensor.lumi_lumi_sensor_magnet_02012603_on_off
from: 'off'
to: 'on'
sensor:
- name: Door Last Opened
state: "{{ now() }}"
- trigger:
- platform: state
entity_id: binary_sensor.lumi_lumi_sensor_magnet_02012603_on_off
from: 'on'
to: 'off'
sensor:
- name: Door Opened Duration
state: "{{ (now() | as_timestamp - states('sensor.door_last_opened') | as_timestamp) | int }}"
it will be great if there is a method to eliminate the first sensor, is there a way to pull the last timestamp when a sensor was in a particular state from the database, or can i use global variables on trigger sensors?
Would you be willing to share the full chunk of config you use here? I’m trying to do a very similar thing but not having any luck, the sensor just shows “unavailable.”
In the past, the Trigger-based Template Sensor’s initial value on startup would be unknown until the binary_sensor changed state (i.e. its computed state did not survive a restart). Now it reports the last-known state on startup.
yes, i just realized that it exists only for platform event, not platform state. so, i guess the shortest way to do it is a manual timestamp delta as above. There was a discussion in some forum about a timedelta() function in jinja so you could do that more direct. But not sure if that exists or not
- trigger:
- platform: state
entity_id: binary_sensor.door
from: 'on'
to: 'off'
sensor:
- name: Backyard door open duration
state: "{{ (trigger.to_state.last_updated - trigger.from_state.last_updated).total_seconds() }}"
last_updated contains a datetime object. If you subtract two datetime objects the result is a timedelta object. The computed value of the subtraction is reported, in seconds, by the timedelta object’s total_seconds() method.