Put together a sleep timer slider

There is so much wonderful documentation and forum posts, I just struggle to put it all together.

What I am trying to achieve is have a Lovelace UI card with a slider that you use to set a sleep timer for my TV.

All I have currently is a script that works to turn the TV off after a delay, I just want to make it more “smarter” and improve the WAF (wife approval factor).

Now to create the Lovelace UI Card, I can use the slider entity (I think):
https://github.com/thomasloven/lovelace-slider-entity-row

And to create the entity I need to create a template, but I have no idea on what to actually put in the template: https://www.home-assistant.io/integrations/template/

Then I need to pass the details of the slider to my script, this is what I have for my script so far:

bedroom_tv_sleep_timer:
  alias: Bedroom TV Sleep Timer
  fields:
      minutes:
        name: Minutes
        description: "The amount of time to wait before turning on the living room lights"
        selector:
          number:
            min: 0
            max: 60
            step: 1
            unit_of_measurement: minutes
            mode: slider
    # If called again while still running (probably in delay step), start over.
  mode: restart
  sequence:
  - delay:
      minutes:  "{{ minutes }}"
  - service: media_player.turn_off
    target:
      entity_id: media_player.bedroom_android
 

Then after all that, I am going to try and update the slider to reflect how long is left: https://community.home-assistant.io/t/timer-using-input-number-that-is-dynamically-updated/199757

Any ideas on how I put this all together?

Thanks

Have a look at this for some ideas ( it is essentially the same, except you are switching off a tv not a light):

Thanks @tom_l but again for my poor brain it is missing information.

It is not clear where to put things, like this goes here and this goes there.

However with blood, sweat and tears (no blood or sweat, but there were tears) I think I got it cracked.

In my configuration.yaml I added this section:

sensor BedroomSleepTimer:
  - platform: template
    sensors:
      bedroom_sleep_timer_remaining:
        friendly_name: 'Bedroom Sleep Timer Remaining'
        value_template: >
          {% if is_state('input_boolean.bedroom_sleep_timer', 'on') %}
            {{ [ (states('input_number.bedroom_sleep')|int - (as_timestamp(now()) - as_timestamp(states.input_boolean.bedroom_sleep_timer.last_changed)) / 60)|round(0) ,0 ]|max }}
          {% else %}
            0
          {% endif %}
        unit_of_measurement: "min"

input_boolean:
  bedroom_sleep_timer:
    name: Bedroom Sleep Timer
    icon: mdi:pizza

input_number:
  bedroom_sleep:
    name: Bedroom Sleep
    initial: 30
    min: 15
    max: 120
    step: 5
    unit_of_measurement: min
    icon: mdi:update

And then in my automations.yaml:

- id: e0a18608-491a-48e6-90f6-fa0b1a87a0ac
  alias: 'Bedroom Sleep Timer Reset'
  trigger:
    platform: numeric_state
    entity_id: sensor.bedroom_sleep_timer_remaining
    below: 1
  condition:
  - condition: state
    entity_id: input_boolean.bedroom_sleep_timer
    state: 'on'
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.bedroom_sleep_timer
  - service: media_player.turn_off
    entity_id: media_player.bedroom_android

Then added the new entities to my Lovelace UI:
Screenshot 2021-08-08 165314

The card appears to work with the timer counting down, now to see if it will turn off the TV.

4 Likes

I’m not sure how you have your configuration organised, but I think that will work. Does the timer remaining value get set to the slider value when you trun on the switch?

If you are using the UI to create Helpers you could have entered the input number and input boolean there.

One thing you might want to change is the input_boolean icon:

From:

icon: mdi:pizza

To:

icon: mdi:bell-sleep

Or

icon: mdi:sleep

You can search other appropriate icons here: https://materialdesignicons.com/

My code works as I tested it a few times and does what is says on the box.

I don’t get what you mean by use “the UI to create Helpers”, I have no doubt this is written somewhere I just know where.

Thanks for the icon help it will look a bit better, cheers.

1 Like

hello, just to help if needed, i just make one with an “input_number” helper and a good flow into NodeRed (into HomeAssistant of course).
it’s for my bed (pre)warmer : bed warm from 9PM til 12PM.
When i turn on light after 9PM, it set the OFF timer to 15 minutes. (slider auto go to 15 minuts)
But i can change of very cold or if it’s warm enough (change slider live)
slider move with decement counter.
At 0 second, it stop bed warmer. :wink:

Screenshot from 2022-11-30 18-43-24