Turn off screen of Android device

Hi, I have an Android smartphone with the Home Assistant Companion app installed. It’s battery charging circuit has failed, so I modified it to work without a battery.
I want to keep the screen on if there is movement in the room it’s in, and turn it off if there isn’t.
To keep the screen on (but at minimum brightness unless someone touches it), I use Caffeine Tile.
To turn the screen on, I use the command_screen_on notification command. (I have no screen lock set.)
To detect if the screen is on, I use the Interactive sensor.
However, I can’t figure out how to turn the screen off.

My template switch that I control with an automation:

  - platform: template
        value_template: >-
          {{ is_state('binary_sensor.term_pixel_2_interactive', 'on') }}
          service: notify.mobile_app_term_pixel_2
            message: command_screen_on
              # needed to make sure the notification is received after long
              # inactivity
              ttl: 0
              priority: high
        # TODO turn the screen off
          service: script.dummy
        icon_template: "mdi:cellphone"

Any suggestions?

there is no way to turn off the screen, just set a proper screen timeout in android settings and go by that

Thanks for the reply.

If I used the standard screen timeout, the screen would stay at standard brightness. I don’t want to cause burn-in of the AMOLED display. With that caffeine thing, the screen stays at minimal brightness (same as in the last seconds before it completely turns off when using the standard screen timeout) until I touch it, and then it goes to standard brightness.

Have you looked at the Tasker app? You can do all kinds of customizations and automations on Android.

Yes, I know Tasker. That’s my backup plan. I wanted to use only free and open source software if possible (and avoid adding my Google account to that device).

Another option you can look into is the Wallpanel app, which is also open-source or available on Google Play store. It supports MQTT where you can send commands to turn on the screen, but I wasn’t able to turn off the screen using MQTT. There is a feature to dim the screen by 99% after a set period of inactivity. It’s not exactly what you’re looking for but maybe you can make it work.

thanksmister/wallpanel-android: WallPanel is an Android application for Web Based Dashboards and Home Automation Platforms (github.com)

1 Like

Thanks, WallPanel seems to work quite well. I’ll experiment with it.

This may help or give you inspiration:

This basically (ab)uses the fact that playing a video on Android keeps the screen from turning off, right?
I don’t think this will help here, because it would also keep the screen at full brightness. I don’t want to do that because of AMOLED display burn-in.
Thanks for the suggestion though.

Yeah it (ab)uses iframes to keep the screen on, but you can also do that with command notifications or wallpanel mqtt. Command notifications need to be resent every x minutes depending on what time out you set on the tablet, and to turn it off, just dont use that caffeine tile and let the timeout timer elapse. Wake the screen whenever there is motion again, or the lights go on.

I use that method on out two tablets.

I send a message to the tablet with topic “turn off” and message “turn off” and tasker reacts on it to switch it off.
Then I have a boolean that is the condition of an automation, and the trigger is the screen interactive on.

When all that is combined, it’s a way to make your kids stop using the tablets (or phones).

I think I already have what I want with WallPanel, although it’s still quite a mess because I haven’t had time to completely move away from Home Assistant Companion yet.
I don’t use Caffeine Tile, and I have the screen timeout set to 5 minutes. I have an automation that sends command_screen_on every 290 seconds.
I still have the Home Assistant Companion app running in background, and use that to get the “interactive” sensor status and to handle command_screen_on notifications.
I have configured WallPanel to reduce screen brightness by 99% after 15 seconds. This way, the screen is at minimal brightness most of the time, and all I need to do to wake it up is to touch it.

Thank you all for your help.

Could you elaborate this (HA and tasker), cause I’m looking for that solution…

I’m currently busy but here is a description of part of the solution, the event.
Then the action is just screen off in Tasker.

Thanks, it works but…

I have a tablet wich should wake up and show me a camera when there is motion.

Your solution works when the tablet is alive or just in sleep mode…after about 1 minute in sleep mode it doesn’t wake up anymore. I turned off battery optimization for tasker and HA, but this doesn’t help.

In tasker I made a task wich does 3 things : turn on, call HA and give a beep. When the tablet doesn’t wake up and I manualy turn on the tablet, I hear the beep and HA is opened.

Any idea ?

No I don’t have any ideas

I think it’s about connection…

When I manual turn on my tablet, I see the HA app with a message “Lost connection…”. After that it reconnects and I get the beep.

Do you know a way to resolve this ?

Sounds like it’s a wifi sleep issue.
See if you can keep the wifi on even when the phone is in sleep

I use AUTOMATION. its foss, you can download from fdroid and you can turn off the screen usin a notification.

I got something working with the vanilla companion app; I’m able to replicate the screen on / screen off functionality, though this does involve a work-around on what I suspect is a bug in the command_screen_on command.

I use the following to turn off the Android screen (shown below) when occupancy is cleared, and turns on the screen (shown below) when occupancy is detected.

Screen On

This enables the always on screen then opens companion app (this is needed since sometimes when the phone goes to sleep the app is put in background).

  - service: notify.mobile_app_a12
      message: command_screen_on
        command: keep_screen_on
  - service: notify.mobile_app_a12
      message: command_webview
      data: {}

Screen Off

This part is tricky because I’m pretty sure there’s a bug. When the command_screen_on is set to default, the setting isn’t applied until either the app restarts or the user goes into settings (without doing anything) then goes back to dashboard.

The screen off timeout set to 1 ms (or something really low; assuming your device has support), is needed for the screen to actually turn off around the time requested.

The sending of command_webview essentially re-opens the app, effectively applying the bug work-around described above.

  - service: notify.mobile_app_a12
      message: command_screen_off_timeout
        command: 1
  - service: notify.mobile_app_a12
      message: command_screen_on
        command: keep_screen_off # Per the doc, this can be any arbitrary command to set back to the default value.
  - service: notify.mobile_app_a12
      message: command_webview
      data: {}
1 Like