Simple Android TV integration by Tasker and MQTT

I have a Xiaomi Mi Box 4K with Android TV. Unfortunately, there is no way in HA to integrate with Android TV devices. I wanted to be able to run the application on the device straight from HA and receive information what the application is displayed on the screen.

I used Tasker and the MQTT Client for integration. You need to download the applications on your phone and save them as apk files, which you can install on your Android TV device using a network or pendrive. The mouse will be necessary to configure the application (I used the Bluetooth mouse).

I assume you have configured the MQTT broker and you know the basics of Tasker.

After installing MQTT Client, configure it according to the screenshots below.
Adding MQTT broker (I use Mosquitto running on my Raspberry Pi):


After connecting with MQTT broker send a message to check if everything works. I use the topic homeassistant/androidtv/run with the application name as a message to run the application.

Now we go to Tasker and create a profile that run applications:

This profile use event Message Arrived from plugin MQTT Client:

appname and action will be local variables, appname will contain MQTT message, action - MQTT topic.

Task for this profile (Run Netflix if %action contains run and %appname contains Netflix):

Now we create a profile sending to HA the name of the application that is on the screen.

The profile will be activated when the event Variable Set for the %WIN variable occurs. Variable %WIN is standard Tasker variable with name of the displaying screen/application. You have to enable accessibility service for Tasker (in system settings) so it can collect data about the currently displayed screen/application.

Task for this profile use Publish MQTT Message action from plugin MQTT Client:

The messaage is variable %WIN and I use topic homeassistant/sensor/androidtv-activity:

Now You have to add mqtt sensor to your HA configuration:

- platform: mqtt
name: 'AndroidTV Activity'
state_topic: 'homeassistant/sensor/androidtv-activity'

To run application on Android TV from HA you have to use mqtt.publish service:

- service: mqtt.publish
  data:
    topic: 'homeassistant/androidtv/run'
    payload: 'Netflix'

If you want to use the discovery for the sensor you need to create a profile with the event Device Boot, which will send message {"name": "AndroidTV Activity"} in the topic homeassistant/sensor/androidtv-activity/config with enabled option Retained and change topic for sensor state to homeassistant/sensor/androidtv-activity/state with enabled option Retained. This example use discovery_prefix: homeassistant.

17 Likes

Cool idea!

For my needs, I just wanted to be able to turn on the Shield TV and run Plex, so I used adb over network.

shell_command:
  start_plex_shield_tv: 'adb connect <ANDROID_TV_IP> && sleep 1 && adb shell input keyevent KEYCODE_WAKEUP && adb shell monkey -p com.plexapp.android -v 500 && adb kill-server'

edit:

I found out a better way to start Plex, the previous command sometimes showed a weird popup, this one works flawlessly, also, I added a command to restart the PMS:

shell_command:
  start_plex_shield_tv: 'adb connect 10.0.0.2 && sleep 1 && adb shell input keyevent KEYCODE_WAKEUP && adb shell am start -n com.plexapp.android/com.plexapp.plex.activities.SplashActivity && adb kill-server'
  restart_plex_media_server: 'adb connect 10.0.0.2 && sleep 1 && adb shell am force-stop com.plexapp.mediaserver.smb && adb shell am start com.plexapp.mediaserver.smb/com.plexapp.mediaserver.ui.main.MainActivity && adb kill-server'
1 Like

Great writeup! I really like the idea. I have a question, as after the Mi Box Oreo update the box is online even when it is in standby (I guess this is to be able to cast to it all the time). This makes it very hard to get the device’s correct state. Previously I pinged the Mi Box to check if it is on or off, this does not work any more.

I am not sure I fully understand the last sentence:

If you want to use the discovery for the sensor you need to create a profile with the event Device Boot, which will send message {“name”: “AndroidTV Activity”} in the topic homeassistant/sensor/androidtv-activity/config with enabled option Retained and change topic for sensor state to homeassistant/sensor/androidtv-activity/state with enabled option Retained. This example use discovery_prefix: homeassistant.

Are you talking about tracking the device’s state here? This will send a MQTT message when the device is turned on right? But will it also send a MQTT message when it is turned off? Or how do I track this?

No, this is for MQTT Discovery. If you want to track state of the Mi Box you may use Tasker variable %SCREEN (I’m not sure that this works on AndroidTV).

I clicked the link, but I am still not sure what the purpose is?
Can you explain what you use it for?

MQTT Discovery means you don’t have to manualy add sensors to HA config. With this HA add sensor automatically. It’s not related with device state.

I have configured the MQTT Client. It says that it is connected. However, I am new to MQTT and the topic says that there is no message received. Can anyone point me in the direction to figure this out? Thanks in advance for the help!

If you use RPi or another device with Linux you can install mosquitto_clients package and in the shell use command

mosquitto_sub -h broker_host -p port -u username -P password -t "#" -v

to subscribe all topics. This will show you that MQTT Client on Android TV works or not.

Cool idea!
How do you make sure your app gets up next time you reboot your shield?
Also - will mqtt listener work in the background?

Tasker and MQTT Client start with system and are woking in the background.

Yes, It worked for me quite OK, even after reboot.
Now the issue that got me stumbling is: how to turn Nvidia Shield on from HA?

WoL doesn’t work. Some people recommend ADB, some IR.
Any hints? Or verified ways of turning it on?

You can try this https://github.com/home-assistant/home-assistant/pull/16975

I’m able to get this to work with SSL turned off, but since I have Let’s Encrypt setup on my HA instance it would be great if I could use that. Turning on just ends up having the Mosquitto server start throwing the following errors:

1543593914: New connection from 192.168.1.1 on port 1883.
1543593914: Socket error on client <unknown> disconnecting.

I assume I’m missing a step to get this to work with SSL. Can someone point me in the right direction on this?

MQTT is working fine via non-SSL connection, so I decided to proceed as is for now.

I’m able to send messages to the MQTT sensors and they show the test status no problem, but I’m not getting any updates from Tasker. I’ve setup the %WIN profile as detailed above but it never triggers. I’ve granted Tasker accessibility but no luck.

Btw I’m attempting this on the Nvidia Shield TV.

Any help would be appreciated.

I tested this only on the Xiaomi Mi Box. Theoretically, it should work on Shield.

I managed to get the %SCREEN Tasker variable to work, but %WIN doesn’t seem to work on the Shield.

Still no luck connecting via SSL.

There is true integration on the way https://github.com/home-assistant/home-assistant/pull/19157
I tested this component and it works great.

1 Like

This looks awesome! I just got a shield and cannot wait for this!

This looks like a promising solution.

So far I have successfully allowed my Android TV to notify Home Assistant when the screen is on or off. I use this notification to trigger a Hue Bloom light to turn on and off behind the TV for ambiance.

Using the %WIN variable would be useful I suppose for dimming lights when pausing/playing a movie on any app like Netflix, Prime, etc. Unfortunately, I have not been able to test this.

I have an nVidia Shield and the %WIN variable is unusable. It remains blank. Enabling accessibility for Tasker results in no change in the %WIN variable being usable. Additionally, accessibility being enabled causes a single click/tap on an app to be registered as a long click/tap - a dialog appears asking of you want to open, view info, uninstall.

I will poke at this some more and possibly open a new post referencing this one. In the meantime, if anyone has any suggestions, let me know.

I appreciate your very clever idea. This idea opens some other door to me. Would you please let me know how is it possible to show MQTT messages as an On Screen Display message on Android TV ?
I have the same setup that you mentioned but I want to show the MQTT message as an OSD rather than run an application like Netflix.