Using rules to execute a script with number parameter

Hello,
I’d like to execute a script and send it a parameter that corresponds to the number value of an OpenHAB selection item. For example

item definition:

Number light_level "Level [%.1f]"

sitemap:

Selection light_level label="Brightness" mappings=[1="1%", 10="10%", 30="30%", 60="60%", 80="80%", 100="100%"]

rule:

rule "Living Room Lights Dimming"
when
    Item light_level received update
then
    executeCommandLine("/opt/myscripts/lightlevel_X.sh")
end

Rather than having a bunch of scripts for the different levels and if statements, can I do it with just one script that gets a parameter? I don’t know how to form the execute line in OpenHAB to do it.

Is there a better way of doing this that lets you use a slider instead of a itemized list of numbers? Any ideas to do this better would be much appreciated.

You can pass the value of light_level as the command line arguments to light_level.sh using

executeCommandLine("/opt/myscripts/lightlevel_X.sh " + light_level.state)

From within your she’ll script you reference the first command line argument using $1 I believe.

2 Likes