Adjust Brightness of Official Raspberry Pi Touchscreen with command line switch

I’m using a Raspberry Pi 3 with the official 7" touchscreen to run my Home Assistant and have it set up as an always on screen for a wall mounted control. I have it in my living room and would like to be able to dim the screen at night when watching our tv with the lights dim. I can set up the automation no problem but I cant get the command_line switch to work.

This command works through putty to adjust the brightness where n is the brightness level:
sudo echo n > /sys/class/backlight/rpi_backlight/brightness

How do i get it to work with Home Assistant?

Here is what I tried that did not work:
switch:

  • platform: command_line
    switches:
    screen_brightness:
    command_on: ‘sudo echo 250 > /sys/class/backlight/rpi_backlight/brightness’
    command_off: ‘sudo echo 10 > /sys/class/backlight/rpi_backlight/brightness’

If there is a way to do this with a brightness slider, that would be even better.

Sounds like what you need here is the Input slider component of HA, from there the automation example pretty much shows you how it works and how you could apply it to your scenario :slight_smile:

I read through the input slider component and I don’t see how it could help. Having a slider isn’t really the issue. My issue is with getting the command line to work. Once I get the command line working I might be able to use the input slider though.

I’ve created an easy to use mqtt service for the rpi so you can control it with a mqtt light in HA

2 Likes

The mqtt service works quite nicely, thank you for that!
Used Systemctl though, and added shebang to script :wink:

/etc/systemd/system/mqtt_backlight.service

Description=Backlight Mqtt daemon
After=network-online.target


[Service]
Type=simple
User=mqtt_backlight  #needs udev rules - else use root user
WorkingDirectory=/opt/mqtt_backlight
ExecStartPre=/bin/sleep 30
ExecStart=/opt/mqtt_backlight/mqtt_backlight.py

[Install]
WantedBy=multi-user.target

Could you helpme with the third step?

  1. edit config and enter your mqtt broker info and optional change the control and state topics you can use “${HOSTNAME}” in clientid, state_topic, command_topic, brightness_state_topic, brightness_command_topic and it will be replaced by the hostname of the device sudo nano /etc/rpi_screenbrightness_mqtt.conf

Its an empty file? Im opening this file but it is blank.

did you clone the latest version from github?
if the file does not exist then the install script did not run properly because it should copy the file there (cp ./assets/rpi_screenbrightness_mqtt.conf /etc/rpi_screenbrightness_mqtt.conf)
maybe run the install again and see if there are any errors

Hi Tobi,
FYI, your script doesn’t work for me on the latest Raspberry Pi OS.

pi@EntrancePlate:~ $ sudo ./rpi-screenbrightness-mqtt/install.sh

Installing dependencies…

==========================

Hit:1 Index of /debian trixie InRelease

Hit:2 Index of /debian trixie-updates InRelease

Hit:3 Index of /debian trixie InRelease

Get:4 http://deb.debian.org/debian-security trixie-security InRelease [43.4 kB]

Fetched 43.4 kB in 1s (59.1 kB/s)

All packages are up to date.

python3 is already the newest version (3.13.5-1).

python3 set to manually installed.

python3-pip is already the newest version (25.1.1+dfsg-1).

python3-pip set to manually installed.

git is already the newest version (1:2.47.3-0+deb13u1).

git set to manually installed.

Summary:

Upgrading: 0, Installing: 0, Removing: 0, Not Upgrading: 0

error: externally-managed-environment

× This environment is externally managed

╰─> To install Python packages system-wide, try apt install

python3-xyz, where xyz is the package you are trying to

install.

If you wish to install a non-Debian-packaged Python package,

create a virtual environment using python3 -m venv path/to/venv.

Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make

sure you have python3-full installed.

For more information visit http://rptl.io/venv

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.

hint: See PEP 668 for the detailed specification.

HI,

please edit install.sh and change line 18 to:
python3 -m pip install --break-system-packages .

and try again. let me know if this works.
cheers

I think that worked, at least I have the service running now. I cannot control the brightness yet though.
I tried through the command line and I had to adjust the path to /sys/class/backlight/10-0045/brightness, maybe that`s the issue?

Disregard, I discovered touchkio and that solves most issues I had.