[SOLVED] Using Exec binding 2.x to control screen brightness of 7" Raspberry Pi Screen

Hi All,

I successfully use the Exec Binding 2.x to restart and shutdown my Pi so I’m reasonalby familiar with the setup.

That said I’m really struggling to get my head around why I cannot send another command. I’ve read How to solve Exec binding problems but to be honest it’s still a bit above my head.

The command that works in terminal is
sudo bash -c "echo 50 > /sys/class/backlight/rpi_backlight/brightness"

My /etc/sudoers.d/common_users file contains the following:

openhab ALL = NOPASSWD: /sbin/reboot, /sbin/shutdown, /bin/bash, /sys/class/backlight/rpi_backlight/brightness

I created a thing in the PaperUI that contains the following (along with about 100 other tests :roll_eyes:) without success:

/usr/bin/sudo /bin/bash -c "echo 100 > /sys/class/backlight/rpi_backlight/brightness"

Could someone be so kind as to point me in the right direction as to where I’m going wrong…?

I assume it is something to do with my common_users file as the following does not work in terminal?

sudo -u openhab bash -c "echo 100 > /sys/class/backlight/rpi_backlight/brightness"

I feel I’m getting closer… I changed the ownership of the file I wanted to control to openhab

sudo chown openhab /sys/class/backlight/rpi_backlight/brightness

Now the above command works in terminal but when I paste it into the PaperUI as a run switch nothing happens.

I’ve tried the following:

sudo -u openhab bash -c "echo 100 > /sys/class/backlight/rpi_backlight/brightness"

sudo bash -c "echo 10 > /sys/class/backlight/rpi_backlight/brightness"

bash -c "echo 100 > /sys/class/backlight/rpi_backlight/brightness"

/usr/bin/sudo bash -c "echo 100 > /sys/class/backlight/rpi_backlight/brightness"

"echo 10 > /sys/class/backlight/rpi_backlight/brightness"

After much deliberating here How to solve Exec binding problems I found the issue was due to the binding functioning oddly. The solution was to create a shell script that sat on my pi which was then called in the binding.

My /etc/sudoers.d/common_users file contains the following:

openhab ALL = NOPASSWD: /sbin/reboot, /sbin/shutdown, /bin/bash

I created the shell script like this:

# Create file
sudo touch brightness50.sh

# Make executable
sudo chmod u+x brightness50.sh

# Open
sudo vi /home/pi/brightness50.sh

# Paste
sudo bash -c "echo 50 > /sys/class/backlight/rpi_backlight/brightness"

# Convert
sudo vim +'set ff=unix | x' /home/pi/brightness50.sh

# Run
sudo sh /home/pi/brightness50.sh

I then simply added this to the run in the paperUI

sudo sh /home/pi/brightness50.sh