[SOLVED] Execute command with arguments when item is turned on

Hello,

I am trying to solve the following issue, but haven’t been able to find a proper solution, yet:

I am using the Python scripts for Broadlink devices to control my AC, TV, Roomba, etc. The usage of the script is basically

broadlink.py -c COMMAND -d DEVICE

What I don’t understand is how to get this now integrated in OpenHAB, such that I do not create for every single command a “virtual” switch that triggers a rule that then executes the above command for that switch - this is a lot of duplicated code.

My question is, whether there is some way to define an item like this

Switch AC_OFF { channel="shell:/usr/local/bin/broadlink:<command>@@<device>" }

which basically means, that if the switch is turned on, the command /usr/local/bin/broadlink is executed with the parameters command and device.

Perhaps somebody can help me on this, and not have me write rules for every command like

rule
Item AC_OFF changed to ON
then
executeCommand('/usr/local/bin/broadlink@@command@@device')
end

which is far less maintainable as well. Thanks a lot in advance.

No.

You can write a rule triggered from a whole bunch of Items, or better a Group member, and have the rule figure out who did what and use that to build a string to call the Exec action.

Or you can use the exec binding as documented here

which would allow you to define a channel for each individual device, but insert the OH command as it is sent using the substitution %2
The docs take a bit of study to disentangle ‘command’ in the sense of what you want to execute from ‘command’ as the instruction from openHAB UI or rules.

A channel per device is a pretty standard OH requirement.

1 Like

Hi @rossko57,

thanks for your answer. You said “which would allow you to define a channel for each individual device, but insert the OH command as it is sent using the substitution %2” – could you give an example please? I’ve been knocking my head the whole evening but don’t get it how you mean that. I tried defining a thing as described in the documentation of the Exec binding, but I didn’t get how through %2 I could be getting there something done. Might be I misread your post.

example items

Switch MyTV "TV power" {channel="exec:command:tvonoff:input"}
// ON/OFF cmd here will set state, state passes to tvonoff Thing input channel
Number MyChannel "TV channel number " {channel="exec:command:tvchan:input"}
// number commands here will set state and pass to tvchan Thing as input

example exec Things

Thing exec:command:tvonoff [command="/somefolder/here myscript tvid %2$s", interval=0, timeout=5, autorun=true]
// %2$s substitutes input channel state in exec command line
// interval=0 for one-shot execution
// timeout=5 in case script hangs
// autorun=true to make it execute when input channel state changes
// input channel (and others) automatically created by Thing
Thing exec:command:tvchan [command="/somefolder/here myscript tvchan %2$s", interval=0, timeout=5, autorun=true]

If you had to, you could make just one Thing with no params for the script, link it a dummy Item, and have something send it both params as a single string. Something like a rule triggering from many Items that builds the string with item reference + value.
Not sure why you’d want to do that.

1 Like

Thank you so much. That did it for me! I was a bit confused by the different options and - honestly - thought it had to be much more complicated :slight_smile:

It’s one of these areas where OH & binding provide a lot of flexibility … choices offered makes it hard to settle on one approach.

Exec binding allows you to micro-manage execution, maybe there’s a giant script that returns a complicated result or maybe the script is working some device directly.
But your case just seems to need a fire-and-forget job.

If you have to “press buttons in a sequence” you might run into timing issues with your script if it doesn’t handle reentrancy very well (multiple calls before the last one finished).

It’s basically to control an AC via Broadlink IR. So I have my Item of type String and the value that I pass to it calls the Broadlink script I got from GitHub and executes the command that I specify with the string.

So to chose between different modes I have in Sitemap just that item as selection and mappings that contain the commands. Basically through this it is 2 lines in OH (item and sitemap) plus Exec thing in Paper UI – which is great.

Thanks again!