MQTT - Sleep as Android

On every event, Sleep as Android will publish a message to SleepAsAndroid topic in the following format:


This is a companion discussion topic for the original entry at https://docs.sleep.urbandroid.org//services/mqtt.html

I have configured Sleep as Android to send events to my MQTT broker using both a ā€˜remote’ URL and a local IP address. Sleep as Android appears to send events to the broker ie a notification appears showing the event being sent to MQTT; however it doesn’t seem to be received at the broker and no error messages are displayed. Has anyone got MQTT working?

Hello Benjamin, big thanks for reporting this. We did not yet test the MQTT implementation on a local server here, can you please use menu - report a bug after the event is send by SaA? That would be very helpful.

Hello Benjamin, I have now tested with a local mosquitto instance and it worked… I think the problem is the URl needs to have protocol tcp or ssl not mqtt as stated in the hint… I’m fixing this for the next BETA release, which I’m going to release now… there was also a crashing bug on disconnect which I’m fixing too so please update…many thanks for your feedback…

Hi Petr. Thanks for the update! I’ve submitted a new bug report as SaA it’s now working with a URI like tcp://192.168.0.254:1833 but not tcp://user:pass@host.ddns.net:1883 … Ben

1 Like

@petr-urbandroid, I’ve just set up the MQTT integration to use with my openHAB home automation system, and it works great. I’ve written a tutorial for other openHAB users.

Is there a possibility of sending commands to Sleep as Android over MQTT in the future?

Thanks!

1 Like

Hello, big thanks for the guide… this is great! Can we include a link into our documentation? We will look into feasibility of a listener for MQTT command son the SaA side although I see some technical issues here… big thnaks…

You can definitely include it here. I’ve already made some edits to improve it.

I’d be happy to test MQTT commands if and when you’ve got something to try. If there are concerns about the impact on battery, perhaps it could be set to only listen when the device is charging?

New question for you: how are the value1, value2, and value3 codes generated?

It occurred to me that it would be valuable to be able to distinguish between alarms, and perhaps the values do that in some way. If not, it would be great if an alarm identifier could be added to the MQTT payload.

I had a connection with mqtt on my homeassistant yesterday however for some weird reason it didn’t show up today anymore. I hadn’t changed anything in the settings. I used ā€œtcp://homeassistant:[mypassword]@[myduckdns].duckdns.org:1883ā€ and it was publishing fine on it’s topic. Has there been an update breaking it or is it just me being stupid?

Mqtt works great on my OnePlus, but it often gets stuck on my wife’s Pixel 3XL. Randomly throughout the night, the MQTT notification gets stuck and doesn’t go away unless I force stop the app. After it gets stuck it doesn’t send anymore events. Any suggestions on how to fix this? I’ve already checked the power/battery management settings

Hello @sgobat, can you please use menu - report a bug when this happens to give us some details?

For some reason everything is working again. Not sure if you’ve patched it but if so. Thanks :slight_smile:

Hey,
when i activate my sleep tracking it first fires:
{"event":"sleep_tracking_started"}
and instantly after that:
{"event":"sleep_tracking_paused"}
and:
{"event":"not_awake"}
when still the 1min timer runs down.
This makes no sense because i am still ā€œawakeā€ when the timer runs or when i extended the timer. Instead of ā€œnot_awakeā€, ā€œawakeā€ should be fired when the timer starts.

Also when i turn on my screen again it fires:
{"event":"not_awake"}

I never saw:
{"event":"awake"}
Maybe this only shown after a longer time when moving the phone or something.

A nice feature would be to send the next activated alarm, so i can for example activate my ceiling light few minutes before the alarm starts.

Edit:
Also tested with the newest beta, it fires another sequence of events:
{"event":"sleep_tracking_started"}
like the normal version but then:
{"event":"not_awake"}
and after the 1min timer runs out
{"event":"sleep_tracking_resumed"}

For me this looks worse then the normal version, because its resumes without pausing it first.
Please fix it and think about my feature suggestion ^^

Hello, many thanks I’m preparing a fix for the immediate firing of the not_awake event… the reason is it takes some time (1-2 min and more) before the Awake heuristics get into a confident awake state…

Edit:
Also tested with the newest beta, it fires another sequence of events:
{"event":"sleep_tracking_started"}
like the normal version but then:
{"event":"not_awake"}
and after the 1min timer runs out
{"event":"sleep_tracking_resumed"}
For me this looks worse then the normal version, because its resumes without pausing it first.

Please fix it and think about my feature suggestion ^^

Little bit more info about the beta version behavior added. Thanks for quick answer @petr-urbandroid ^^

Our mqtt broker password has characters that are not acceptable in a URL, and I am not able to change the password. Is there an alternative way to provide the password?

Hello @Will_Freeman I guess you can simply URL encode the password? Please see https://www.urlencoder.org/

Hello! Do you have a list of the event name descriptions? Tasker has broken so I was trying to get mqtt to talk straight to node red. I want to set a lights event when sleep smart period starts. I suspect event is called sleep_smart_period? What other events can I use? When are they called and the name? I’d like one for when I dismiss my alarm too.

Thanks! You are an amazing programmer and I bought the unlock tonight. You have 1M supports so you should be doing well! Thanks again.

Hello Josh, Tasker should be working well, but you need to explicitedly enable Tasker support in Settings > Services > Automation > Tasker… in the same scree you see all events and their description…