Dim a LED with exec binding doesn't work

Hey there,
I’m trying to dim a simple LED using openHab. However, I’m not getting anywhere. So my idea is to use “switch” with different mappings to control the brightness of the LED (Off, low, middle, high) with the exec binding.

If I use the command “sudo python slider.py 18 HELL” everything works fine but with the buttons on openHAB nothing happens…

I’m quite new to programming and openHAB. Hope anyone could help me with my problem.

Best wishes Stephan

Here is my Code:

My Item:

String led1 'Dimmer 1' <light> {channel='exec:command:led1-control:input', autoupdate='true'} 

My thing:

Thing exec:command:led1-control [command="/etc/openhab2/scripts/slider.py 18 %2$s", interval=0, autorun=true]

My script:

import RPi.GPIO as GPIO

import sys

import time 

GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

if len(sys.argv) > 2:

    RELAIS_GPIO = int(sys.argv[1])# + int(sys.argv[2])

    GPIO.setup(RELAIS_GPIO, GPIO.OUT)

    p=GPIO.PWM(RELAIS_GPIO, 2000)
    p.start(0)
    if sys.argv[2] == 'AUS':
        p.ChangeDutyCycle(0)
        time.sleep(10)
    elif sys.argv[2] =='DUNKEL':
        p.ChangeDutyCycle(25)
        time.sleep(10)
    elif sys.argv[2] =='MITTEL':
        p.ChangeDutyCycle(50)
        time.sleep(10)
    elif sys.argv[2] =='HELL':
        p.ChangeDutyCycle(75)
        time.sleep(10)

my sitemap:

sitemap weatherstation label="Smart Garden"
       Frame label="Licht" {
           Switch item=led1 mappings=["AUS"="AUS", "DUNKEL"="DUNKEL", "MITTEL"="MITTEL", "HELL"="HELL"]
        }

OpenHAB Basic UI:
Bildschirmfoto 2020-04-30 um 12.05.41

There are lots of posts about exec binding, and stuff to investigate. Often it comes down to permissions.

A diagnostic aid - see what the response is updated to, if anything

String led1exec 'Exec response' {channel='exec:command:led1-control:output'}

There is no response to the String led1exec line at all. I also added the line

/etc/openhab2/scripts/slider.py 18 %2$s

to my misc folder in the exec.whitelist File to give the exec binding the permissions. The log tells me what I want to hear but nothing happens to my LED:
“led1 changed from AUS to DUNKEL”

Don’t forget we cannot see that.

Also note what it says in exec docs - when using autorun, the exec only runs if the command to input is different from the last command. It’s an unexpected quirk. The binding docs show how to workaround that with the run switch if needing to send the same command twice.

Test your file permissions out with a rule

rule "exec diagnostic"
when
   Item led1 received command
then
   Thread::sleep(1500)
   val result=executeCommandLine("/etc/openhab2/scripts/slider.py 18 DUNKEL", 1000)
   logInfo(“exectest”, “Command line result: {}”, result)
end

No whitelist action is needed here

EDIT - Fix the quotemarks

logInfo("exectest", "Command line result: {}", result)
1 Like

Ok, I implemented the rule like above. My log shows this:

2020-04-30 14:40:14.126 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘exectest.rules’ has errors, therefore ignoring it: [7,17]: no viable alternative at input ‘"’
[7,18]: no viable alternative at input ‘exectest’
[7,49]: mismatched input ‘:’ expecting ‘end’

Sry, I don’t know what that means, I very new to programming.

Fix the quotemarks, the type is important in rules, original post amended

Ah ok, thanks for that!
Now the log shows this (The XXX are from me because I think the Code is a kind of safety key):

