Openhab 2 exec binding 2.0 configuration

Answering my own question, I realise I was over-thinking it. The items line can remain unchanged from original and there is no need for a corresponding “things” line.
I.e. Number DS18B20ESP “Temperature ESP2 [%.2f °C]” (Temperature,HouseTemps) {mqtt="<[localbrokerrb:/DeviceESP12E/Room1/Temperature:state:default]"}
in the .items file and no line added to the .things file and all is working well with mqtt alongside Exec 2.0.

Still need to find an answer to Number vs String issue.

Hi all,
Anything new on the number vs string issue? I have the same problem as everybody else here…

Here is my workaround (rule to convert the string to a double…):

import java.lang.Double.*
import java.lang.String.*

rule "SHT11 Converter"

when
	Item SHT11_Data changed
then
	val String data = SHT11_Data.state.toString
    var String[] dataParts = data.split(":")
	postUpdate(SHT11_Temperature, Double::parseDouble(dataParts.get(0)))
	postUpdate(SHT11_Humidity, Double::parseDouble(dataParts.get(1)))
end

My items are

String SHT11_Data "Data [%s]" <temperature> {channel="exec:command:05ff2016:output"}
Number SHT11_Temperature "SHT11 Temperature [%.2f C]" <temperature>
Number SHT11_Humidity "SHT11 Humidity [%.2f %%]" <humidity>

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