Simple Switch to Turn On / Off the Screen of a Linux Machine you can ssh into

Just a simple switch I use for my Raspberry Pi Touchscreen dashboard. Should work on most *nixes I guess.

- platform: command_line
  switches:
    pi_bash_screen:
      command_on: ssh pi@[Pi IP] 'export DISPLAY=:0 && xset dpms force on'
      command_off: ssh pi@[Pi IP] 'export DISPLAY=:0 && xset dpms force off'
      command_state: ssh pi@[Pi IP] 'export DISPLAY=:0 && xset -q | grep "Monitor is" | cut -c14-17' | grep "On" > /dev/null

In the first instance, the switch may not recognise the current state of the screen, when the switch is toggled once it will behave normally from then on.

1 Like

For mine, I had to do use these commands, specific to my screen’s GPIO driver, but working without Xorg:

Turn off: sh -c "echo 1 > /sys/class/backlight/fb_ili9320/bl_power"
Turn on: sh -c "echo 0 > /sys/class/backlight/fb_ili9320/bl_power"
Read current status: exit $(cat /sys/class/backlight/fb_ili9320/bl_power)

(The first two need root access)

Note that you can simplify your commands: you don’t need to export the DISPLAY variable, but you can directly declare it in your xset call: export DISPLAY=:0 && xset dpms force on can be replaced by DISPLAY=:0 xset dpms force on.

1 Like

Thanks!

It might be my Pi touchscreen, but the command won’t work for me without the ‘&&’.

These commands do work remote (SSH), however i’m looking for a solution in a (local) Python script on my RPi.
I have a script running that toggles the screen ON / OFF by the state of a GPIO pin that is connected to a sensor.
The thing is, i can’t get around this error: xset: unable to open display ":0"