I’m wanting to make a display that tells me how long our baby has been in bed. I do this by monitoring the open close state of her door. So I want to make a badge in lovelace that shows the amount of time since last the door was closed. This is what I have so far:
- platform: template
sensors:
rosie_slaap_timer:
entity_id: sensor.time
friendly_name: Rosie slaap timer
icon_template: mdi:clock-outline
#device_class: timestamp
value_template: "{{ relative_time(states.binary_sensor.rosie_deur.last_changed) }}"
However this sensor only updates once after device reboot and then remains fixed at a value around 37 or 40 seconds depending on boot speed. How to make this update?
2 Likes
tom_l
March 24, 2020, 12:33pm
2
It should update every minute. Unless you don’t actually have a sensor.time
Thanks, you found the missing part!
1 Like
arisermpo
(Arisermpo)
February 2, 2021, 4:17pm
4
Hey! I’m trying to do the same thing, but I get a warning:
[homeassistant.components.template.sensor] The 'entity_id' option is deprecated, please remove it from your configuration
tom_l
February 2, 2021, 4:30pm
5
Remove this line, specifying an entity id to monitor for updates is no longer supported:
- platform: template
sensors:
rosie_slaap_timer:
entity_id: sensor.time ####### Delete this line #######
friendly_name: Rosie slaap timer
icon_template: mdi:clock-outline
value_template: "{{ relative_time(states.binary_sensor.rosie_deur.last_changed) }}"
However if you want it to update every minute the template will need adjustment:
- platform: template
sensors:
rosie_slaap_timer:
friendly_name: Rosie slaap timer
icon_template: mdi:clock-outline
value_template: >
{% set t = states('sensor.time') %}
{{ relative_time(states.binary_sensor.rosie_deur.last_changed) }}"
1 Like
arisermpo
(Arisermpo)
February 2, 2021, 4:53pm
6
Thank you. Worked like a charm!
1 Like
danielo515
(Daniel Rodríguez Rivero)
February 21, 2021, 6:43am
7
Do you mind explaining why this works? It is just because you set a variable that consumes sensor.time ? Is that the only purpose of such variable?
tom_l
February 21, 2021, 6:56am
8
Correct. The template engine extracts entities to monitor for updates. So if we want the template to update every minute we have to put sensor.time
(or the function now()
) in there somewhere.
danielo515
(Daniel Rodríguez Rivero)
February 21, 2021, 10:28am
9
Thanks for the answer.
The problem of this approach is that it includes any change to the state of the entity, for example if it goes offline and then recovers. I guess a more reliable solution will require a helper “sensor” which you change using an automation that previously checks if the reason for triggering the state change is the correct one. Right?
tom_l
February 21, 2021, 12:08pm
10
You can use an availability_template to prevent the template evaluating erroneous states of the sensors involved.
pidiot
(Brian)
January 13, 2022, 5:18pm
11
New guy here, trying to accomplish basically the same thing… Apparently the sensor and binary sensor template configurations mentioned here are now obsolete (per https://www.home-assistant.io/integrations/template/#legacy-binary-sensor-configuration-format ). Any chance you want to give us an updated configuration example? I would really appreciate it! Thanks!
2 Likes
I too would like an updated solution here. Bump to @tom_l
I’m monitoring how long my coffee machine has been on. The new format is like this:
template:
- sensor:
name: "Coffee On"
icon: mdi:clock-outline
state: >
{% set t = states('sensor.time') %}
{{ relative_time(states.switch.espresso.last_changed) }}
In my setup I have a line like this in my configuration.yaml
template: !include templates.yaml
And then my templates.yaml
(a separate file that sits in the same directory as configuration.yaml
) has many template entities including the one above:
- sensor:
name: "Coffee On"
icon: mdi:clock-outline
state: >
{% set t = states('sensor.time') %}
{{ relative_time(states.switch.espresso.last_changed) }}
1 Like
This is my current version:
- name: "xxx slaap timer"
icon: mdi:clock-outline
unit_of_measurement: "Uur:Min"
state: >
{% set ct = ((as_timestamp(now()) - as_timestamp(states.binary_sensor.dc_xxx_deur.last_changed))/60) | int %}
{{states('sensor.time') and (ct // 60) }}:{{ '{:0>2d}'.format(ct%60) }}
unique_id: "xxx_slaap_timer"
kruton
(kruton)
July 8, 2023, 10:12am
15
Hey,
is there an easier way now to expose it in lovelace, now that the time since last change is shown in the details popup of the switch?
2 Likes
The entity card allows for ‘secondary information’ to show, including ‘Last changed’:
This can be set from the editor by clicking on the pencil next to an entity, or directly in the code:
type: entities
entities:
- entity: binary_sensor.findus_charging
secondary_info: last-changed