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

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

Hey guys, my template for next alarm not triggering anymore:

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

But there is also no error in the logs.
Any hints what’s wrong with my template?

PS.: it should trigger 18 minutes before the next alarm

thx

Solved!
I accidently removed the “sensor.date_time_iso”. After readding to my configuration all is working again.

Hey guys

Not sure what I’m doing wrong, as the Automation does trigger and switch on my heating as it should, but the logs warn me about this:

Error evaluating ‘template’ trigger for ‘websocket_api’: ValueError: Template error: as_timestamp got invalid input ‘unavailable’ when rendering template ‘{% set alarm_time = as_timestamp(states(‘sensor.echo_mancave_next_alarm’)) %} {% set current_time = as_timestamp(states(‘sensor.date_time’).replace(’,’, ‘’)) %} {% set pre_time = 1800 %} {{ current_time >= alarm_time - pre_time }}’ but no default was specified

Error evaluating ‘template’ trigger for ‘websocket_api’: AttributeError: ‘NoneType’ object has no attribute ‘tzinfo’

Admittedly, I never really looked into the logs as stuff just works, but trying to get as many things clean as possible, such stuff now makes me wonder where I’m going wrong.

Any help is much appreciated :slight_smile:

Since Oct '21 the as_timestamp and other filters have required that defaults are explicitly declared.

When there isn’t a defined next alarm, you are going to get errors because there’s no value to apply your filters to.

These two sources of errors are eliminated in the template trigger example I gave above.

Thanks so much!

I read everything in this thread, but templating still isn’t something I feel good with and therefore didn’t quite get it.

I tried your solution again and it works perfectly. Will review my other templates now as well on my quest to reduce errors and avoid “delay for” etc.

Can anyone here help me please,

I’d like to use the next_time senor of my echo. I tried every trigger you wrote above, but it will just not trigger.

I used the template from didgeridrew and I added a time sensor in my configuration.yaml

{% set alarm_time = states(‘sensor.bad_next_alarm_next_alarm’) | as_datetime %}
{% set minutes_before = 1 %}
{% if alarm_time != None %}
{{ now() >= alarm_time - timedelta(minutes = minutes_before) }}
{% else %}
False
{% endif %}

@kristina , double check your template…

  1. It looks like you have an autofill error that doubled _next_alarm : sensor.bad_next_alarm_next_alarm
  2. You have “fancy quotes” around the sensor’s entity id, you need to use plain quotes. This happens sometimes when copying text from the forum that isn’t in a formatted code block.
{% set alarm_time = states('sensor.bad_next_alarm') | as_datetime %}
{% set minutes_before = 1 %}
{% if alarm_time != None %}
{{ now() >= alarm_time - timedelta(minutes = minutes_before) }}
{% else %}
False
{% endif %}

Oh my goodness. I really didn’t see my fault with the double thing. The quotes were alright. These were just fancy because of the font in my internet explorer.

Thank you so much for your help.

1 Like

I’m a complete beginner at Homeassistant and I’m trying to set up my automation to run 5 minutes before the alarm, but I don’t really understand the example above.
I have created a sensor and can see the Developer Tool / Template that it shows True/False depending on whether the alarm is set 5 minutes before or not.

What I don’t really understand is how to create an automation of it.
This is the Triggers Template

  trigger:
    platform: template
    value_template: >
	{% set alarm_time = states('sensor.bedroom_next_alarm') | as_datetime %}
	{% set minutes_before = 5 %}
	{% if alarm_time != None %}
	{{ now() >= alarm_time - timedelta(minutes = minutes_before) }}
	{% else %}
	False
	{% endif %}

This is from the Edit in YAML

alias: Alexa WakeUp Light
description: ""
trigger:
  - platform: template
    value_template: "  trigger:\n    platform: template\n    value_template: >\n\t{% set alarm_time = states('sensor.bedroom_next_alarm') | as_datetime %}\n\t{% set minutes_before = 1 %}\n\t{% if alarm_time != None %}\n\t{{ now() >= alarm_time - timedelta(minutes = minutes_before) }}\n\t{% else %}\n\tFalse\n\t{% endif %}"
    enabled: true
condition:
  - condition: device
    type: is_off
    device_id: f3ed9a8f0a526677ae0e685f0bb5e1ba
    entity_id: switch.first_floor_bedroom_lamp
    domain: switch
action:
  - type: turn_on
    device_id: f3ed9a8f0a526677ae0e685f0bb5e1ba
    entity_id: switch.first_floor_bedroom_lamp
    domain: switch
mode: single

The trigger is not executed and can someone please show me how this should be done correctly?

You have a copy/paste error in your trigger configuration that is causing it to fail.

If you already created the template sensor, you do not need to use the template again in your trigger. You have not included the entity ID of your sensor so you will need to correct it (and possibly the to: state as well) before using the following automation:

alias: Alexa WakeUp Light
description: ""
trigger:
  - platform: state
    entity_id: sensor.#YOUR_ALARM_SENSOR#
    to: 'True'
condition:
  - condition: device
    type: is_off
    device_id: f3ed9a8f0a526677ae0e685f0bb5e1ba
    entity_id: switch.first_floor_bedroom_lamp
    domain: switch
action:
  - type: turn_on
    device_id: f3ed9a8f0a526677ae0e685f0bb5e1ba
    entity_id: switch.first_floor_bedroom_lamp
    domain: switch
mode: single

Thanks a lot Didgeridoo for helping out :blush:

Got everything working flawlessly now :+1: :ok_hand: :laughing: