I want to trigger a good morning script using the Input Datetime helper on the frontend. I need to be able to set different times for different days so I set up input_datetime.sunday_alarm, input_datetime.monday_alarm
, etc. When the set time is reached on that particular day I want it to turn on input_boolean.wakeactivated
which runs my script. How do I fashion the trigger template so it knows which day and time it is? If there’s a better way to go about this I’m all ears.
I would think something like this could be easier to manage.
{% set times = {"Sun":"10:00", "Mon":"08:00", "Tue": "07:00", "Wed": "05:00", "Thur": "09:00", "Fri": "09:00", "Sat": "07:30" } %}
{{ times[(now().timestamp() | timestamp_custom("%a"))]}}
times could be an input text I think.
But can’t you use the alarm on your phone?
If you’ve already setup (or just prefer) the input_datetimes, as long as you keep the naming convention you gave in the first post, you can use a template trigger like:
trigger:
- platform: template
value_template: >
{% set input = "input_datetime."~(now().strftime("%A")|lower)~"_alarm" %}
{{ now() >= today_at(states(input)) }}
Another option if you want something that is easy to adjust in the frontend is the Scheduler custom component and its associated lovelace card (both available in HACS)
Thanks. It works great. I’ll take a look at the Scheduler Card. I’m pretty happy with what I have though.
@jwelvaert Do you have the yaml-code for that setup? I would like to do something similar for me and my partners alarms. I’m updating my frontend and my alarm clocks look very basic in comparison to this.
This is the frontend (Lovelace)YAML
cards:
- entities:
- entity: input_boolean.wakestatus
name: Mode
- entity: input_datetime.sunday_alarm
- entity: input_datetime.monday_alarm
- entity: input_datetime.tuesday_alarm
- entity: input_datetime.wednesday_alarm
- entity: input_datetime.thursday_alarm
- entity: input_datetime.friday_alarm
- entity: input_datetime.saturday_alarm
show_header_toggle: false
show_icon: true
show_name: true
type: entities
title: Morning Alarm
type: horizontal-stack
This is from my automations.yaml
- alias: Morning Alarm
initial_state: true
trigger:
- platform: template
value_template:
'{% set dow = now().strftime("%A")|lower %} {% set input = "input_datetime."~dow~"_alarm"
%} {{ now() >= today_at(states(input)) }}
'
condition:
- condition: state
entity_id: input_boolean.wakestatus
state: "on"
action:
- service: input_boolean.turn_on
entity_id: input_boolean.wakeactivated
- delay: 00:00:05
- service: input_boolean.turn_off
entity_id: input_boolean.wakeactivated
id: c7f5197f730d423ba890a1bc1f28c424
- alias: Alarm Activated
initial_state: true
trigger:
platform: state
entity_id: input_boolean.wakeactivated
to: "on"
action:
service: script.turn_on
entity_id: script.wake_up_lygea
id: f23a9ede9640492fb71d1e1a6914fa94
You have to make 9 helpers - 1 for each day of the week - for example - input_datetime.sunday_alarm, input_boolean.wakestatus for the Mode switch and input_boolean.wakeactivated to activate the alarm when the wake time is achieved.
The automation is exactly the same as when I posted it almost a year ago. It has been working great.
Weird, it’s not triggering.
And in development tools I get:
ValueError: could not convert str to datetime: '2023-05-26 15:46:00'
For:
{% set dow = now().strftime("%A")|lower %} {% set input = "input_datetime."~dow~"_alarm"%} {{ now() >= today_at(states(input)) }}
Your input datetimes need to to be time-only, not date and time.
Thanks, @Didgeridrew.
And, @jwelvaert, what kind of script are you using to activate the alarm itself (script.wake_up_lygea); just to have some ideas… I was thinking of activating it in an android devices, but without defining in advance the time in the msg/command to notify the device, it seems it will not work out.
edit: Hmmm, its working fine with media start playing on a google nest speaker.
Here is the script. It basically turns the fans off, turns on the bedroom lights gradually, reactivates the motion sensors and plays a message.
wake_up_lygea:
sequence:
- service: switch.turn_off
entity_id:
- switch.jeffs_fan
- service: script.toggle_lygeas_fan_power
- delay: 00:00:03
- service: notify.alexa_media
data:
target: media_player.bedroom_echo
data:
type: tts
message:
Lidgy Lou... It's time to rise and shine.. honey.... today is going to be your best day ever. Time
for sunshine in a cup!
- service: automation.turn_on
entity_id: automation.master_bathroom_light_on_with_ceiling_motion_sensor
- service: light.turn_on
data:
transition: 300
brightness_pct: 100
target:
entity_id: light.master_bedroom_level
- delay: 00:01:00
- service: zwave_js.set_config_parameter
target:
entity_id: light.master_bedroom_level
data:
parameter: 6
value: Enabled
- service: zwave_js.set_config_parameter
target:
entity_id: light.master_bedroom_level
data:
parameter: 3
value: Occupancy
- service: input_boolean.turn_off
entity_id: input_boolean.snooze
mode: single
alias: Wake Up Lygea