[SOLVED] Pass parameters to bash script

Hello,
i need to pass parameters to bash script in order to reusing the script.

default.thing
Thing exec:command:steckdoseG4-on [ command="/opt/openhub/script/test.sh %2$s" , transform=“REGEX((.*?))”, interval=0, autorun=true ]
default.item
Switch Bedroom_Light “Light” (Bedroom, gLight) { channel=“exec:command:steckdoseG4-on:input”, autoupdate=“false” }
default.sitemap
Switch Bedroom_Light “Light” (Bedroom, gLight) { channel=“exec:command:steckdoseG4-on:input”, autoupdate=“false” }

i have 30 lights and 20 window items, each need ON/OFF or up/down/stop function.
what is the right way doing it?
if i could pass custom parameter from sitemap to bash script it will be great
Thanks
Chen

please use Code fences to make your Code readable (one of the Icons on the right).

first of all - the sitemap is just a means for displaying items. So all the logic should (read: must) be already in the items definition. otherwise you can’t automate it easily.
With your items having all indentical channel-definitions you can’t distinguish them at the channel. They all do exactly the same at the moment.

Can you please provide more Information on:

  • your shellscript (e.g. what arguments does it expect)?
  • your configuration (do you have exec binding and Regex Transformation installed?)

Thanks
bash script will run the above command:
/usr/bin/python /opt/openhub/script/send.pi >10220u<
where 10220 is the device ID and ‘u’ for ON or up if the device is a window
right now it accept one arguments, i can change it to accept two, one for device ID and other for operation if it necessary

yes, i have exec binding and Regex Transformation

According to the docs, you can only use a String Item with the input channel. So what you will probably need to do is create Dimmer Proxy Item and a Rule to translate the commands to the Dimmer to the argument that you pass to the script. When you sendCommand that String to the Item bound to the input channel it will execute the script with the value of the String as the argument (as you have it configured).

NOTE: There is no OH Item that supports ON/OFF and UP/DOWN/STOP commands. You can use a Dimmer that supports ON/OFF INCREASE/DECREASE or Percent or you can try to use a Rollershutter which supports UP/DOWN, STOP/MOVE, or Percent. Or you can use a Number or String Item and use a MAPPING on the sitemap to get buttons for on, off, up, down, stop.

So if you used a Dimmer you would have

Items

String Bedroom_Light_CMD   { channel=“exec:command:steckdoseG4-on:input”, autoupdate=“false” }
Dimmer Bedroom_Light “Light” (Bedroom, gLight)

Rules

rule "Trigger bedroom light"
when
    Item Bedroom_Light received command
then
    var String cmd = null
    switch(receivedCommand){
        case ON: cmd = "on"
        case OFF: cmd = "off"
        case INCREASE: cmd = "up"
        case DECREASE: cmd = "down"
        default: logError("light", "Do not know how to handle " + receivedCommand.toString)
    }

    if(cmd !== null) Bedroom_Light_CMD.sendCommand(cmd)
end

Sitemap

Slider item="Bedroom_Light"

NOTE: Your sitemap entry is an Item definition, not a sitemap element.

Adjust the above as desired depending on whether you want to use a Rollershutter or Number/String Item with MAPPINGS.

Once you come up with an approach from above and get it working for one light, come back and we can help make it generic so you don’t have to have 50 copies of the same rule.

thank you
i have it working for one light, finally
i intend to use Switch for lighning and Rollershutter for windows

now, how can i change the rule for executing command with a different id ?

Thing exec:command:steckdoseG4-on [ command="/usr/bin/python /opt/openhab/script/send.pi %2$s" , transform="REGEX((.*?))", interval=0,  autorun=true ]
String Bedroom_Light_CMD   { channel="exec:command:steckdoseG4-on:input", autoupdate="false" }
Dimmer Bedroom_Light "Light" (Bedroom, gLight)
rule "Trigger bedroom light"
when
    Item Bedroom_Light received command
then
    var String cmd = null
    switch(receivedCommand){
        case ON: cmd = ">10220u<"
        case OFF: cmd = ">10220d<"
        default: logError("light", "Do not know how to handle " + receivedCommand.toString)
    }

    if(cmd !== null) Bedroom_Light_CMD.sendCommand(cmd)
