I struggled with how to make device specific alarm automations, for when we have company and my father gets up two hours before me (he is a 5 am sort of guy). I know others have mentioned having children, and wanting their lights to come on when just their echo went off.
Step 1)
Make sure you have the Alexa Media Player custom component:
Make an automation! Check your list of sensors and you will notice you have some new ones with the _next_alarm ending. For this example I will use sensor.master_bedroom_echo_show_next_alarm. Please swap it out for the device you want triggered. The rest should be copy and paste.
This trigger will set off your automation 2 seconds before your echo specific alarm. It has to be before because once the alarm goes off it there will no longer be a _next_alarm time. If you need them to sync up add the following to your action.
I’m really interested in getting this to work but when I try to assemble the automation i get this error msg: Message malformed: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘value_template’]
Hi
im new to Homeassistant and wanted some trigger like this for ages.
Unfortunately i can’t get this running, despite following your steps.
Are there things I’m overlooking ? Here are the steps i followed:
-activating the “Alexa Media Player custom component” → works. sensors are showing up.
-adding time sensors by copy and pasting the sensors textblock to the “configuration.yaml” file
-adding the triggercode to the automation and changing the sensor to my alexa device
im not getting any syntax errors. the automation just wont fire
This is how the automation looks like:
alias: alexa alarm
description: test
trigger:
- platform: template
value_template: >
{% set alarm_time = as_timestamp(states('sensor.echo_dot_next_alarm')) %}
{% set current_time = as_timestamp(states('sensor.date_time').replace(',', '')) %}
{% set pre_time = 2 %}
{{ current_time >= alarm_time - pre_time }}
action:
- service: notify.alexa_media_echo_dot
data:
message: alarm will trigger soon
data:
type: tts
mode: single
btw: thanks for the beginner-friendly tutorial. It’s one of the few you can find on this topic.
edit: nvm. it works … just make sure you set your system-timezone right …
In my testing I found that the sensor.echo_dot_next_alarm doesn’t get updated until after the triggered alarm is turned off. Therefore it seems unnecessary to mess around with delays.
I also use a fallback so there are no issues when an alarm is not set and the state is therefore unavailable. I use an arbitrary large number (1e+11) with the as_timestamp function, which will work fine until the year 5138.
does this work on timezones like mine: +1/+2 (with day saving)
does this method use the actual date and time of the alarm or just the time?
Asking this because, sometimes (on weekend) we dont have an alarm set and dont wont to trigger the automations
The state for the next alarm comes in with the day/time including timezone, for example: 2022-02-16T06:45:00-06:00
I am comparing that to home assistant’s date/time which doesn’t include a timezone, for example: 2022-02-15T13:53:00
When the as_timestamp function converts a day/time with a timezone, it will do the math to convert it into HA’s own timezone. If the day/time doesn’t have a timezone, it assumes it is already in HA’s own timezone.
In short:
Yes this will work with time zones (and therefore daylight savings)
This method uses all 3: the date, the time, and the timezone.
However I’m not clear how the timezone is set in the Alexa alarm. But if you had an Alexa in one timezone, and your HA in another timezone an hour behind, and your Alexa alarm was set for 5AM, the automation should trigger at 6AM in home assistant’s time and therefore would be at the same instant as the Alexa alarm.
Thanks a lot for clearing things up.
Have set up an test automation tomorrow to see how it goes.
Another question:
On my “old” smarthome system fhem i have setup an trigger to go of 18 minutes before the next alarm and trigger an sunrise effect on an hue lamp.
How to i archive an delta if -18:00 on the trigger?
Also, just ask Alexa to set an alarm for 1 minute from now. No need to wait until the morning to debug an automation. Or if you’re going to add the offset, make it a 1 minute (60 second) offset and ask Alexa to set an alarm for 2 minutes from now. Once you confirm everything is working, then you can simply edit the numbers to the full 18 minutes and set the alarm for the morning.
Also if you aren’t already using it, in Home Assistant under the “developer tools” screen there is “template” where you can try out the code. Paste everything between the double-quotes into that screen and see if you get what you expect. It should be true or false. You can then modify the code or just use parts of it. This is how you can inspect to determine what each piece of code does. If you want to see what Alexa’s next alarms, and the format it comes in, you can type in: {{ states('sensor.echo_dot_next_alarm' }}
Thanks a lot for your help!
Will take look into the developer tools template function, which up until now i haven’t used (just a few days in the HA world )
Logger: homeassistant.helpers.template
Source: helpers/template.py:1292
First occurred: 08:55:39 (1 occurrences)
Last logged: 08:55:39
Template warning: 'as_timestamp' got invalid input 'unavailable' when rendering template '{% set alarm_time = as_timestamp(states('sensor.echo_schlafzimmer_next_alarm')) %} {% set current_time = as_timestamp(states('sensor.date_time').replace(',', '')) %} {% set pre_time = 3600 %} {{ current_time >= alarm_time - pre_time }}' but no default was specified. Currently 'as_timestamp' will return 'None', however this template will fail to render in Home Assistant core 2022.1
Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:63
Integration: Template (documentation, issues)
First occurred: 08:55:39 (1 occurrences)
Last logged: 08:55:39
Error evaluating 'template' trigger for 'Morgen Routine': TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'