Ability to turn off/on tablet screen according to HA event

Hi,

i have tablet with HA on it, and i have motion sensor in this room,
i want to add functionality that if motion sensor is on the screen of the tablet is on
and if no motion in room the screen will be off.

someone try to do it ?

I reckon this user has what you need:

This topic is old, but it’s the top Google search result for “home assistant tablet turn screen off.” With the advent of the official Home Assistant Android app, there’s a better way to do it nowadays using notification commands.

Android doesn’t allow you to turn off a device’s screen without ADB, and ideally I don’t want to mess around with ADB.

As a fallback, I send the device to the homescreen, which has a 15 second sleep timer (the companion app has a setting you can set to force the screen to stay on when a webview is open, so we don’t need to worry about it otherwise). There is one small snag with this approach: as far as I can tell, there’s no way for Android to magically detect the default launcher for the homescreen (or rather, there is in Java, but I couldn’t get this method working in the companion app). However, if you know the name of your launcher (and aren’t planning on changing it), this can be worked around.

Here’s a template switch which will send the device to the homescreen when the switch is turned off (then the screen will turn off 15 seconds later). When the switch is turned on, it will open the Home Assistant app to the default page. My device uses the Pixel Launcher (com.google.android.apps.nexuslauncher); you will need to work out what the package name for your launcher is, otherwise this won’t work.

switch:
  - platform: template
    switches:
      kitchen_tablet_screen:
        value_template: "{{ is_state('binary_sensor.tablet_interactive', 'on') }}"
        turn_on:
          - service: notify.mobile_app_kitchen_tablet
            data:
              message: "command_screen_on"
          - service: notify.mobile_app_kitchen_tablet
            data:
              message: "command_webview"
        turn_off:
          service: notify.mobile_app_kitchen_tablet
          data:
            message: command_activity
            data:
              tag: "android.intent.action.MAIN"
              channel: "com.google.android.apps.nexuslauncher"

Note that the HA Companion app will need the “draw over other devices” permission for this to work. You also need to disable your device’s lock screen.

1 Like

I know this is old but hopefully someone can still help…

I got this working with the HA app like Jay explained but, the problem is that when the tablet has been idle for to long it doesn’t wake up with the commands. It just works if I used the tablet recently. It’s like the tablet goes into hibernation when not used… once I turn on the screen manually all the commands that where sent meanwhile are executed all at once… I already deactivated battery optimization on the HA app so I’m out of ideas…

p.s. it’s a Samsung Tab A7 with Android 11 and One UI 3.1

Thank you for sharing this, this was super helpful. For anyone looking to figure out what the command for their launcher is, I found the easiest way to do this was run adb shell cmd package list packages and search the list of packages for anything named launcher and try them until I found the right one.

If it’s helpful for anyone in the future, I found the following for my Lenovo and Fire tablets:

switch:
  - platform: template
    switches:
      lenovo_tablet_screen:
      unique: lenovo_tablet_screen
        value_template: "{{ is_state('binary_sensor.lenovo_tablet_interactive', 'on') }}"
        turn_on:
          - service: notify.mobile_app_lenovo_tablet
            data:
              message: "command_screen_on"
          - service: notify.mobile_app_lenovo_tablet
            data:
              message: "command_webview"
        turn_off:
          service: notify.mobile_app_lenovo_tablet
          data:
            message: command_activity
            data:
              tag: "android.intent.action.MAIN"
              channel: "com.tblenovo.launcher"
switch:
  - platform: template
    switches:
      fire_tablet_screen:
      unique: fire_tablet_screen
        value_template: "{{ is_state('binary_sensor.fire_tablet_interactive', 'on') }}"
        turn_on:
          - service: notify.mobile_app_fire_tablet 
            data:
              message: "command_screen_on"
          - service: notify.mobile_app_fire_tablet 
            data:
              message: "command_webview"
        turn_off:
          service: notify.mobile_app_fire_tablet 
          data:
            message: command_activity
            data:
              tag: "android.intent.action.MAIN"
              channel: "com.amazon.firelauncher"
1 Like