Automations based on next scheduled alarm (on Android device)

Hi Ionut,

thank you very much for your link. It seems there is an issue (maybe with formatting / indentation) with this example? I copied it and edited it the way I think is correct, but still get an error:

Invalid config for [automation]: [minutes] is an invalid option for [automation]. Check: automation->minutes. (See /config/configuration.yaml, line 18). Please check the docs at Automation - Home Assistant

this is my code in automations.yaml so far:

- alias: Turn on light_sz-01 before Android Alarm
  trigger:
    platform: time
    minutes: /1
    seconds: 0
    condition:
      condition: template
      value_template: '{{ (((as_timestamp(now()) | int) + 5*60) | timestamp_custom("%Y-%m-%d %H:%M:00")) == states.sensor.wecker.state }}'
  action:
    service: homeassistant.turn_on
    entity_id: light.light_sz-01

maybe formatting is wrong?
Thank you!

I think I managed to get it to work by myself. platform: time should actually be platform: time_pattern.
For anyone who is also struggling, here is my code for the automation:

- alias: turn on bedlamp 5 minutes before android alarm
  trigger:
    platform: time_pattern
    minutes: /1
    seconds: 0
  condition:
    - condition: template
      value_template: '{{ (((as_timestamp(now()) | int) + 5*60) | timestamp_custom("%Y-%m-%d %H:%M:00")) == states.sensor.android_alarm.state }}'
  action:
    service: homeassistant.turn_on
    entity_id: light.bedlamp
3 Likes

Does this work with HTTPS? I can’t seem to get the app to connect to my HA. Thanks! Awesome idea.

I am using a Samsung S9 and Ariela. The timestamp I get in HA from my alarm sensor is in format “2001-01-01T01:01+0100”.
I have tried to use the suggested format above, but can’t get HA to read it.

value_template: ‘{{ (((as_timestamp(now()) | int) + 1*60) | timestamp_custom("%Y-%m-%d %H:%M:00")) == states.sensor.galaxy_s9_alarm_sensor.state }}’

Any suggestions please?

It might not :). But I can look into it.

Then it looks like you get a different format than %Y-%m-%d %H:%M:00, as you also have the timezone embedded. Is the states.sensor.galaxy_s9_alarm_sensor.state not of format 2019-12-14 07:55:00 ?

I tried HTTPS now and it works. What does the log say when you do “save and test connection”?

When I look at the state in HA it’s in this format: state: 2019-12-16T05:55+0100

1 Like

Can you try again with the latest version 1.8 and use the input_datetime instead? You can read how to configure the input_datetime here. If the problem persist, can you paste the log from the “Edit Connection” view?

Thanks for reply. I have installed latest version on Play (1.12) and it seems i have to use legacy sensor to get successful connection to my HA.

All good so far and I have the “input_datetime.next_alarm” in my HA. However, it says “Invalid Date”.
image
When trying the code below in template tool, I get the result “None”:

Below full log from App:

Using long lived token
Entity ID is of type legacy sensor
Request: POST http://(my_HA_server_ipaddress):8123/api/states/input_datetime.next_alarm

Request headers: Authorization: Bearer
(My Long lived token removed here)

Request body: {“state”:“2020-01-13 05:55:00”}

Success, response: {“attributes”: {}, “context”: {“id”: “751143328f3b47c3acd36c31174f6c00”, “parent_id”: null, “user_id”: “a40c230a48f045de9bb3f160fed3a90c”}, “entity_id”: “input_datetime.next_alarm”, “last_changed”: “2020-01-12T22:00:18.219764+00:00”, “last_updated”: “2020-01-12T22:00:18.219764+00:00”, “state”: “2020-01-13 05:55:00”}

You need to uncheck “entity ID is legacy sensor instead of input_datetime” (the second switch) in the Hassalarm app under Edit Connection.

Awesome Johan :+1:
When I first tested with “Test connection” it failed, that is why I used legacy sensor. When testing now, it also show successful.

Do you by any chance know why HA interoret my alarm time -5 min? I use Samsung standard clock and alarm.

Yes! It’s because of this bug in the Samsung alarm clock: https://eu.community.samsung.com/t5/Galaxy-S9-S9/Pie-Clock-app-bug/td-p/927333

A “solution” is to use another alarm clock, like the Google Clock instead, or compensate for the drift in your home assistant automations.

For reference: https://github.com/Johboh/hassalarm/issues/7

Ahhh, sorry, didn’t see that thread. Sorry for double your things. You have made an awesome project and I like it very much. I will definitely use this onwards. Thanks!

Hello, i am unable to reach to the HA assistant with the hassalarm app.
Getting this info in the app log:

Using long lived token
Entity ID is of type input_datetime
Request: POST http://xxxxxxx.duckdns.org:443/api/services/input_datetime/set_datetime

Request headers: Authorization: Bearer long_lived_token_here_abcdefgh

Request body: {“datetime”:“2020-02-04 06:12:01”,“entity_id”:“input_datetime.next_alarm”}

Failure: unexpected end of stream on Connection{xxxxxxx.duckdns.org:443, proxy=DIRECT hostAddress=xxxxxxx.duckdns.org/IP:443 cipherSuite=none protocol=http/1.1}

Hi!
What if you try https://xxxxxxx.duckdns.org as your host instead? E.g. https instead of http as well as removing the port number.

//Johan

hi johboh thanks for your fast reply.

Following your suggestion, I tried also https://xxxxxxx.duckdns.org (no port)

Result is:

Using long lived token
Entity ID is of type input_datetime
Request: POST https://xxxxxxx.duckdns.org/api/services/input_datetime/set_datetime
Request headers: Authorization: Bearer long_lived_token_here_abcdefgh
Request body: {“datetime”:“2020-02-04 06:12:01”,“entity_id”:“input_datetime.next_alarm”}
Failure: 400:Bad Request

In case i specify the port 443, i get it reflected in the url
Request: POST https://xxxxxxx.duckdns.org:443/api/services/input_datetime/set_datetime
but same result
Failure: 400:Bad Request

Thanks,
alex

hi johboh
I removed from the configuration.yaml the legacy api password entry. I was keeping it there for historical reasons (silly), although i was not using it anymore.
I restarted HA and now connection is successful using:
https://xxxxxxx.duckdns.org/ (no port)

thanks a lot!

Would it be possible to add a feature where it looks at the label on Android to determine the “next alarm”. For instance, if I have 2 alarms set throughout the day, but only 1 is to get up
I would want my morning automation to only run for the first alarm and not the second. Is this do-able?

I don’t think so. The API only expose the next alarm timestamp, and nothing about the label/name of the alarm. The API I’m using is a public Android OS API which reports the next scheduled alarm, as set by the alarm clocks apps, I’m not interfacing with the alarm app itself (as they don’t expose any API).

However I do understand the need for it. Maybe I can add some kind of silent/cutoff-period setting so one can specify to not report any next alarm to hass between these times. Or a setting to only report the first alarm in the day.

1 Like