Openhab 2 exec binding 2.0 configuration

Hi all,
I used the old exec binding-syntax to control relais via some sort of “init-script”.

Switch so_pin29	 "Schalter 29" {exec=">[ON:/opt/openhab/scripts/relais.sh on] >[OFF:/opt/openhab/scripts/relais.sh off] <[/opt/openhab/scripts/relais.sh stat:6000:REGEX((.*?))]"}

I just don’t see, how i should rebuild this with the new exec-syntax.
I tried the following:
Thing:

exec:command:relais29office [command="/opt/openhab/scripts/relais.sh %2$s", transform="REGEX((.*?))", autorun=false]

and then the item:

Switch so_pin29	 "Schalter 29"	{channel="exec:command:relais29office:run"}

first i realized is, that the switch only run the script in the case it is put “ON” … and nothing in the case it is put “OFF”

1 Like

HI,
so it’s not possibile to use exec binding with Switch item by executing one script for ON and another one for OFF ?
Is it possibile to run older 1.8 exec binding under openhab 2.0 ?

Regars

You can decide in your script what happens.

Here an example:
#!/bin/bash
if [ “$1” = “off” ] || [ “$1” = “0” ];
then
// do something
else
// do something else
fi

Hello to all,
has anyone solved the problem?

I coded now:

.things:

Thing exec:command:steckdoseG2 [command="/opt/rc-switch/433Utils/RPi_utils/steckdose1.sh %2$s", transform="REGEX((.*?))", autorun=false]

.items:

Switch stecker {channel="exec:command:steckdoseG2:run"}

steckdose1.sh

#!/bin/sh

if [ "$1" = "off" ] || [ "$1" = "0" ] || [ "$1" = "OFF"  ]; then
        /opt/rc-switch/433Utils/RPi_utils/SteckdoseG2 15 0
else
        /opt/rc-switch/433Utils/RPi_utils/SteckdoseG2 15 1
fi

Can anyone help me with this problem?

Thank you.

This is a simplified version of what I’m using to start/stop my NAS:

.things
Thing exec:command:device-nas-control [ command="/path/to/script %2$s", interval=0, autorun=true ]

.items
String network_device_nas_switch "NAS" { channel="exec:command:device-nas-control:input", autoupdate="false" }

.sitemap
Switch item=network_device_nas_switch mappings=[ "ON"="ON", "OFF"="OFF" ]

This will either send “ON” or “OFF” to the script provided as “command”.

Thanks, but i get this error:
[ERROR] [hab.binding.exec.handler.ExecHandler] - An exception occurred while formatting the command line with the current time and input values : ‘Format specifier ‘%2$s’’

Can you help? Thanks.

Honestly, no idea. I found my working configuration by searching the forums. I do not really understand the OH2 configuration concepts yet. Here is an intermediate version which actually worked at the time of writing but had some limitations: Trigger Exec Binding switch with HUE Emulation [SOLVED]

My current configuration is much more complex but I can post it if you need it.

maybe it will helps… that will be nice :slight_smile:

Alex, the script of your link works fine :slight_smile:
THANK YOU!!!

Ok, here we go. That configuration allows me to start my NAS using the Amazon Echo and it actually represents the current state of the switch by querying if the device is online:

.things
Thing exec:command:device-nas-control “NAS Control Command” [ command="/home/openhab/bin/device-nas %2$s", interval=0, timeout=10, autorun=true ]
Thing exec:command:device-nas-status “NAS State Command” [ command="/home/openhab/bin/device-nas status", interval=20, timeout=5 ]

.items
String network_device_nas_control “NAS Control” { channel=“exec:command:device-nas-control:input”, channel=“exec:command:device-nas-status:output”, autoupdate=“false” }
String network_device_nas_status “NAS Status” { channel=“exec:command:device-nas-status:output” }
Switch network_device_nas_switch “NAS” (group_location_livingroom) [ “Switchable” ]

.sitemap
Switch item=network_device_nas_switch

.rules

rule "NAS Switch"
when
    Item network_device_nas_switch changed
then
    logInfo("Network", "Switching NAS " + network_device_nas_switch.state.toString + ": " + network_device_nas_control.state.toString)
    network_device_nas_status.postUpdate(network_device_nas_switch.state.toString)
    network_device_nas_control.sendCommand(network_device_nas_switch.state.toString)
end

rule "NAS State"
when
    Item network_device_nas_status changed
then
    logInfo("Network", "Switching NAS " + network_device_nas_status.state.toString + ": " + network_device_nas_control.state.toString)
    network_device_nas_switch.postUpdate(network_device_nas_status.state.toString)
    network_device_nas_control.sendCommand(network_device_nas_status.state.toString)
end
1 Like

Any idea if there’s been a resolution of this problem yet? (exec 2 requiring String objects and no longer working with Number objects)?

I’m trying to migrate from OH1.8 to OH2 but I can’t bring over all my sensor data as Numbers, and every attempt to parse the Strings into Floats etc fails.

1 Like

Same here. Only String works… Number does not work.

My current workaround is to create a stub String item that is bound to the exec binding channel and a second Number item that is populated from the String by a simple rule.

Yeah this is what I’ve been doing too , though it’s a bit ugly having a hundred or so lines of code just parsing each string into a number

Just wondering. Would using a generic lambda function reduce the amount of code to maintain?

Hello Alex,

could you also please provide the script that you are running?

Thank you very mich :slight_smile:

Hello Lucan,

here you go:

#!/bin/bash

THIS_NAME="$(basename "${0}")"

HOST_USER="openhab"
HOST_NAME="nas.home.lan"
HOST_MAC="xx:xx:xx:xx:xx:xx"

case "${1}" in
        [oO][nN])
                exec sudo /usr/sbin/etherwake "${HOST_MAC}" >/dev/null 2>&1
                ;;

        [oO][fF][fF])
                exec ssh -o ConnectTimeout=3 -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -l "${HOST_USER}" "${HOST_NAME}" "sudo /sbin/poweroff" >/dev/null 2>&1
                ;;

        [sS][tT][aA][tT][uU][sS])
                ping -c 1 -W 1 "${HOST_NAME}" >/dev/null 2>&1

                if [ $? -eq 0 ] ; then
                        echo "ON"
                        exit 0
                fi

                echo "OFF"
                exit 1
                ;;

        *)
                echo
                echo "Usage: ${THIS_NAME} <on|off|status>"
                echo
                exit 1
                ;;
esac

exit 0
1 Like

Thank you so much! I’ve made a few changes so it fits my needs and everything works perfectly

Switching from Number to String also solved the problem for me… :slight_smile:

I think there will be a solution for that in the next version oft the exec binding.

I use one rule for all strings to convert them.
It only needs one additional line for each item. And this will be reduced in future to only one group, have a look here.