For the Sonos alarm: HA only sets the alarm’s time and marks it active. I do the rest with the Sonos app - you have way more options there (like source, volume, duration, speaker groups, etc.).
To find the Sonos alarm ID:
Using your Sonos app, set an active alarm. Verify it’s a non-recurring one (Frequency: Once).
Open the speaker’s debug API at http://<sonos_ip>:1400/support/review
Navigate to: <speaker_name> > Alarm Data
The trigger to start fading in is tricky: You cannot use now() in a trigger to compare the times. So, the trigger is to check the condition/value_template every minute.
The additional 15 seconds in the script’s delay (fade_in_time + 15 seconds) prevent the Sonos alarm from being deactivated too early.
First i’d like to say thank you for sharing this piece. I’m trying to implement that in my system, but i’m having a little issue.
1- I cannot locate this option"condition/value_template" in my player. and second can you please tell me how could i change the time format from “condition: template value_template: '{{ (utcnow().strftime("%s")” to local time or eastern time.
Hi @mistrovly You’re welcome, I’m glad I could help others.
condition/value_template has nothing to do with your player. It’s just a “numeric state condition” (https://home-assistant.io/docs/scripts/conditions/#numeric-state-condition) and because I check the values with a template, the key value: becomes value_template:. My remark “condition/value_template” is not a real check/condition, it’s just a reference to my construct at:
condition:
condition: and
conditions:
- condition: state
entity_id: input_boolean.alarm_clock_status
state: 'on'
- condition: template
value_template: '{{ ... }}'
My system does not run on UTC either, it runs on CEST. My check if it’s time to start fading in the lights works as follows:
I take a UTC timestamp (“now in seconds since …”),
then I add the fade in time in seconds -> this gives me a new timestamp in seconds,
this new timestamp is then converted to local time (with timestamp_custom("%H:%M")) and
finally, this is the (local) time I can compare to my (local) alarm time.
So, this calculation (taking a UTC timestamp, do some calulations and print out the local value) should be absolutely independent of your timezone.
If you just want a local timestamp, take now(), see: https://home-assistant.io/docs/configuration/templating/#home-assistant-template-extensions.
Hi, thanks for the howto. I have a problem with finding the Alarm Data. At http://<sonos_ip>:1400/support/review I can only see:
Zone Player Info
Zone Players
Media Servers
DNS Cache
Ethernet Ports
Ethernet Ports Statistics
Accounts
/VERSION
/etc/resolv.conf
/proc/ath_rincon/status
/proc/ath_rincon/phyerr
/proc/ath_rincon/nf
/proc/ath_rincon/station
/sbin/ifconfig
/usr/bin/uptime
/usr/sbin/brctl showstp br0
You’re right, the “Alarm Data” section seems to have vanished with the last update(s). At the moment, I see no other possibility to find the alarm ID but “trying it out”. Try ID = 1 and check if the alarm is updated, if not, try the next ID, etc.
Alright, I will try that, thank you for the fast reply
Edit: It works now, but I have another problem. This line doesn’t work: value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_long.state - ":15" }}'
I want to substract 15 minutes, so that my light starts 15 minutes before the music. Also I have a problem with the transition setting. I want to increase the intensity of the light over 15 minutes. So I would put in 90000, becuase it says it is in ms. I also did not find an option for choosing the colour for Xiaomi Yeelight.
just a stupid/naive question, everyone is still using input slider while i believe that this changed to input_number with 0.55. or did i miss read something somewhere?
Hey Guys, i am brand new to homeassisant and the forums and i was wondering if someone could help me with a modified version of my alarmclock. It does what it is supposed to do but, it seems very roundabout.
- action:
- data:
message: wake up
service: notify.zchat
alias: Wake me up
id: '1507981123211'
trigger:
- platform: time
minutes: '/1'
seconds: 0
condition:
- condition: state
entity_id: input_boolean.alarmweekon
state: 'on'
- condition: state
entity_id: input_boolean.alarmweekday
state: 'on'
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
- condition: template
value_template: ' {{ now().strftime("%H:%M") == states.sensor.alarm_time.state
}}'
- action:
- data:
message: Transition Lights
service: notify.zchat
alias: Transition Lights
id: '1507981123212'
trigger:
- platform: time
minutes: '/1'
seconds: 0
condition:
- condition: state
entity_id: input_boolean.alarmweekon
state: 'on'
- condition: state
entity_id: input_boolean.alarmweekday
state: 'on'
- condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
- condition: template
value_template: ' {{ now().strftime("%H:%M") == (states.sensor.alarm_time_before.state)
}}'
which leads to
What this does, at the moment, is sending two messages to a chat. First when the Transition timer is hit (at the moment set to 0 minutes prior) and second when the alarm goes off.
and to
or two messages with an offset. but i was to lazy to wait…
To my question, i have a bunch of stupid math in the beginning in order to get a point in time before the actual time, is there a nicer way to do that ?
please check the Automation Docs.
They are supposed to start with a trigger, then a condition (if needed) and then… the action.
You seem to start with the action?
Hey Poolcactus, thanks for the info. However, when you create a new automation using the automation creator this is how the file is created. So i am not sure maybe that’s because I am using something above 0.55. I also have to say that these automation work without a problem. I was just wondering if there is a better way to add minutes to time than what I have done
Sorry, I’m not familiar with the automation creator.
However, looking at what I think you are trying to achieve. (having the lights go on before the alarm with a specific offset?)
it might be easier to calculate that time with strptime()
which you can substract from the now.strftime
thank you so much for your contribution! This is really cool and it worked with just minor adjustments, I would like to let everyone know:
As described in the comments, I changed the input_slider to input_number. Maybe you could update the orignal post, so that people do not need to go through all comments?
For me “now()” instead of “utcnow()” worked. I could easily test the right code with the template developer tool
I had to hide the “sensor.alarm_clock_fade_in_minutes” within the “customize”-section.
I have added an automation with trigger on any of the input_numbers (Hour, Minute, Fade-in) in order to turn off the alarm (bool). It forces me to turn the alarm on again after doing changes to Hour, Minute or Fade-in. Otherwise the sonos app would not notice changes to the alarm time…
Hey Nazze
Thank you for your reply/feedback. I wanted to change the original post after input_slider became input_number a while ago, but it seems that posts cannot be edited anymore when they have reached a certain age.
Sure, you could use some “trial and error” method to find your alarm ID. Start with 1, set the alarm and verify in the Sonos app if it was activated. If not, increment the ID and test again.
I suggest taking a non-recurring alarm as the alarm in HA goes to off after it fired. If you use a recurring alarm in Sonos, your “alarm system” gets out of sync: Sonos will continue to ring the alarm while the alarm system in HA is deactivated after the first alarm.
Sorry, I thought you are just looking for the alarm ID in sonos for the setup. I don’t know what you are trying to achieve by dynamically getting the IDs from your Sonos alarms and I don’t know a way how to look them up.
Maybe you could write a script to poll the Sonos Web-API to get the alarm IDs.