I use Sleep As Android for both sleep tracking and my alarm clock. It works very well, combining these features into a “Smart wakeup period” that sounds your alarm at the optimal time in your sleep cycle to help you wake up. Beyond this, it has a lot of great features such as support for wearables and Tasker. There’s a small fee for the app, but it’s easily one of the best purchases I’ve made on Google Play.
Up until now, I’ve used Sleep as Android to trigger Tasker when my alarm goes off, and Tasker then triggers openHAB activities. The developers recently added MQTT to the list of integrations, so I decided to see how I could use it with openHAB. And as a result, I’ve cut Tasker out of the loop.
What can it do?
Sleep as Android can publish 26 different events, from “Sleep tracking started” and “Baby crying” to “Alarm dismissed” and “Snooze clicked”. It’s an impressive list, but personally all I care about is “Alarm started”. Smartly, the developers have made it so that you can select which events will get published.
Something that others might like is the “Smart period” event. This is a period of time before your alarm goes off when Sleep As Android starts collecting data to determine when it should wake you up. If you like the idea of turning on your thermostat or starting your coffee before you wake up, this could work well for you.
You could also set this up on multiple phones with different MQTT Topics and Client IDs, so that openHAB can distinguish between two different alarms. So if you and your significant other wake at different times, openHAB could react differently for each of you.
Unfortunately, Sleep As Android can only publish events to MQTT. It can’t receive commands through MQTT at this time, but I’ve asked the developers if this might be possible in the future.
Prerequisites
I’m running OH3, and this tutorial reflects that. If you’re running OH2, ask for help to get it working on your system. In either case, you’ll need:
- A working MQTT broker (I’m using Mosquitto on my openHABian RPi4)
- JSONPath Tranformation installed
Setting up Sleep As Android
- In Sleep As Android, go to Settings > Services > Automation
- Click the checkbox to enable MQTT.
- Fill in the URL to point it to your MQTT broker and provide credentials (if required).
tcp://username:password@openhab:1883
.
In this case, “openhab” is the name of my RPi4. You could also use the IP address (e.g. 192.168.1.xxx). 1883 is the port.
Note that there’s also a secure option, in which case you would replace tcp
with ssl
. However, that’s out of scope for this tutorial (translation: I don’t know how to do any of that).
If you want to send events from multiple phones, then change Client ID to be unique for each device.
Create a thing in openHAB
Add a Generic MQTT thing in openHAB, attached to your MQTT broker. I’ve used SleepAsAndroid
as the Unique ID.
Now, create a single channel with the following parameters:
- Channel identifier:
Event
- Label:
Event
- Channel Type:
Text Channel
- MQTT State Topic:
SleepAsAndroid
The MQTT Command Topic is blank, since we can’t send commands to Sleep As Android.
Create an item
Finally, link the channel to a new string item. I’ve used the bedroom
category for mine, because it kind of makes sense.
You now have a working item to receive events from Sleep As Android. Open the app on your phone, go back to MQTT settings, and press the “Test” button. You’ll get this in your log:
Item 'SleepAsAndroid_Event' changed from NULL to {"event":"Unknown"}
That’s great, but it’s kind of ugly. And it gets uglier when Sleep As Android sends an actual event:
Item 'SleepAsAndroid_Event' changed from {"event":"Unknown"} to {"value1":"1611764100000","value2":"","event":"alarm_alert_start"}
All we really need is the alarm_alert_start
text in the event, so let’s fix that.
Transform the state with JSONPath
- Go back to your item, then click on the
Event
channel. - Scroll down to Profile and select
JSONPATH
. - In the “JSONPath Expression” field, type
$.event
. - Save the change.
This will extract the data for the event
and ignore everything else. Once again, click the “Test” button in the app. You should get this:
Item 'SleepAsAndroid_Event' changed from {"value1":"1611764100000","value2":"","event":"alarm_alert_start"} to Unknown
And the next time your alarm goes off, you’ll get alarm_alert_start
.
What’s next?
From here, I’ll leave it to you to decide what you do with Sleep As Android. I use it to trigger my morning routine, which includes turning on my bedside light if it’s dark, turning on the thermostat if it’s cold, and casting a radio station to my Chromecast if it’s a weekday.
It’s worth noting that this will only work if your phone is on the same network as your MQTT broker. Personally, that’s exactly what I want. It means that my morning routine will only run when I’m at home, and not when I’m somewhere else.
I would love if the developers added a Command Topic so that I could also start sleep tracking from openHAB. For that, I’m still using Tasker as an intermediary.
I’ve also asked the developers how the value1
, value2
, and value3
codes are generated, as these might be useful for distinguishing between different alarms on a single phone (e.g. your weekday and weekend alarm). If they’re random, perhaps the developers will add add an alarm identifier to the payload.
I hope you found this interesting and/or useful!