Running Script with a Switch

Tags: #<Tag:0x00007f1e57d06138>
(Kevin) #4

Thing manully… item via PaperUI or manually

1 Like

(Rich Koshak) #5

I don’t think that you can create Things in PaperUI for Exec2. If you go to Configuration -> Things and hit the + and you don’t see Exec listed, you must use the text based .things files.

Once you have the Thing you can probably create the Item in Configuration -> Items.

See for how to define things in text config files. See for how to define the Thing if you need more information on how to format the Thing.

1 Like

(Lucan) #6

Thanks for your replys.
Exec is listed under “things” within the configuration.
But there is no switch to run the script.
Already tested all possible channels.


(Rich Koshak) #7

You have to create an Item linked to the RUN channel to trigger the script. Or, if your script takes arguments then you sendCommand to an Item linked to the input channel.

Please read the Exec binding README.

1 Like

(Lucan) #8

I’ve read it and as I wrote I already tried all of the items.
The switch is not “clickable”. I’m not able to use it.


(Rich Koshak) #9

That’s not the Item.

That’s the Thing. PaperUI only shows Things. PaperUI is not intended to be the User’s Interface for your home automation. It is an Administration UI. As such, it provides the Control section and lets you interact with SOME of your Things for testing purposes. But for this you need to create a Switch Item, put that on a Sitemap or HABPanel, and tell the Script to run from there by toggling the Item.

1 Like

(Lucan) #10

Didn’t know that, thank you.

Started the item from Habmin and with the app but nothing happens…

script is running fine… tested it with the openhab user from command line. Thats the one its executed by the exec binding, isn’t it?

Thank you!


(Rich Koshak) #11

Yes, the openhab user is what your script will run as.

At this point my next advice is to look at

Try the various suggestions including replacing spaces with @@ and using executeCommandLine so you can see the result when the script fails.

1 Like

(Lucan) #12

Thank you very much.
I was able to get it work now.

Now I’ve made a other script for waking the server up.

Power On:
Thing exec:command:storage_power_on “Storage Power On” [command="/etc/openhab2/scripts/", interval=0, timeout=15, autorun=false]

Power Off:
Thing exec:command:storage_power_off “Storage Power Off” [command="/etc/openhab2/scripts/", interval=0, timeout=15, autorun=false]

Power On:
Switch storage_poweron_item “Storage Power On” { channel=“exec:command:storage_power_on:run”}

Power Off:
Switch storage_poweroff_item “Storage Power Off” { channel=“exec:command:storage_power_off:run”}

And it works as it should.

But how can I do now the following:

One Item with the switch type (because i need a switch for alexa integration), that if its put “On” it runs the power on thing and if “Off” it runs the power off thing.

How does the iteem need to look like?


(Lucan) #13

Managed to get everything to work.
Big thanks to alex from this post here:

Here is exactly what i’ve got if somebody needs it.

This is used to power on my synology nas with Wake On Lan and to power it off by ssh.

File: /etc/openhab2/things/storage.things

Thing exec:command:device-nas-control "NAS Control (On/Off)" @ "Diverses" [ command="/etc/openhab2/scripts/ %2$s", interval=0, timeout=10, autorun=true ]
Thing exec:command:device-nas-status "NAS Online Status" @ "Diverses" [ command="/etc/openhab2/scripts/ status", interval=20, timeout=5 ]

File: /etc/openhab2/items/storage.items

 String network_device_nas_control "NAS Control" (gOthers) { channel="exec:command:device-nas-control:input", channel="exec:command:device-nas-status:output", autoupdate="false" }
String network_device_nas_status "NAS Online Status" (gOthers) { channel="exec:command:device-nas-status:output" }
Switch network_device_nas_switch "NAS (On/Off)" (gOthers) [ "Switchable" ]

File: /etc/openhab2/sitemaps/[yoursitemap].sitemap

