I’m pretty new to HA, so still figuring out a lot. For one of my first projects, I’m trying to let HA set an alarm on my android phone, either based on my calendar event tomorrow, of a manual alarm set in my dashboard.
Both the calendar and manual alarm have individual scripts/automations resulting in input_datetime.morgen_wekker_tijd being set, both date and time, after which a common-pathway script takes care of setting setting the alarm and doing some lights stuff.
I’m having trouble going from input_datetime.morgen_wekker_tijd to the alarm being set. I’ve tried using chatGPT for help, but am not getting anywhere.
this script works:
sequence:
- variables:
hour1: "{{now().hour|int}}"
min1: "{{now().minute+1|int}}"
alias: Set time
- data:
message: command_activity
data:
intent_package_name: com.google.android.deskclock
intent_action: android.intent.action.SET_ALARM
intent_extras: >-
android.intent.extra.alarm.HOUR:{{hour1|int}},android.intent.extra.alarm.MINUTES:{{min1|int}},android.intent.extra.alarm.MESSAGE:Home
Assistant Alarm,android.intent.extra.alarm.SKIP_UI:true
action: notify.mobile_app_mobiel_niek
mode: single
So I know in basis that the command works.
How do I go from the input_datetime.morgen_wekker_tijd to the variables being set (with a 10min offset)? It seems that whatever I try, the variables contain a wrong data type or something, which then results in the intent not containing the proper information.
an example of what I tried (after which the intent is send like above):
sequence:
But the key piece of information you are missing is that script variables won’t return datetime objects, they are just turned back into a datetime string. Do all your datetime math in the same variable and output it as a simpler data type like a list.
variables:
alarm_time: >
{% set base = strptime(states('input_datetime.morgen_wekker_tijd'), '%Y-%m-%d %H:%M:%S') %}
{% set offset_dt = base + timedelta(minutes=10) %}
{{ [offset_dt.hour, offset_dt.minute] }}
hour1: "{{ alarm_time[0] }}"
min1: "{{ alarm_time[1] }}"
Got it to work a bit better: it’s setting an alarm, but only on the hour. So 7:15 turns to 7:00 alarm.
Had to adjust it a bit, because just parsing your suggestion in variables gave a “Message malformed: expected a dictionary for dictionary value @ data[‘sequence’][0][‘variables’] home assistant” error while saving, so have this now:
alias: Set next alarm 2
mode: single
sequence:
- alias: Haal alarmtijd op
variables:
alarm_time: >
{% set base = strptime(states('input_datetime.morgen_wekker_tijd'), '%Y-%m-%d %H:%M:%S') %}
{% set offset_dt = base + timedelta(minutes=10) %}
{{ [offset_dt.hour, offset_dt.minute] }}
hour1: "{{ alarm_time[0] }}"
min1: "{{ alarm_time[1] }}"
- data:
message: command_activity
data:
intent_package_name: com.google.android.deskclock
intent_action: android.intent.action.SET_ALARM
intent_extras: >-
android.intent.extra.alarm.HOUR:{{ hour1 }},
android.intent.extra.alarm.MINUTES:{{ min1 }},
android.intent.extra.alarm.MESSAGE:Home Assistant Alarm,
android.intent.extra.alarm.SKIP_UI:true
action: notify.mobile_app_mobiel_niek
What am I missing?
Edit:
Even if I change min1 to min1: "{{now().minute+1|int}}"
It still sets the alarm to 7:00, even though it worked fine when hour1 comes from now() too