Wake Up Alarm: Trigger Automation x minutes before input.datetime

I have set up a time only input.datetime helper with the time I wish my wake up alarm to be. I have also set up a input_number which ranges between 0 and 30 minutes. This is the time before the input.datetime that the automation should be triggered to start fading in my lights up to the wake up time. How would I go about using a template to trigger the automation the amount of time in the input_number before the input.datetime. Date is irellevant as it should run every day.

In other words I wish to trigger an automation a variable amount of time before a specific time both of which can be set in the frontend.

2 Likes

Create a Template Sensor that computes the wake up time (subtracts input_number’s value from the input_datetime’s value). Set its device_class to timestamp.

In your automation, use a Time Trigger that monitors the Template Sensor you created.

Simple as that; let me know if you need help creating the Template Sensor.


An alternative is to use a Template Trigger.

Thanks for your reply. I would prefer to use a template trigger to save any uneccesary entities I’m just not sure how to template it. I’m not familiar with templating timestamps.

Going from what you have already written:


trigger:
  - platform: template
    value_template: >
      {{  now() >= today_at(states('input_datetime.XXXX')) - timedelta(minutes= states('input_number.XXXX') | int) }}

1 Like

I have tried this as per your suggestion:

{{ now() >= today_at('input_datetime.alarm_clock_time') - timedelta(minutes=states('input_number.alarm_clock_rise_time') | int) }}

but I get:

ValueError: could not convert str to datetime: 'input_datetime.alarm_clock_time’

and removing the apostrophes:

{{ now() >= today_at(input_datetime.alarm_clock_time) - timedelta(minutes=states('input_number.alarm_clock_rise_time') | int) }}

gives:

UndefinedError: ‘input_datetime’ is undefined

Sorry for the typo, see original post for correction.

Thanks! This appears to work correctly. Now i just need a way to pass the input_number.alarm_clock_rise_time to the scene.turn_on service.

My action for the automation is:

service: scene.turn_on
data:
  transition: 60
target:
  entity_id: scene.standard

And the transition entry needs to take the value of the input number. Any ideas? Thanks!

I don’t know for certain if transition supports templating. If it does then this should work.

service: scene.turn_on
data:
  transition: "{{ states('input_number.alarm_clock_rise_time') | int(0) }}"
target:
  entity_id: scene.standard

This does appear to be supported (it is working) however the transistion is in seconds and my value is in minutes so i changed it to the following for the reference of anyone viewing this thread in future.

service: scene.turn_on
data:
  transition: '{{ states(''input_number.alarm_clock_rise_time'') | int(0) * 60 }}'
target:
  entity_id: scene.wake_up_alarm

The complete automation looks like:

alias: Wake Up Alarm
description: ''
trigger:
  - platform: template
    value_template: >-
      {{  now() >= today_at(states('input_datetime.alarm_clock_time')) -
      timedelta(minutes= states('input_number.alarm_clock_rise_time') | int) }}
    id: Pre Alarm
  - platform: time
    at: input_datetime.alarm_clock_time
    id: Alarm
  - platform: state
    entity_id: binary_sensor.iphone_focus
    from: 'on'
    to: 'off'
    id: iPhone Alarm
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Pre Alarm
          - condition: state
            entity_id: input_boolean.alarm_clock_active
            state: 'on'
        sequence:
          - service: scene.turn_on
            data:
              transition: '{{ states(''input_number.alarm_clock_rise_time'') | int(0) * 60 }}'
            target:
              entity_id: scene.wake_up_alarm
      - conditions:
          - condition: trigger
            id: Alarm
          - condition: state
            entity_id: input_boolean.alarm_clock_active
            state: 'on'
        sequence:
          - service: notify.mobile_app_iphone
            data:
              title: Alarm Clock
              message: It's time to wake up!
      - conditions:
          - condition: trigger
            id: iPhone Alarm
          - condition: state
            entity_id: device_tracker.iphone
            state: home
        sequence:
          - service: scene.turn_on
            data:
              transition: 10
            target:
              entity_id: scene.wake_up_alarm
    default: []
mode: single

1 Like

Since I was looking for similar automation could you pls help with mine? I have a time sensor that dynamically changes let’s say sensor.sunset_time. I want to set an automation that will notify me 10 minutes before the sunset. How can I achieve that?

You could probably just change

to

sensor.sunset_time

I am trying to do something similar, but I cannot get the automation to trigger with

  • platform: time
    at: input_datetime.alarm_clock_time

it only works if I use

  • platform: time
    at: ‘06:00:00’

I have the input.datetime defined as time only.

Any thoughts or suggestions are appreciated.

A Time Trigger supports an input_datetime that includes time-only, date-only, or time and date. If it’s not working for your time-only input_datetime then the question is: are you using a very old version of Home Assistant?

If you are using a recent version, then check the automation’s trace to see if it has in fact triggered but encountered some other problem.

Thank you very much for responding! I am running version 2022.7.7. I checked the trace for the automation but since it doesn’t trigger I don’t get anything.

# Inputdatetime

 alert_reminder_1:
    name: alarm_clock_time
    has_time: true
    has_date: false

 alert_reminder_2:
    name: alarm_clock_time2
    has_time: true
    has_date: false
 

# automation

- alias: Alert Reminder
  id: alertreminder
  initial_state: True
  trigger:
    - platform: time
      at: 'input_datetime.alarm_clock_time'
    - platform: time
      at: 'input_datetime.alarm_clock_time2'
  action: 
    service: input_boolean.turn_on
    data: {}
    target:
      entity_id: input_boolean.reminder_alert

I believe the reason why your automation isn’t triggering is because its two Time Triggers refer to input_datetime entities that don’t exist.

When you define an input_datetime, the name option represents its friendly name not its entity_id. The Time Trigger requires the input_datetime’s entity_id.

Here’s how to correctly reference the two input_datetime entities that you defined:

  trigger:
    - platform: time
      at: 'input_datetime.alert_reminder_1'
    - platform: time
      at: 'input_datetime.alert_reminder_2'

You can also do this:

  trigger:
    - platform: time
      at:
        - input_datetime.alert_reminder_1
        - input_datetime.alert_reminder_2

That was exactly it! I am embarrassed I did not catch that. I really appreciate your help! Thank you!

1 Like