Trigger Automations When a Specific Echo Device's Alarm Goes Off

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:

Step 2)

Add time sensors to your configuration.yaml file

sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

Step 3)

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.

  trigger:
    platform: template
    value_template: >
      {% set alarm_time = as_timestamp(states('sensor.master_bedroom_echo_show_next_alarm')) %}
      {% set current_time = as_timestamp(states('sensor.date_time').replace(',', '')) %}
      {% set pre_time = 2 %}
      {{ current_time >= alarm_time - pre_time }}

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.

action:
  - delay: 00:00:02
  - 

Enjoy!!!

3 Likes

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’]

platform: template
value_template: >
{% set alarm_time = as_timestamp(states(‘sensor.guest_room_echo_spot_next_alarm’)) %}
{% set current_time = as_timestamp(states(‘sensor.date_time’).replace(’,’, ‘’)) %}
{% set pre_time = 2 %}
{{ current_time >= ( alarm_time - pre_time }}

So first let’s make sure it’s formatted correctly.

trigger:
    platform: template
    value_template: >
        {% set alarm_time = as_timestamp(states('sensor.guest_room_echo_spot_next_alarm')) %}
        {% set current_time = as_timestamp(states('sensor.date_time').replace(',', '')) %}
        {% set pre_time = 2 %}
        {{ current_time >= alarm_time - pre_time }}

Try to copy and paste this code, let me know if that helps.

PS. In future when you paste code in here, highlight it and click the </> button, which will make sure it’s formatted correctly for posting.

Thank you I will try it out and my bad about the code.

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 :frowning:

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 …

You no longer need to do a timestamp conversion, you can use timedelta instead:

alias: alexa alarm
description: test
trigger:
  - platform: template
    value_template: >
      {% set alarm_time = states('sensor.echo_dot_next_alarm') | as_datetime %}
      {{ now() >= alarm_time - timedelta(seconds = 2) }}
action:
  - service: notify.alexa_media_echo_dot
    data:
      message: alarm will trigger soon
      data:
        type: tts
mode: single
2 Likes

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.

  alias: Lamp Alarm
  description: Turn on Lamp whenever Alexa Alarm triggers
  trigger:
  - platform: template
    value_template: "{{ as_timestamp(states('sensor.echo_dot_next_alarm'),1e+11) <= as_timestamp(states('sensor.date_time_iso'),0) }}"
  action:
  - service: light.turn_on
    target:
      entity_id: light.lamp

@mekaneck
Thanks a lot!
Will implement this soon.

Just two questions:

  • 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

thx

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.

1 Like

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?

thx

You can just subtract the number of seconds off the next alarm time, so the automation will trigger that many seconds earlier:

value_template: "{{ as_timestamp(states('sensor.echo_dot_next_alarm') - 18*60, 1e+11) <= as_timestamp(states('sensor.date_time_iso'),0) }}"

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.

1 Like

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' }}

1 Like

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 :slight_smile: )

this one throwed an exception because you can not substract an int from an string.
The correct template is:

value_template: "{{ (as_timestamp(states('sensor.stefi_s_echo_show_next_alarm'),1e+11) - 18*60) <= as_timestamp(states('sensor.date_time_iso'),0) }}"

anyway, thx a lot for your input.
This is just if anyone uses the code above.

I should have taken my own advice and tested out my code before I posted :blush:

Glad you got it figured out!

1 Like

I have this error now

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'

Can anyone help me?

You need to add a default for as_timestamp:

Can i use timedelta instead as_timestamp?

Yes

{% set alarm_time = states('sensor.echo_schlafzimmer_next_alarm') | as_datetime %}
{% set minutes_before = 60 %} 
{% if alarm_time != None %}
  {{ now() >= alarm_time - timedelta(minutes = minutes_before) }}
{% else %} 
  False 
{% endif %}
1 Like

I used this template

{% set alarm_time = states('sensor.echo_schlafzimmer_next_alarm') | as_datetime %} {{ now() >= alarm_time - timedelta(seconds = 3600) }}

with this template my morning routine working again.

1 Like