Frame label=“Storage” {
Switch item=network_device_nas_switch

File: /etc/openhab2/rules/storage.rules

rule "NAS Switch"
Item network_device_nas_switch changed
logInfo(“Network”, "Switching NAS " + network_device_nas_switch.state.toString + ": " + network_device_nas_control.state.toString)

rule "NAS State"
Item network_device_nas_status changed
logInfo(“Network”, "Switching NAS " + network_device_nas_status.state.toString + ": " + network_device_nas_control.state.toString)

File: /etc/openhab2/scripts/

# wakeonlan need to be installed; sudo apt-get install wakeonlan
THIS_NAME="$(basename "${0}")"
#Admin-User of Synolgy, must be active and the default user "admin"
#Synology Admin PW
#Synology IP Adress
#Synology MAC Adress

case "${1}" in
                exec wakeonlan "${HOST_MAC}" >/dev/null 2>&1 

				exec sshpass -p "${HOST_PASS}" ssh -o StrictHostKeyChecking=no "${HOST_USER}"@"${HOST_NAME}" 'echo "'${HOST_PASS}'" | sudo -S shutdown now -P' >/dev/null 2>&1

                ping -c 1 -W 1 "${HOST_NAME}" >/dev/null 2>&1

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

                echo "OFF"
                exit 1

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

exit 0
1 Like

(Andrea Riela) #14

Any change to power off the synology without exec binding? I mean, with OH in a Docker container?



(Scott Rushworth) #15

You could run the script in a rule using executeCommandLine instead of using the Exec binding.


(Andrea Riela) #16

an external script outside the container? Or a script inside the container but calling apps outside the container?



(Rich Koshak) #17

Your need to have something outside the container that OH talks to to issue the command.

The shutdown command probably doesn’t exist inside the Docker image and if it did I doubt it would be allowed to run, it at best it would shutdown the container. One of the whole points of containers is to prevent apps from doing this sorry of thing.

It’s the same problem. Inside the container there are a limited set of executables available. Ping isn’t even there.


(Andrea Riela) #18

Maybe this is the way to go?


(Rich Koshak) #19

I still don’t think a Docker container will be allowed to shutdown the host is running on. That violates all sorts of container principles.


(Andrea Riela) #20

Just thinking loud … that means I’m not sure OH should be the right app to put in a container … as the strength of OH is to communicate with things all around … does it make any sense?

Edit: no, it doesn’t :slight_smile: you are right, but at least it should be able to shut down another host (in case, for example, you have OH in a server, and your media center in another nas). Makes sense?


(Scott Rushworth) #21

You could run a cron job that runs a script that polls the state of an item through the REST API, and depending on the state, could power off the NAS.

1 Like

(Rich Koshak) #22

It depends on your needs and requirements. For you and many it may not be a good choice. For me and others it’s perfect for our needs. For still others it’s their only option.

There are over 330 bindings. If only 329 of them work in the Docker container does that negate it’s utility entirely? Not in my opinion.

1 Like

(Felix) #23

I am pretty new to the topic of openHAB2, so I got some questions concerning your script.
Just to test with my Synology I tried to copy/paste everything to a fresh install of openHABian.
Bindings used were WOL, NETWORK, EXEC.
The WOL function and Status function work without issues, but I have issues with the shutdown.

My items file is like this:

Switch DS215j "Diskstation DS-215j" {	
	exec=">[OFF:sudo ssh USERNAME@192.168.178.xx shutdown -p now]",

The errorlog always gives an error that it cannot run script “error=33”.
My assumption is that the openhab user does not have the rights to run the script. I also tried changing user and group in /vusr/lib/systemd/system/openhab2.service to root:root but it did not work.

Basically what I want to do is, have a Sitemap-switch that gives status of NAS and can WOL the NAS, as well as shut it down.

SSH commands I cannot get working because I dont want to mess with my NAS (ssh.key login).
Is there any other way? I mean the DS Finder app on my phone can shut the Synology down without issue/messing with ssh keys and so on.

Help would be greatly appreciated.