Need help to create "Template Sensor" to show time since, time until etc

I’m stuck…again.

The plan was to create a sensor that shows time since the last state change of a few sensors i use:

  • Time since awake/sleeping
  • Time since home/away
  • Time until next calendar event

Tested in the template editor and it works with these options, thinking about using the negative values for sleep and positive for awake.
Thinking to add a criteria to change between seconds/minutes/hours, but only for the sensor used in the frontend. Automations will have to be based on minutes i suppose.

Tested formulas:

{{ ((as_timestamp(now()) - as_timestamp(states.input_boolean.occupancy.last_changed))/3600) | round(1) }} hours awake
{{ ((as_timestamp(states.input_boolean.occupancy.last_changed) - as_timestamp(now()))/3600) | round(1) }} hours sleeping
{{ ((as_timestamp(now()) - as_timestamp(states.input_boolean.occupancy.last_changed))/60) | round(1) }} minutes awake
{{ ((as_timestamp(states.input_boolean.occupancy.last_changed) - as_timestamp(now()))/60) | round(1) }} minutes sleeping
{{ (as_timestamp(now()) - as_timestamp(states.input_boolean.occupancy.last_changed)) | round(1) }} seconds awake
{{ (as_timestamp(states.input_boolean.occupancy.last_changed) - as_timestamp(now())) | round(1) }} seconds sleeping

Template sensor:

time_sleep_awake:
  friendly_name_template: >-
    {% if is_state('input_boolean.awake', 'on') %}
      Awake
    {% elif is_state('input_boolean.awake', 'off') %}
      Sleeping
    {% endif %}
  unit_of_measurement: hours
  value_template: "{{ (as_timestamp(now()) - as_timestamp(states.input_boolean.awake.last_changed))/3600 }}"
  icon_template: >-
    {% if is_state('input_boolean.awake', 'on') %}
      mdi:sleep-off
    {% elif is_state('input_boolean.awake', 'off') %}
      mdi:sleep
    {% endif %}

The problem is you’re using now() in the template, and that does not cause entity state changes. Template sensors only update when entities they use generate state change events.

The easiest way to fix this is to enable sensor.time, and then add it to your template sensors via the entity_id configuration parameter:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      time_sleep_awake:
        # All your config parameters go here...
        ...
        entity_id: sensor.time

This will force them to update whenever sensor.time updates, which is once a minute.

EDIT: Actually, you’ll probably need to also add other entities your template uses. So:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      time_sleep_awake:
        # All your config parameters go here...
        ...
        entity_id:
          - sensor.time
          - input_boolean.awake
1 Like

It works!
That is awesome :slight_smile:
Thank you for helping out with what may be a “simple” issue.
Adding the sensor.time makes sense, thanks for explaining.

1 Like