[Android App] Trigger automations with your Android Alarm Clock!

Hi everybody, I would like to share my most recent project:

Problem

I like to use my phone as an alarm clock for various things such as cooking and waking up in the morning. However, I have not found a convenient possibility to reliably and dynamically integrate my phones alarms into Home Assistant without actually having to actively interact with Home Assistant.

Ideally I want to be able to distinguish between following states of an alarm:

  • when an alarm is active
  • multiple user definable stages before and after ab alarm
  • when an alarm is disabled
  • when snooze-mode is enabled

All stages should be silent and parseable programatically.

Solution

I have forked the awesome open source alarm clock called simple clock from simple mobile tools and have extended it to do exactly what I have described above.

Each created alarm has the possibility to create multiple “silent alarms”. A silent alarm is a broadcast which can be processed by other apps such as Tasker, Easer or Broadcast to mqtt. The occurring event can then be sent to Home Assistant in order to trigger an automation.

The image below shows a screenshot of the applications alarm setup with two silent alarms scheduled:

  • Audible alarm at 7:00 am
    • Triggers broadcast com.simplemobiletools.ALARM_IS_ACTIVE
  • Silent alarm at 6:30 am
    • Triggers broadcast com.simplemobiletools.ALARM_GOING_TO_RING
  • Silent alarm at 7:15
    • Triggers broadcast com.simplemobiletools.ALARM_GOING_TO_RING

Example usage Scenarios

  • Trigger my wakeup-light before my alarm activates without having to interact with home assistant

    • A automation simply waits for the broadcast message ALARM_IS_GOING_TO_RING and then triggers the wakeup light routines
  • After I disable my alarm, I want certain devices to be turned on, such as my coffee machine, some lights, start playback of the local news, etc.

  • When I hit the snooze function too often, my home blasts me with unpleasant lights and plays loud music

Documentation

Following broadcasts are emitted:

com.simplemobiletools.ALARM_SET : when a new Alarm is created.
com.simplemobiletools.ALARM_GOING_TO_RING : when a silent alarm is triggered - only if the main alarm is enabled.
com.simplemobiletools.ALARM_IS_ACTIVE when an audible alarm is active.
com.simplemobiletools.ALARM_SNOOZED when an audible alarm has been snoozed.
com.simplemobiletools.ALARM_DISABLED when an audible alarm has been disabled.

All broadcasts contain the same payload with the attributes, which can be processed by other automations:

label
hours
minutes
days
id

All silent alarms have the same label as the main alarm, thus helping you distinguish individual alarms in your automations.

Example Automations

Wakeup light

This automation triggers my sunrise script once the MQTT message containing the action “ALARM_GOING_TO_RING” with the alarm label “get_up_work” is received.

- alias: 'Wake up light'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ "ALARM_GOING_TO_RING" in trigger.payload_json.action }}'
      - condition: template
        value_template: '{{ "get_up_work" in trigger.payload_json.label }}'    
  action:
    - service: script.sunrise

Power on morning devices

This automation is triggered as soon as I disable the Alarm displayed when it is ringing on the phone. Again the conditions are a MQTT message containing the action “ALARM_DISABLED” with the alarm label “get_up_work”.


- alias: 'Alarm power on morning devices'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ "ALARM_DISABLED" in trigger.payload_json.action }}'
      - condition: template
        value_template: '{{ "get_up_work" in trigger.payload_json.label }}'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.select_activity
        option: "Nicht gesetzt"
    - service: input_select.select_option
      data:
        entity_id: input_select.select_activity
        option: "Aufstehen"
    - service: scene.turn_on
      entity_id: scene.comfortable

Download

Releases

20.02.20 broadcasts_v1.0.1-(5.1.0)
Changelog:

Update About section
New Feature: Enable/Disable, Update and Create alarms using intents
Change versioning scheme
Update Readme

16.02.20 [Release_v0.0.2]
Download: broadcast_v0.0.2.apk
Changelog:

Fixed issues with Android 10
Synced upstream
Convenience function to automatically adjust all silent alarms relative to a new alarm, when changed    (prompt appears after changing alarm time)
All silent alarms of an alarm now have the same label of the triggered alarm

19.10.2019 Release_v0.0.1

Initial release

Source

Feedback

Please let me know if this is useful to you. I am open to ideas on how this can be improved. Furthermore, I am not responsible for missed alarms etc. usage at your own risk, this is the first release and may contain bugs, however it is working fine for me :slight_smile: .

Feel free to create issues and feature requests in the git repository.

10 Likes

Hi! It’s an awesome idea! Mega usefull tool. I’ve installed your .apk file but i can’t see any MQTT-relevant settings in the app (like Server address, credentials). Where am i able to set them?
Tnak you:)

Hi there! Great, I’m happy you find my app idea useful!

I did not want to bloat the alarm clock with a mqtt client, so the app emits events which other apps can listen to. For example, you can use tasker + its mqtt plugin to process the event, which I am currently doing.

In the Tasker app you can paste the event e.g. com.simplemobiletools.ALARM_GOING_TO_RING you wish to listen for and then forward it to home-assistant using the mqtt plugin of tasker.

I hope the explanation helps :slight_smile: , if you have more questions don’t hesitate to ask!

Oh, awesome! I’ve got it:) I also use Tasker, so it will be an easy game to grab the events and forward them with Tasker over MQTT. Thanks for your help!:slight_smile:

