I use the Sleep as Android App to track my sleep every day. It’s an amazing app with a great UI and integrates with a lot of stuff like Tasker and Home Assistant. Since I run a Home Assistant instance at home, I decided to go with the later.

Integrating Sleep as Android with Home Assistant is done using MQTT. We setup an MQTT server in Home Assistant. If you’re running HassOS, it’s as simple as installing the MQTT add-on from the Add-On store. Setup a username and password for the MQTT server and it should be up and running.

home-assistant-mqtt

The next is to install the Sleep as Android integration for Home Assistant. You can find it here. If you’re running HACS, then this step should be really easy.

You also need to setup the Sleep as Android integration in Home Assistant. Configure the integration by going to ‘Devices & services’ in Home Assistant settings and enter the MQTT topic.

home-assistant-sleep-as-android-configuration

Now you need to configure the Sleep as Android app to integrate with your MQTT server. Go to settings in the app, Services, Automation and enable the ‘MQTT’ option. Fill in the details as requested.

sleep-as-android-mqtt-config

You can click the ‘Test’ option in the app to check whether your configuration is working correctly. If everything went well, you should be able to see a new sensor in Home Assistant named ‘SleepAsAndroid_*’ with the current state.

Next time when you go to sleep, start sleep tracking on the Sleep as Android app and Home Assistant should get the data from the app.

home-assistant-sleep-as-android-sensor

But we’re not done yet! Home Assistant is all about automations. So you need to write automations to make use of this data. I have a few different automations utilizing the data from the Sleep as Android app right now. One of them is my favourite and saves me a bit of time and a lot of electricity.

Automations

Sleep time automation

This triggers when it’s bedtime. Home Assistant automatically turns off all lights currently switched on in my room, except a small light bulb in the corner and my phone plays a soothing tune. The idea is (although I can’t say it actually works in my case) to train my mind to feel sleepy when the lights turn on and the music plays, kinda like Pavlov’s Dog.

Sleep tracking started automation

This event triggers when I’ve actually laid down in bed and start sleep tracking on the app. Home Assistant turns off all my lights and cuts power to my monitor, laptop and any other devices I don’t use when I’m asleep.

Snoring automation

This one is my favourite and the most useful one. Sleep as Android triggers a ‘snoring’ event when it detects snoring from the microphone in my phone (turn your weaknesses into strengths!). I utilize this event in two ways right now:

  • I usually fall asleep watching Twitch or YouTube on my phone. I often forget to lock my phone as I fall asleep. This drains the phone’s battery overnight and I miss my alarms, calls, meetings and stuff. To solve this (other than making sure to plug in my phone), I created a Tasker profile which listens to the snoring event from Sleep as Android. If it gets the trigger and phone is unlocked, it instantly locks it.
  • I use a smart plug to control the air conditioner in my room. I usually turn it on at bedtime and set it to ’turn off at sunrise’ or ’turn off after x hours’. But now, I set Home Assistant to automatically switch the air conditioner off when the snoring event fires from the Sleep as Android app. This saves me a ton in electricity (calculated using power consumption data) cost while not sacrificing my comfort since I’m already deep asleep.

Alarm time automation

This is pretty straight-forward. Sleep as Android publishes an ‘alarm start’ event. This triggers at the time you have set your alarm for in the Sleep as Android app. I’ve created an automation which will turn on a batten light in my room with a warm color temperature (trying to replicate sunlight) when this event occurs. The brightness of the light slowly increases till its at its maximum.

Sleep tracking stopped automation

Sleep as Android also publishes an event for when I physically turn off sleep tracking to denote that I’ve actually woken up. I’ve created an automation to turn power back on to devices I turned off when I started sleep tracking.