HomeAutomation

Sleep automation with Mi Band and Home Assistant

Have you ever thought about using your Mi Band to help you automate stuff? Read this article and you will know how you can use your Mi Band sleep detection to trigger automations in your Home Assistant!

You will need:

  • Tasker
  • Notify & Fitness for Mi Band
  • A Mi Band
  • An automation which creates a webhook and then publishes your sleep status on a MQTT topic
  • A sensor based on the MQTT topic previously defined (it can be avoided for the purpose of this automation but I prefer to have it to keep track of the sleep status)
  • Tasker profiles which react to your sleep status

Create a sleep_status.yaml file in your packages directory with the following content.

Replace [YOUR_NAME] and [YOUR_WEBHOOK_ID].

homeassistant:

automation:
  - alias: [YOUR_NAME] Sleep Status
    initial_state: true
    trigger:
      - platform: webhook
      webhook_id: [YOUR_WEBHOOK_ID]
      condition: []
    action:
      - service: mqtt.publish
        data_template: 
          payload: "{\"sleepStatus\": \"{{trigger.json.sleepStatus}}\"}"
          topic: "sleepstatus/[YOUR_NAME]"

sensor:
  - platform: mqtt
    name: "[YOUR_NAME] Sleep Status"
    state_topic: "sleepstatus/[YOUR_NAME]"
    value_template: '{{ value_json["sleepStatus"] }}'

Take now your phone, open Tasker and setup the following profiles which will send your sleep status events (when you wake up and fall asleep) to your Home Assistant.

Replace [YOUR_HOME_ASSISTANT_DOMAIN_NAME] and [YOUR_WEBHOOK_ID]

<TaskerData sr="" dvi="1" tv="5.9.rc">
<Profile sr="prof61" ve="2">
<cdate>1574199690392</cdate>
<clp>true</clp>
<edate>1574529186304</edate>
<flags>8</flags>
<id>61</id>
<mid0>63</mid0>
<nme>MiBand Fell Asleep</nme>
<Event sr="con0" ve="2">
<code>599</code>
<Str sr="arg0" ve="3">com.mc.miband.tasker.fellAsleep</Str>
<Int sr="arg1" val="0"/>
<Int sr="arg2" val="0"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
</Event>
</Profile>
<Task sr="task63">
<cdate>1574199712492</cdate>
<edate>1574529096550</edate>
<id>63</id>
<nme>Send Asleep Payload</nme>
<pri>6</pri>
<Action sr="act0" ve="7">
<code>339</code>
<Bundle sr="arg0">
<Vals sr="val">
<net.dinglisch.android.tasker.RELEVANT_VARIABLES><StringArray sr=""><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0>%http_data
Data
Data that the server responded from the HTTP request.</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1>%http_file_output
File Output
Will always contain the file's full path even if you specified a directory as the File to save.</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2>%http_response_code
Response Code
The HTTP Code the server responded</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3>%http_headers()
Response Headers
The HTTP Headers the server sent in the response. Each header is in the 'key:value' format</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4>%http_response_length
Response Length
The size of the response in bytes</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4></StringArray></net.dinglisch.android.tasker.RELEVANT_VARIABLES>
<net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>[Ljava.lang.String;</net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>
</Vals>
</Bundle>
<Int sr="arg1" val="1"/>
<Str sr="arg2" ve="3">https://[YOUR_HOME_ASSISTANT_DOMAIN_NAME]/api/webhook/[YOUR_WEBHOOK_ID]y</Str>
<Str sr="arg3" ve="3">Content-Type:application/json</Str>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3">{ "sleepStatus" : "Asleep" }</Str>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Int sr="arg8" val="30"/>
<Int sr="arg9" val="0"/>
</Action>
</Task>
</TaskerData>
<TaskerData sr="" dvi="1" tv="5.9.rc">
<Profile sr="prof66" ve="2">
<cdate>1574200024388</cdate>
<edate>1574529166065</edate>
<flags>8</flags>
<id>66</id>
<mid0>65</mid0>
<nme>MiBand WokeUp</nme>
<Event sr="con0" ve="2">
<code>599</code>
<Str sr="arg0" ve="3">com.mc.miband.tasker.wokeUp</Str>
<Int sr="arg1" val="0"/>
<Int sr="arg2" val="0"/>
<Str sr="arg3" ve="3"/>
<Str sr="arg4" ve="3"/>
</Event>
</Profile>
<Task sr="task65">
<cdate>1574199886906</cdate>
<edate>1574529090100</edate>
<id>65</id>
<nme>Send Awake Payload</nme>
<pri>6</pri>
<Action sr="act0" ve="7">
<code>339</code>
<Bundle sr="arg0">
<Vals sr="val">
<net.dinglisch.android.tasker.RELEVANT_VARIABLES><StringArray sr=""><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0>%http_data
Data
Data that the server responded from the HTTP request.</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES0><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1>%http_file_output
File Output
Will always contain the file's full path even if you specified a directory as the File to save.</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES1><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2>%http_response_code
Response Code
The HTTP Code the server responded</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES2><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3>%http_headers()
Response Headers
The HTTP Headers the server sent in the response. Each header is in the 'key:value' format</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES3><_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4>%http_response_length
Response Length
The size of the response in bytes</_array_net.dinglisch.android.tasker.RELEVANT_VARIABLES4></StringArray></net.dinglisch.android.tasker.RELEVANT_VARIABLES>
<net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>[Ljava.lang.String;</net.dinglisch.android.tasker.RELEVANT_VARIABLES-type>
</Vals>
</Bundle>
<Int sr="arg1" val="1"/>
<Str sr="arg2" ve="3">https://[YOUR_HOME_ASSISTANT_DOMAIN_NAME]/api/webhook/[YOUR_WEBHOOK_ID]</Str>
<Str sr="arg3" ve="3">Content-Type:application/json</Str>
<Str sr="arg4" ve="3"/>
<Str sr="arg5" ve="3">{ "sleepStatus" : "Awake" }</Str>
<Str sr="arg6" ve="3"/>
<Str sr="arg7" ve="3"/>
<Int sr="arg8" val="30"/>
<Int sr="arg9" val="0"/>
</Action>
</Task>
</TaskerData>

Now, you can create a Node-RED flow which reacts to sensor.[YOUR_NAME]_sleep_status with Awake/Asleep value and executes your preferred stuff (e.g. turn off all the lights as soon as you fall asleep).

I have used my Mi Band 2 and it detects my sleep activity usually within 5 minutes so don’t use it for real-time automations.

Edit: it works on my Mi Band 4, too!

Let me know what automations you are going to create!