@lihuuhloi thanks for sharing your work, looks nice.
Can you share your automations with us? More examples on topic would be nice.

1 Like

Sure, here are example automations. Most of them are not final as I only have limited time to tinker however, they do work well :slight_smile: :

# Wakeup light          
- alias: 'Wake up light'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
  condition:
      condition: template
      value_template: '{{ "ALARM_GOING_TO_RING" in trigger.payload_json.action }}'
  action:
    service: shell_command.sun_rise
#power on morning devices 
- alias: 'Alarm Power on morning devices'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
  condition:
      condition: template
      value_template: '{{ "ALARM_DISABLED" in trigger.payload_json.action }}'
  action:
    - service: input_select.select_option
      data:
        entity_id: input_select.select_activity
        option: "Nicht gesetzt"
    - service: input_select.select_option
      data:
        entity_id: input_select.select_activity
        option: "Aufstehen"
    - service: scene.turn_on
      entity_id: scene.comfortable
      #
# Test Payload
- alias: 'test mqtt payload'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
  condition:
      condition: template
      value_template: '{{ "ALARM_SET" in trigger.payload_json.action }}'
  action:
    service: shell_command.sun_rise
    - service: notify.notify_telegram
      data_template:
        message: > 
          '{{trigger.payload_json.id}} Planned Alarm going to ring at: {{ trigger.payload_json.minutes }}m 
          {{ trigger.payload_json.minutes // 60 }}:{{ (((trigger.payload_json.minutes / 60) 
            -(trigger.payload_json.minutes // 60))*60)|round|int }}'

Great, thank you.
But why you dont simplify your automations with payload?
Example:


# Wakeup light          
- alias: 'Wake up light'
  trigger:
    - platform: mqtt
      topic: 'android/broadcast/redmi-note-3'
      payload: YOUR PAYLOAD
  action:
    service: shell_command.sun_rise

Nice - I will have a look at the payload options - I simply was not aware that the payload attribute existed :slight_smile: .

I’d love to see a written or video tutorial on how to set this up. I’ve not yet worked with mqtt in Home assistant, but this looks like something I’ve been looking for…

Hi glad to see hear you’re interested in my project. I’ll try to make a setup video as soon as I have some spare time. However, setting up MQTT in Home-Assistant would be overkill, there are plenty of tutorials out there and MQTT is well documented here too :slight_smile: . If you have issues don’t hesitate to ask.
In a nutshell, you need to configure a MQTT server in your configuration file, I believe Home Assistant also provides an integrated one, thus reducing effort in the setup Using the embedded MQTT broker.
Once you have the server running your devices e.g. can connect to it and can communicate with Home Assistant using MQTT. I have provided example automations in the first post on how you can work with the messages used by my project.
I hope this helps in some way :slight_smile:.

Really love this component! I’ve been using my alarm to trigger automations for years now. I’m surprised more people don’t use it!

Is there any way to allow Tasker to adjust the time the alarm is set for? My current use case and issues I have…

Currently, when I plug my phone in at night (technically when my battery changes) Tasker evaluates the state of my alarm, and gets the (%M) minutes until my alarm, then I have Tasker do math and subtract 30 from %M (%M-30=%A) to get an adjusted time. Tasker then waits %A minutes and calls a webhook which runs a script to adjust my heaters, waits 30 minutes, then turns my lights on. I do it this way because one, my alarm actually sounding has not triggered anything in years, I guess it’s broken, and two, I like the heaters to come on before hand. But, there’s a caveat. A few times a month I have various guys come to the house to do yard work. This requires me to get up a few minutes early to clear the yard of toys and things and make sure the dogs have been let out as I have to lock up the doggy door for the day. So I use autonotification and Tasker to intercept their email saying they’re coming and adjust my alarm, which is different on different days of the week.

So, can Tasker adjust your alarm clock on the fly? Thanks again for making this! I’m probably still going to install and check it out, I really like the idea of the snooze mode and can really take advantage of that!

Hi! It’s always great to hear that someone else than myself has interest in this! I also really love reading about the use-cases people have to automate stuff!

  • The possibility to change an alarm using tasker should be doable - I’ll gladly look into it as soon as I have a spare minute!
  • I’m not sure if I have misread: Is a feature broken for you?
  • Another new feature I forgot to mention in the change log is: that now all silent alarms of an alarm also have the same label as the parent alarm, thus you can associate individual alarms

My regular, native alarm doesn’t trigger Tasker events anymore. I’ve tried and tried but I couldn’t get it to work. I think it broke with a new version of Android. But it’s kinda of ok because looks I said, I want to trigger events in the house 30 prior to the alarm anyways. Yours can do both, trigger on the alarm and a predetermined amount of time before the alarm. I love it!

I’m really happy you’re enjoying my app that much :slight_smile: - that encouraged me to implement your nifty idea: you should be able to control alarms using intents now - check out the readme on github for detailed instructions.

I can’t get the app to work properly, due to my lack of knowledge of Tasker.
I have installed the broadcast app and configured some alarms.
Now I want Tasker to pickup the events and send an MQTT message to HA.
What event in Tasker is used to obtain the broadcast message?
I tried the intent recieved event, but it does not detect the changes in het broadcast app.

Can someone explain how to recieve the messages and trigger a Tasker event?