==> /var/log/openhab2/openhab.log <==
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185) ~[?:?]
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:135) ~[?:?]
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) ~[?:?]
… 3 more
2020-04-30 16:24:27.070 [INFO ] [io.openhabcloud.internal.CloudClient] - Disconnected from the openHAB Cloud service (UUID = XXXXXXXXXXXXXXXXX, base URL = http://localhost:8080)
2020-04-30 16:24:28.675 [INFO ] [io.openhabcloud.internal.CloudClient] - Connected to the openHAB Cloud service (UUID = XXXXXXXXXXXXXXX, base URL = http://localhost:8080)
2020-04-30 16:25:53.565 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model ‘exectest.rules’
2020-04-30 16:26:14.656 [WARN ] [lipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program “/etc/openhab2/scripts/slider.py” (in directory “.”): error=13, Permission denied)
2020-04-30 16:26:14.691 [INFO ] [ipse.smarthome.model.script.exectest] - Command line result:
2020-04-30 16:29:29.007 [WARN ] [okhttp3.OkHttpClient ] - A connection to https://myopenhab.org/ was leaked. Did you forget to close a response body? To see where this was allocated, set the OkHttpClient logger level to FINE: Logger.getLogger(OkHttpClient.class.getName()).setLevel(Level.FINE);

and I did this:

ls -al /etc/openhab2/scripts

and that gave me this:

total 28
drwxr-xr-x 2 openhab openhab 4096 Apr 30 11:09 .
drwxr-xr-x 14 openhab openhab 4096 Apr 17 14:59 …
-rwxr-xr-x 1 root root 385 Apr 19 17:56 helligkeit.py
-rw-r–r-- 1 openhab openhab 236 Mar 19 17:42 readme.txt
-rwxr-xr-x 1 root root 524 Apr 19 14:32 relais.py
-rw-r–r-- 1 root root 664 Apr 30 11:09 slider.py
-rwxr-xr-x 1 root root 2986 Apr 23 18:10 weatherstation.py

Here is helligkeit.py, relais.py and weatherstation.py in a green fat font, readme.txt and slider.py are in a normal font.

That seems clear enough, but I cannot advise how to fix your file permissions as I know little of *nix
There don’t seem to be any x for executable in your ls

Ok, i fixed that permission issue with the line:

sudo chmod +x /etc/openhab2/scripts/slider.py

But now there’s a new Error which says:

==> /var/log/openhab2/openhab.log <==
2020-04-30 18:04:22.490 [INFO ] [ipse.smarthome.model.script.exectest] - Command line result: /etc/openhab2/scripts/slider.py: 1: /etc/openhab2/scripts/slider.py: import: not found
/etc/openhab2/scripts/slider.py: 2: /etc/openhab2/scripts/slider.py: import: not found
/etc/openhab2/scripts/slider.py: 3: /etc/openhab2/scripts/slider.py: import: not found
/etc/openhab2/scripts/slider.py: 5: /etc/openhab2/scripts/slider.py: Syntax error: word unexpected (expecting “)”)

Those are all errors coming from your python script. There is something wrong with the script or that script depends on packages that are not available to the openhab user.

Ok thanks everyone!!! Got it. The “import:not found” error is easy to fix with the line

#!/usr/bin/env python

Now you’ve fixed it to run from the rule, you can go back to using exec binding. It works fine, but can just be harder to diagnose external problems like that.

Now I have a problem with the brightness buttons. When I press a button for the first time everything is fine and the LED shines in the right brightness. However, when I tap a different brightness afterwards, the LED begins to flicker. I think the problem is that the script is stuck in my while loop and then just executing the next command at the same time, right? Is there a rule for this or is my script simply wrong? What can I do to fix this?

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import RPi.GPIO as GPIO
import sys
import time

GPIO.setwarnings(False) 
GPIO.setmode(GPIO.BCM)

if len(sys.argv) > 2:
    RELAIS_GPIO = int(sys.argv[1])
    GPIO.setup(RELAIS_GPIO, GPIO.OUT)
    p=GPIO.PWM(RELAIS_GPIO, 2000)
    p.start(0)
    while True:
        if sys.argv[2] == "AUS":
            p.ChangeDutyCycle(0)
        elif sys.argv[2] == "DUNKEL":
            p.ChangeDutyCycle(25)
        elif sys.argv[2] == "MITTEL":
            p.ChangeDutyCycle(50)
        elif sys.argv[2] == "HELL":
            p.ChangeDutyCycle(75)
        elif sys.argv[2] == "MAX":
            p.ChangeDutyCycle(100)

Sounds right, multiple copies of your script running doing different things.
Why did you add a while loop?

Without the while loop the led only blinks for a short time.

If you wanted to run your script forever, you would want it (one copy!) running independently like a service, and somehow modify it to listen out for a parameter sent by openHAB. Using the parameter to modify the PWM.

But can’t you just send settings to this GPIO utility, and it sustains the last setting by itself?
This seems to work with no script -