Simple Android TV integration by Tasker and MQTT

For display message on the screen you can use AndroidTV notify component https://www.home-assistant.io/components/notify.nfandroidtv/

1 Like

Thank you so much. Great.
What is your recommendation if I want to push MQTT messages into the TV?

I am trying to run particular video (or a playlist) on youtube when launching the app but I am unsuccessfulā€¦ Can anyone help with this ? I would love to launch playlist when opening youtube appā€¦ or at least a videoā€¦

Thank you so much for this. I was able to use this tutorial to automate my baby monitor with tinycam pro to show up on the TV as needed and to hide it after. Pretty simple to do once you understand the workflow :slight_smile:

1 Like

For anyone else having problems getting %WIN to work with a Nvidia Shield. Check your Tasker Preferences and make sure under Monitor settings, App Check Method is set to Accessibility. This was set to App Usage Stats for me and that didnā€™t seem to work.

I just got a USB ethernet adapter for my MiBox and found, to my annoyance, that I couldnā€™t have USB (and network) debugging enabled and use the Ethernet adapter at the same time. So I had to figure out another way to connect them.

I took cues from this post, thanks, and adjusted a few bits. I managed to squeeze a little more out of this combination.

This setup can:

  • Get available sources (apps) from Android TV
  • media_player.select_source can be used to select a source from the list
  • Android TV can show notifications (but as toasts, so a bit different from what you get with the androidtv notification platform)
  • Shows ā€˜idleā€™ state when no app is running (sitting at launcher)

Hereā€™s the tasker profile:

    Profile: App Launched (2)
    	Restore: no
    	Event: App Changed [ Output Variables:* Package:* ]
    Enter: Store App Name (9)
    	A1: Variable Set [ Name:%CurrentApp To:%app_name Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    
    Exit: Send HA Update (7)
    	A1: Variable Set [ Name:%app_name To:%CurrentApp Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A2: If [ %app_name eq Android TV Home ]
    	A3: Variable Set [ Name:%state To:idle Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A4: Else 
    	A5: Variable Set [ Name:%state To:on Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A6: End If 
    	A7: JavaScriptlet [ Code:var data = {
        "state": state,
        "attributes": {
        "source": app_name,
        "source_list":["Plex","Kodi","Netflix","YouTube","Les Mills"]
    }
    }
    
    var state = JSON.stringify(data); Libraries: Auto Exit:On Timeout (Seconds):45 ] 
    	A8: Publish MQTT Message [ Configuration:hassio - mqtt.local : house/rooms/livingroom/mibox/state : %state : 0 : false Timeout (Seconds):0 ] 
    
    Profile: MQTT Command Arrived (5)
    	Restore: no
    	Event: Message Arrived [ Configuration:hassio - mqtt.local : house/rooms/livingroom/mibox/command/# : payload : topic :  :  ]
    Enter: Execute MQTT command (6)
    	<Strip topic prefix>
    	A1: Variable Search Replace [ Variable:%topic Search:house/rooms/livingroom/mibox/command/ Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In Array: Replace Matches:On Replace With: ] 
    	A2: Launch App [ Package/App Name:%payload Data: Exclude From Recent Apps:Off Always Start New Copy:Off ] If [ %topic eq set_source ]
    	A3: Flash [ Text:%payload Long:Off ] If [ %topic eq notify ]
    
    Profile: Every 2 minutes (8)
    	Restore: no
    	Time:  Every 2m
    Enter: Send HA Update (7)
    	A1: Variable Set [ Name:%app_name To:%CurrentApp Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A2: If [ %app_name eq Android TV Home ]
    	A3: Variable Set [ Name:%state To:idle Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A4: Else 
    	A5: Variable Set [ Name:%state To:on Recurse Variables:Off Do Maths:Off Append:Off Max Rounding Digits:3 ] 
    	A6: End If 
    	A7: JavaScriptlet [ Code:var data = {
        "state": state,
        "attributes": {
        "source": app_name,
        "source_list":["Plex","Kodi","Netflix","YouTube","Les Mills"]
    }
    }
    
    var state = JSON.stringify(data); Libraries: Auto Exit:On Timeout (Seconds):45 ] 
    	A8: Publish MQTT Message [ Configuration:hassio - mqtt.local : house/rooms/livingroom/mibox/state : %state : 0 : false Timeout (Seconds):0 ] 

The universal media_player and template sensor yaml:

media_player:
  - platform: universal
    name: Livingroom MiBox
    attributes:
      state: sensor.livingroom_mibox
      source_list: sensor.livingroom_mibox|source_list
      source: sensor.livingroom_mibox|source
    commands:
      select_source:
        service: mqtt.publish
        data:
          topic: house/rooms/livingroom/mibox/command/set_source
          payload_template: "{{ source }}"
          retain: false

sensor:
  - platform: mqtt
    name: "Living Room MiBox"
    unique_id: media_player.livingroom_mibox
    state_topic: "house/rooms/livingroom/mibox/state"
    value_template: "{{ value_json.state }}"
    json_attributes_topic: "house/rooms/livingroom/mibox/state"
    json_attributes_template: "{{ value_json.attributes | tojson }}"

Prereqs:

  • Set the following variables in Tasker:

Adjustments:

  • Edit the source_list to add your own apps.
  • If you adjust the command topic, edit the ā€œStrip topic prefixā€ node in the Execute MQTT Action task accordingly