Official raspberry touchscreen turn on/off from hass

Hi,

I have been trying to set a shell command to turn on/off the screen of my raspberry pi with the official touchscreen. The command shell calls a python script:

pantalla_on: python home/homeassistant/.homeassistant/python_scripts/modo_pantalla.py '0'

and the python script:

import os, sys

def turn_on_off_screen(mode):
    os.system('sudo bash -c "echo ' + str(mode) + ' > /sys/class/backlight/rpi_backlight/bl_power"')

if __name__ == '__main__':
    exec('turn_on_off_screen(' + sys.argv[1] + ')')

I tried to call that python script in the terminal and it works; “python modo_pantalla.py ‘0’” turns the screen on and “python modo_pantalla.py ‘0’” turns it off but when it is called by the shell_command it does not work and I get this message:

Error running command: `python home/homeassistant/.homeassistant/python_scripts/modo_pantalla.py '1'`, return code: 1
NoneType: None

Any idea?
Thanks!

Here are some tips about shell commands.

Why do use this bash command in a python script?

Thanks VDRainer.

Why do use this bash command in a python script?

To be honest, because for me it was the easiest way to execute that command. I am not good on linux :persevere:
I will keep trying to find the way to disable the touchscreen.

Finally I found the way to fix it :partying_face:

First I did what you suggest, instead of calling a python script I run it directly in the shell command:

pantalla_on: 'echo 0 | tee /sys/class/backlight/rpi_backlight/bl_power'
pantalla_off: 'echo 1 | tee /sys/class/backlight/rpi_backlight/bl_power'

The real problem was in the permissions of the bl_power file, to fix it we need to add/edit the following file:

sudo nano /etc/udev/rules.d/backlight-permissions.rules

And add this line:

SUBSYSTEM=="backlight",RUN+="/bin/chmod 666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"

That give the proper permissions to change the status of the screen.

2 Likes