Exec Binding --> Turn off Screen on Linux (xset dpms force off)

I’m running OH on Ubuntu Intel NUC attached to a touchscreen. I’d like now to turn off the screen with a rule. Unfortunatelly it does not work.

Switch item for testing

Switch swi_turn_screen_off

rule

rule "Turn Screen Off"  
 when  
     Item swi_turn_screen_off changed to ON 
 then  
    executeCommandLine("xset dpms force off")
 end

I also tried it with a separate sh file.

sh file (executable, /etc/openhab2/scripts/turnondisplay.sh):

#!/bin/bash
xset dpms force off

rule

rule "Turn Screen Off SH"
 when  
     Item swi_turn_screen_off changed to ON 
 then  
    executeCommandLine("/etc/openhab2/scripts/turnondisplay.sh", 5000") 
 end

Log entries:

2017-10-29 10:58:32.184 [ItemCommandEvent          ] - Item 'swi_turn_screen_off' received command ON
2017-10-29 10:58:32.186 [ItemStateChangedEvent     ] - swi_turn_screen_off changed from OFF to ON
2017-10-29 10:58:32.191 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'sh /etc/openhab2/scripts/screen_off.sh'

Has anyone an idea what’s going wrong? The script works if executed manually.

Try without adding sh at the beginning and only doing

("/etc/openhab2/scripts/screen_off.sh")

Also make sure your sh file permissions are correct. When you run manually are you root or openhab user?

General hint for debugging:

Log the output of the execute like this:

val output = executeCommandLine("/etc/openhab2/scripts/screen_off.sh") 
logInfo("Test", output)

That way you archieve more information about what is going wrong.

Thanks for your inputs! I’m using know a separate script file and a rule:

sh file (executable, /etc/openhab2/scripts/turnondisplay.sh):

#!/bin/bash
xset dpms force off

rule

rule "Turn Screen Off SH"
 when  
     Item swi_turn_screen_off changed to ON 
 then  
    executeCommandLine("/etc/openhab2/scripts/turnondisplay.sh", 5000") 
 end

It works now theoretically. Unfortunatelly there’s an issue with the users.

Openhab runs with the user “openhab”. The current session showing HABpanel runs with the user “viewer”.

The script ends with the following message:

unable to open display

This makes sense because the openhab user runs just the openhab service …

Any ideas or workarounds?

it works now with the following command:

sudo -i -u viewer xset -display ":0" dpms force off

So the openhab users switches to the user “viewer” owning the display session.

I needed to add the user openhab ALL=NOPASSWD: ALL to visudo.
Is it possible to allow openhab only executing the above sudo command? In my opinion it’s not really a specific command like “systemctl” … so it’s not as easy to restrict, is it?

Idea 1: So basically make a script that has the command and set this to visudo.

Idea 2: Ebable the user openhab to access the xserver. Not tested but found in German forum.

Only works till reboot.

xhost +si:localuser:openhab

Set this to the file .xsessionrc for permanent rights.

+si:localuser:openhab