Intent to set google clock alarm from input.datetime not working

Hi people,

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:

    variables:
      base_time: >
        {{ strptime(states('datetime.morgen_wekker_tijd'), '%Y-%m-%d %H:%M:%S') }}
      alarm_time: >
        {{ base_time + timedelta(minutes=10) }}
      hour1: "{{ alarm_time.hour }}"
      min1: "{{ alarm_time.minute }}"

Thanks for any help you can give me, sorry if it’s a really obvious answer!

You are missing the input_ in the entity ID.

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] }}"

Thanks!

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

There seems to be something going on with using a multi-line format. I’ve tried all the different multi-line indicators with no working results.

But, if you format it as a single line, it works. Both of the follow formats are working for me:

- 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

or

- 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

awesome, thank you so much, this got it working!

Hi, welcome to the forum!

Good that your issue got solved.

Please take the time to mark the post that holds the answer.
You do that by selecting the three dots under the post:

image

Then select the check box:

image
By doing so:

  • this thread can be useful to other users as well (this thread comes up as solution when starting a thread which is similar to yours).
  • this prevents that someone else steps in trying to help

Thanks for giving back to the community! :+1: