Sonos Sleep timer template

Hi,

been a while since I’ve posted anything, but thought I would share my script for a Sonos Sleep timer that takes a number input rather than a fixed number.

I use a button entity with an icon to run the script from the dashboard and an entity card for the time input (actual a number input, but its used as time)

I have a number helper that has a step size of 1, a max of whatever you want in minutes(I have 120 minutes as max), minutes as unit of measurement and an entity Id of input_number.sleep_timer

this is the very simple script for the timer

sonos_timer:
  sequence:
  - service: sonos.set_sleep_timer
    target:
      entity_id: media_player.master_bedroom
    data:
      sleep_time: '{{ states(''input_number.sleep_timer'') | int * 60  }}'
  mode: restart
  alias: Sonos Sleep Timer
  icon: mdi:timer-sand

whilst the Sonos is playing you can set the time and then press the button to run the script , Simple!

because the script restarts you can change the time if you like and then re-run the script before its finished.

any edits you think might improve the script let me know.

enjoy

2 Likes

Hi Grey Linux,

I believe this is what I am looking for. Just to confirm it would also work with turning off my air conditioning right?

In theory , possibly, it depends if you service that your calling requires a sleep_time , this is not really a timer script its more of a way of being able to input the number in the sleep_time variable that the
sonos.set_sleep_timer service requires an integer for.

if you want to turn your air con off at a certain time then I have an automation for that ,
I was helped with this automation on this thread here

Hi Greylinux,

Thanks for your reply. After I replied I found the specific sonos timer function.

Thank you too for your link I had a quick look, and I mean really quick more like a gander and possibly saw that the timer function is setup as an input time format. While that would work for me of course I was more thinking of an actual timer which generally comes with a tv or natively with an airco. I did find a sleep timer blue print but I haven’t figured out how to set that up yet. iow how I setup the input time on the dashboard and then say start the countdown and when your done switch off the air con :wink:

Unfortunately unless the integration you want to use for the switch of your air con , has a service with a timer built in like the sonos timer, you will have to create a timer automation that on completion, triggers another automation or script that calls the service for the switch to turn off .
As far as the template goes for the input number you can use my example above as the value for the timer and a little of the link I posted about making an automation to turn switches off.

Plus the timer integration should help with some examples

Of course if you really get stuck you can of course post a thread with what you’ve done and ask for some help .

Hope this helps

thank you, I will for sure ave a look and when I find my solution i’ll post it here :wink:

Thank you @GreyLinux for that little script. It still works fine, and I have used GitHub - jcwillox/lovelace-paper-buttons-row: Adds highly configurable buttons that use actions and per-state styling. to make it into a usable UI:
image

type: entities
entities:
  - entity: input_number.bedroom_sonos_sleep_timer
    name: Sleep timer
    extend_paper_buttons_row:
      position: right
      buttons:
        - icon: mdi:check
          tap_action:
            action: call-service
            service: script.sonos_timer
            preset: mushroom
        - icon: mdi:cancel
          tap_action:
            action: call-service
            service: sonos.clear_sleep_timer
            service_data:
              entity_id: media_player.unnamed_room_2
            preset: mushroom
title: Sonos Bedroom

The behaviour of Sonos’ sleep timer is a little strange though: X minutes at the initial volume, and then it tapers the volume down to 0 in a minute.

1 Like