Sleep Timer

This blueprint takes an input_number and a timer to create a sleep timer! The user can configure:

  • The action(s) that should occur when the timer starts
  • The action(s) that should occur when the timer expires
  • The action(s) that should occur when the timer is canceled

Note that the unit of measurement on the input_number defaults to minutes.

blueprint:
  name: Sleep Timer
  description: Perform one or more actions after a sleep timer expires
  domain: automation

  input:
    input_number:
      name: Input Number
      description: The input number that determines how many minutes
        the sleep timer should last for
      selector:
        entity:
          domain: input_number

    timer:
      name: Timer
      description: The entity that represents the timer itself
      selector:
        entity:
          domain: timer

    timer_cancel_action:
      name: Action on Timer Cancel
      description: The action(s) to run when the timer is canceled
      default: []
      selector:
        action: {}

    timer_expire_action:
      name: Action on Timer Expire
      description: The action(s) to run when the timer expires
      default: []
      selector:
        action: {}

    timer_start_action:
      name: Action on Timer Start
      description: The action(s) to run when the timer starts
      default: []
      selector:
        action: {}

mode: restart

variables:
  slider: !input input_number
  slider_value: "{{ states(slider) | int * 60 }}"

trigger:
  - platform: state
    entity_id: !input input_number
  - platform: state
    entity_id: !input timer
    to: idle
action:
  choose:
    # 1. The timer expires:
    - conditions:
        - condition: template
          value_template: >
            {{ trigger.to_state.state == "idle" }}
      sequence:
        - service: input_number.set_value
          data:
            entity_id: !input input_number
            value: 0
        - choose: []
          default: !input timer_expire_action
    # 2. The timer is canceled:
    - conditions:
        - condition: numeric_state
          entity_id: !input input_number
          below: 1
      sequence:
        - service: timer.cancel
          data:
            entity_id: !input timer
        - choose: []
          default: !input timer_cancel_action
    # 3. The timer is started:
    - conditions:
        - condition: numeric_state
          entity_id: !input input_number
          above: 0
      sequence:
        - service: timer.start
          data:
            entity_id: !input timer
            duration: "{{ slider_value }}"
        - choose: []
          default: !input timer_start_action
7 Likes

Hey dont get it working :frowning

just the “Start” procedure works for me.

Following automation made with the assistant:

- id: '1615771078429'
  alias: Sleep Timer
  description: ''
  use_blueprint:
    path: bachya/sleep-timer.yaml
    input:
      input_number: input_number.sleeptimer
      timer: timer.sleep
      timer_cancel_action:
      - service: switch.toggle
        target:
          entity_id: switch.smartplug
      timer_expire_action:
      - type: turn_off
        device_id: db88ee791536077bdeac580ddad761cf
         entity_id: light.luester
        domain: light
      timer_start_action:
      - type: turn_on
        device_id: db88ee791536077bdeac580ddad761cf
        entity_id: light.luester
        domain: light
        brightness_pct: 50

I added a timer and a input with the helper:

Sleep   timer.sleep  Timer

Sleeptimer  input_number.sleeptimer  Nummer

I also tried this with adding the timer in the config and the input number in the inpt_number.yaml Added a new Automation with the assistant, but Same Problem.

Any Idea?
Regards

Hey,

so i found my mistake:
made a “min” with the input. I made 5 minutes min, because I never need a timer less then 5 minutes.

When i cancel the timer, the expired action runs too. Wasnt expecting this.

The rest works fine now, THX for your work!

Nice blueprint, works for me, now the TV shutdown automatically and dont run Till the morning When i wakeup :sweat_smile:

Thanks for this, but:

I just don’t understand why the actions that should be performed when timer expires are executed when the time is cancelled as well.

I finally solved it by using this blueprint automation just to start the timer with no actions.

Then I created another automation in UI with trigger:

Trigger Type: Event
Event type: timer.finished
Event data: entity_id: timer.<timer_name>

This one is triggered only when timer is finished, not cancelled.

1 Like

kkoouu

i have the same problem. I guess its something with the “0” and “1” if. They seem to be wrong. But im new here and wasnt able to check. My heating controlling was more important for me.

everything in computers have something to do with 0 and 1 :smiley:

You can test my workaround, it’s working fine :slight_smile: I will try to review original blueprint later …

1 Like

Not sure whats going on but even though the timer seems to work correctly, when the timer expires and go to idle and get this error and my automation doesnt work ie. turn off tv

2021-09-28 20:10:49 ERROR (MainThread) [homeassistant.components.automation.sleep_timer] Sleep Timer: Choose at step 1: choice 1: Error executing script. Invalid data for call_service at pos 1: Invalid value for input_number.sleep_timer_duration: 0.0 (range 10.0 - 30.0)
2021-09-28 20:10:49 ERROR (MainThread) [homeassistant.components.automation.sleep_timer] Sleep Timer: Error executing script. Invalid data for choose at pos 1: Invalid value for input_number.sleep_timer_duration: 0.0 (range 10.0 - 30.0)
2021-09-28 20:10:49 ERROR (MainThread) [homeassistant.components.automation.sleep_timer] Error while executing automation automation.sleep_timer: Invalid value for input_number.sleep_timer_duration: 0.0 (range 10.0 - 30.0)

That seems to indicate that your input_numbers have a min of 10 and a max of 30; a value of 0 won’t work there.

The initial is 10 and I tried min 5 and max 30 and still the same issue.

Checked the trace and yeah it think i chose 0

choose:
  - conditions:
      - condition: template
        value_template: |
          {{ trigger.to_state.state == "idle" }}
    sequence:
      - service: input_number.set_value
        data:
          entity_id: input_number.sleep_timer_duration
          value: 0

It weird that it shooting the error after the timer completes when that type of error should throw at the start. Either way, it doesnt allow the action to continue. Il have to try to do it manually instead.

This fixed it for me.