Here’s one way that might work for you. (I tested it and it seems to work ok. Obviously replace input_boolean.dryer
with your dryer on sensor.) Don’t forget to exclude sensor.dryer_on_time
from the recorder, otherwise it will “spam” your database.
input_boolean:
dryer:
input_datetime:
dryer_start:
has_date: true
has_time: true
automation:
- trigger:
platform: state
entity_id: input_boolean.dryer
to: 'on'
action:
service: input_datetime.set_datetime
entity_id: input_datetime.dryer_start
data_template:
datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
- trigger:
platform: time_pattern
seconds: '*'
condition:
condition: state
entity_id: input_boolean.dryer
state: 'on'
action:
service: homeassistant.update_entity
entity_id: sensor.dryer_on_time
sensor:
- platform: template
sensors:
dryer_on_time:
value_template: >
{{ (as_timestamp(now())
- state_attr('input_datetime.dryer_start', 'timestamp'))
|timestamp_custom('%H:%M:%S', false) }}