end

Thanks
Chen

You will need to create Items for each of your Devices.

Trigger the Rule by each of those Items.

In the Rule you can see what device received the command with triggeringItem. Assuming the only difference between them all is the the String you pass to your script then you can have a switch on triggeringItem.name to build the first part and then the switch on receivedCommand like you have it to append the u< or d< or whatever is appropriate for the command.

i don’t get it
can you please give an example on how to use triggeringItem.name inside a rule?
i need to combine device ID and operation (ON/OFF)

Thanks
Chen

triggeringItem is an Item. It is the Item that triggered the rule to be exact. You use it like you would use ANY Item in a Rule.

if(triggeringItem.name == "MyItemName") triggerintItem.sendCommand(OFF)

Maybe you will find some helpfull infos there

Something like this

Thanks, i got it working now.
this is how my rule looks like:

rule "Trigger Any item"

when
    Item * received command

then
    var String cmd = null
    val String Iname = triggeringItem.name
        if(Iname == "Bedroom_light") cmd =  "10110"
            if(Iname == "Bedroom_heat") cmd =   "10360"
            if(Iname == "KidsRoom_light") cmd = "10120"
            if(Iname == "hall_light") cmd =     "10130"
            if(Iname == "Bathroom_light") cmd = "10140"
            if(Iname == "Bathroom_heat") cmd =  "10370"
            if(Iname == "FamilyRoom_ligh") cmd = "10170"
            if(Iname == "diningroom_light1") cmd = "10210"
            if(Iname == "diningroom_light2") cmd = "10220"
            if(Iname == "Kitchen_light1") cmd = "10230"
            if(Iname == "Kitchen_light2") cmd = "10240"
            if(Iname == "Kitchen_light3") cmd = "10250"
            if(Iname == "LivingRoom_light1") cmd = "10260"
            if(Iname == "LivingRoom_light2") cmd = "10270"
            if(Iname == "LivingRoom_light3") cmd = "10280"
            if(Iname == "Outside_light") cmd = "10310"
            if(Iname == "Backyard_light3") cmd = "10320"
            if(Iname == "FrontYard_light1") cmd = "10330"
            if(Iname == "FrontYard_light2") cmd = "10340"
            if(Iname == "Backyard_light1") cmd = "10350"
            if(Iname == "Backyard_light2") cmd = "10380"
            if(Iname == "Bedroom_win_keren") cmd = "16010"
            if(Iname == "Bedroom_win_chen")  cmd = "16020"
            if(Iname == "Bedroom_win_park")  cmd = "16030"
            if(Iname == "KidsRoom_win")      cmd = "16040"
            if(Iname == "Bathroom_win")      cmd = "16050"
            if(Iname == "FamilyRoom_win")    cmd = "16060"
            if(Iname == "Kitchen_win")       cmd = "16070"
            if(Iname == "LivingRoom_win1")   cmd = "16080"
            if(Iname == "LivingRoom_win2")   cmd = "14010"
            if(Iname == "LivingRoom_win3")   cmd = "14020"
            if(Iname == "LivingRoom_win4")   cmd = "14030"

    switch(receivedCommand){
        case ON: cmd  = ">" + cmd + "u<"
        case OFF: cmd = ">" + cmd + "d<"
        case UP:   cmd = ">" + cmd + "u<"
        case DOWN: cmd = ">" + cmd + "d<"
        case STOP: cmd = ">" + cmd + "s<"
          
        default: logError("light", "Do not know how to handle " + cmd )
    }

    if(cmd !== null) Bedroom_Light_CMD.sendCommand(cmd)
end


How do I create a general rule that is activated in any case of any item received
command?
i need to change the “when” condition in order it to work
Thanks
Chen

At this time you will have to add each item as a trigger to the rule. TriggeringItem doesn’t yet work with Group triggers.

i change the “when” condition to:

rule "Trigger Any item"

