Display uptime/status of my Sauna (In Hours and Minutes) in Lovelace UI

Can anyone help me figure this out? Haven’t had any luck with History stats… I’m trying to display the number of hours and minutes since my sauna switch has been turned on, and if it’s off, display the text “Off”. I’m relatively new to HA and have minimal coding experience… Thanks in advance for any help!

In order to preserve the time over HA restarts, I’d do this (substituting the actual entity name of your sauna switch):

Create helper input_datetime.sauna_last_on (date and time).

Automation:

alias: Update sauna last on record
trigger:
  - platform: state
    entity_id: switch.sauna
    from: 'off'
    to: 'on'
action:
  - service: input_datetime.set_datetime
    target:
      entity_id: input_datetime.sauna_last_on
    data:
      timestamp: "{{ now()|as_timestamp }}"

Template sensor:

template:
  - sensor:
      - name: Sauna running time
        state: >-
          {% if states('switch.sauna') == 'on' %}
            {% set t = now()|as_timestamp - states('input_datetime.sauna_last_on')|as_timestamp %}
            {{ '%02d:%02d' % ((t/3600)|int, (t%3600)/60) }}
          {% else %}
            Off
          {% endif %}

Pop that sensor (sensor.sauna_running_time) in an entities card. This will work over restarts, but will show “Off” if the switch cannot be read (‘unavailable’ / ‘unknown’ for whatever reason).

That will give hh:mm time format, and still works if the time exceeds 24h (long sauna!).

1 Like

Thanks, Troon! This is fantastic! I had to teach myself a little bit on how to create helpers and Templates, but this was a great practical exercise! It now works perfectly!! On a related note… If I wanted to automatically turn OFF the sauna say, 120 minutes after it was turned on, how would I do that leveraging what you’ve already done?

Using the helper, an automation:

trigger:
  - platform: template
    value_template: >-
      {{ now()|as_timestamp|timestamp_custom('%H:%M') ==
         (states('input_datetime.sauna_last_on')|as_timestamp+7200)|timestamp_custom('%H:%M') }}
action:
  - service: switch.turn_off
    entity_id: switch.sauna

That will work even if you restart, provided the system isn’t rebooting at the two-hour mark.

Alternatively, a simple version which won’t handle reboots:

trigger:
  - platform: state
    entity_id: switch.sauna
    to: 'on'
    for:
      hours: 2
action:
  - service: switch.turn_off
    entity_id: switch.sauna

Good call on accommodating for restarts - that is critically important. I attempted to implement the first solution you detailed, but I can’t get it to work. For starters, Visual Studio is telling me I’m missing property “value_template”, and “template” is not allowed. I gave up attempting to use the “New Automation” page because it kept messing with the formatting, and instead I am now attempting to edit the YAML directly. Here’s what’s currently in my automations.yaml:

- id: '1623448969320'
  alias: Automatically Turn Off Sauna After 2 Hours
  description: ''
  trigger:
  - platform: template
    value_template: "{{ now()|as_timestamp|timestamp_custom('%H:%M') ==
      (states('input_datetime.test_datetime')|as_timestamp+7200)|timestamp_custom('%H:%M') }}"
  condition: []
  action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id: switch.sauna

Any ideas what else I could do next to make this work?

Sorry, my mistake with the template vs value_template — I’m writing this by hand and sometimes make errors. The other problem was that I was referencing a test input_datetime from my system to try out the template, and forgot to replace it with yours. I’ve edited my post to fix both of these.

You should try to understand the content of the code rather than just copy-paste — that would have picked up the second error.

The template simply compares the current time, formatted as HH:MM, with the sauna-last-on time plus two hours (7200s) formatted the same way. When they match, go!

Also, I’ve guessed the name of the switch you need to operate to turn the sauna on — obviously, replace switch.sauna with the real name if different.

Works like a charm now. Thanks for all your help - I’m inspired to keep learning more!

1 Like

If I want to enter the delay time using the helper on dashboard (like: input_datetime.sauna_duration_time), how should I replace (7200s) in the code? I don’t know how to do it, thanks.