Running Script with a Switch

Hello,

I want to start and power off my nas with openhab.
Powering on with openhab works with the WOL Binding.

Now i wrote a small shell script, that connects to the nas and exectutes the power off command.
The script works fine so far as i was able to test it on the command line. also running it with the openhab user works fine.

Now I installed the exec2 binding and added the command.
./etc/openhab2/scripts/poweroff_storage.sh

But how do I now create an item with paperui that makes the script run?

If that works, a nother nice step would be:

When switching on: It should use the WOL Binding, if switching off, it should use the exec binding with my script. But at first a simple button that makes the script run (from paperui) would be great

https://docs.openhab.org/addons/bindings/exec/readme.html

Thing

Thing exec:command:pow_off_storage [command="/etc/openhab2/scripts/poweroff_storage.sh"]

Item

Switch pow_off_storage_item { channel="exec:command:pow_off_storage:run"}

If the Script needs sudo … you need to add Openhab user to sudoers

With PaperUI go to your Exec Thing und and Connect a Item to the run Channel :slight_smile:

1 Like

Thanks for you reply.
I read the readme and also a lot of stuff at the forum.
But even with your reply i’m not sure what to do.

Do I need do manually add the thing and item? No possibility to do it with paperui or Habmin? If so: where?
Where to find / enter what you wrote on paper UI?

Thank you!

Thing manully… item via PaperUI or manually

1 Like

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 http://docs.openhab.org/configuration/things.html#defining-things-using-files for how to define things in text config files. See https://docs.openhab.org/addons/bindings/exec/readme.html for how to define the Thing if you need more information on how to format the Thing.

1 Like

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.


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

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.

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

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!

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

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

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

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

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

Items:
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?

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/storage.sh %2$s", interval=0, timeout=10, autorun=true ]
Thing exec:command:device-nas-status "NAS Online Status" @ "Diverses" [ command="/etc/openhab2/scripts/storage.sh 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”
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

File: /etc/openhab2/scripts/storage.sh

#!/bin/bash
# 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"
HOST_USER="admin"
#Synology Admin PW
HOST_PASS="12345"
#Synology IP Adress
HOST_NAME="192.168.178.XX"
#Synology MAC Adress
HOST_MAC="XX:XX:XX:XX:XX:XX"

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

        [oO][fF][fF])
				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
				;;

        [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

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

thanks
Andrea

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

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

Thanks
Andrea

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.

Maybe this is the way to go?

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.

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?