Show time since last state change in lovelace

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

It should update every minute. Unless you don’t actually have a sensor.time

Thanks, you found the missing part!

1 Like

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

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

Thank you. Worked like a charm!

1 Like

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?

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.

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?

You can use an availability_template to prevent the template evaluating erroneous states of the sensors involved.

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"

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