when

    Item Bedroom_light received command or
    Item Bedroom_win_keren received command or
    Item Bedroom_win_chen received command or
    Item Bedroom_win_park received command or
    Item Bedroom_heat received command or
    Item KidsRoom_light received command or
    Item KidsRoom_win received command or
    Item hall_light received command or
    Item Bathroom_light received command or
    Item Bathroom_win received command or
    Item Bathroom_heat received command or
    Item FamilyRoom_win received command or
    Item FamilyRoom_light received command or
    Item diningroom_light1 received command or
    Item diningroom_light2 received command or
    Item Kitchen_light1 received command or
    Item Kitchen_light2 received command or
    Item Kitchen_win received command or
    Item Kitchen_light3 received command or
    Item LivingRoom_win1 received command or
    Item LivingRoom_win2 received command or
    Item LivingRoom_win3 received command or
    Item LivingRoom_light1 received command or
    Item LivingRoom_win4 received command or
    Item LivingRoom_light2 received command or
    Item LivingRoom_light3 received command or
    Item Outside_light received command or
    Item Backyard_light3 received command or
    Item Backyard_light4 received command or
    Item FrontYard_light1 received command or
    Item FrontYard_light2 received command or
    Item Backyard_light1 received command or
    Item Backyard_light2 received command


then
    var String cmd = null
    val String Iname = triggeringItem.name
        if(Iname == "Bedroom_light") cmd =  "10110"
            if(Iname == "Bedroom_heat") cmd =   "10360"
            if(Iname == "KidsRoom_light") cmd = "10120"
            if(Iname == "hall_light") cmd =     "10130"
            if(Iname == "Bathroom_light") cmd = "10140"
            if(Iname == "Bathroom_heat") cmd =  "10370"
            if(Iname == "FamilyRoom_light") cmd = "10170"
            if(Iname == "diningroom_light1") cmd = "10210"
            if(Iname == "diningroom_light2") cmd = "10220"
            if(Iname == "Kitchen_light1") cmd = "10230"
            if(Iname == "Kitchen_light2") cmd = "10240"
            if(Iname == "Kitchen_light3") cmd = "10250"
            if(Iname == "LivingRoom_light1") cmd = "10260"
            if(Iname == "LivingRoom_light2") cmd = "10270"
            if(Iname == "LivingRoom_light3") cmd = "10280"
            if(Iname == "Outside_light") cmd = "10310"
            if(Iname == "Backyard_light3") cmd = "10320"
            if(Iname == "FrontYard_light1") cmd = "10330"
            if(Iname == "FrontYard_light2") cmd = "10340"
            if(Iname == "Backyard_light1") cmd = "10350"
            if(Iname == "Backyard_light2") cmd = "10380"
            if(Iname == "Bedroom_win_keren") cmd = "16010"
            if(Iname == "Bedroom_win_chen")  cmd = "16020"
            if(Iname == "Bedroom_win_park")  cmd = "16030"
            if(Iname == "KidsRoom_win")      cmd = "16040"
            if(Iname == "Bathroom_win")      cmd = "16050"
            if(Iname == "FamilyRoom_win")    cmd = "16060"
            if(Iname == "Kitchen_win")       cmd = "16070"
            if(Iname == "LivingRoom_win1")   cmd = "16080"
            if(Iname == "LivingRoom_win2")   cmd = "14010"
            if(Iname == "LivingRoom_win3")   cmd = "14020"
            if(Iname == "LivingRoom_win4")   cmd = "14030"

    switch(receivedCommand){
        case ON: cmd  = ">" + cmd + "u<"
        case OFF: cmd = ">" + cmd + "d<"
        case UP:   cmd = ">" + cmd + "u<"
        case DOWN: cmd = ">" + cmd + "d<"
        case STOP: cmd = ">" + cmd + "s<"

        default: logError("light", "Do not know how to handle " + cmd )
    }
	
    if(cmd !== null) Iname.sendCommand(cmd)
end

i have problem exec the command:
in log file i see error:

2018-01-20 22:59:05.834 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert '>10170d<' to a command type which item 'FamilyRoom_light' accepts: [OnOffType, RefreshType].

what i"m doing wrong ?

Thank you all
now it’s working

i change the line to:

if(cmd !== null) sendCommand(Iname + "_CMD", cmd)
1 Like