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 %}