Oscar
October 5, 2018, 8:50pm
1
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.
Hi @augerl , on which OS runs your HA?
If HA runs in a virtualenv, you should test your script/command on the command line as HA user when venv is activated.
For Hassbian:
sudo su -s /bin/bash homeassistant
source /srv/homeassistant/bin/activate
There are several problems with the $PATH variable in the venv, so HA cannot find the commands.
Run your commands with the full path, so insteed of echo try /bin/echo for the command_on: statement and in the scripts.
You can get the full path with w…
Why do use this bash command in a python script?
Oscar
October 7, 2018, 11:38am
3
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
I will keep trying to find the way to disable the touchscreen.
Oscar
October 8, 2018, 8:01pm
4
Finally I found the way to fix it
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