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

1 Like

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