Switch state from network binding combined with wake-on-lan action

my.items

Switch	MyPC_Running	"MyPC"	{wol="192.168.1.255#AA:BB:CC:DD:EE:FF"}

my.things

network:pingdevice:my_pc [ hostname="192.168.1.65", retry=1, timeout=5000, refreshInterval=60000 ]

Can I configure MyPC_Running so that it reflects the status from network:pingdevice:my_pc and allows me to use Heimkino_ChristaPC_Power.sendCommand(ON) in rules for WOL?

Or should I split it in 2 switches for status and WOL?

I have seen examples like ON, OFF, STATE with HTTP- and REGEX-Binding

Switch gs1 "gs1 [%s]" {http=">[ON:GET:http://admin:admin@192.168.125.101/goform/device?cmd=set&alarmout.status=1] >[OFF:GET:http://admin:admin@192.168.125.101/goform/device?cmd=set&alarmout.status=0] "}

but I’m not through the full docs yet and don’t understand how exactly this works and if this is even related to my question.

given the thing

network:pingdevice:my_pc [ hostname="192.168.1.65", retry=1, timeout=5000, refreshInterval=60000 ]

you can simply setup your item to

Switch	MyPC_Running	"MyPC"	{wol="192.168.1.255#AA:BB:CC:DD:EE:FF", channel="network:pingdevice:my_pc:online", autoupdate="false"}

If using

MyPC_Running.sendCommand(ON)

this will send a WOL magic packet to MAC AA:BB:CC:DD:EE:FF (with broadcast in segment 192.168.1.0)
The autoupdate="false" will prevent that the item is set to the new state instantly. Of course, as you maybe switched the item, it will take some seconds before openHAB updates the state to OFF again.
Now, if the computer got the packet and is starting, after some time the network binding will have success to ping the IP and will change the state of the item to ON.

2 Likes

Thanks a lot! That’s exactly what I was looking for.
Will check tomorrow.

Da scheint noch was zu fehlen.

Command ON is not supported for channel: online

(gleich für OFF)

Should work as suggested.

Maybe this warning appears when starting through WOL?
Of course, as online Channel is read only, I would expect the binding to ignore commands.

I’m not yet able to verify if your suggestion works.

Currently I’m stuck at the binding not working at all.

arping does not work on Mac. brew install arping works, but running it causes an error with some library and I wasn’t able to find anything helpful online.

I set allowSystemPings=false but I wasn’t able to find anything in the logs that suggests it actually does ping the computer (previously I at least got warnings about arping).

Some status info from habmin

image

Hi @Udo_Hartmann
just to get it right, is rule needed in this application or {wol=“xxx”} will do it somehow automatically when item is off?

The point is, the item will show if the device is ON or OFF. (by receiving network information).
If switching the item to ON, it will send a WOL packet to the device, which then hopefully will switch to on. After the device has booted, the network binding then will change the status of the item.

Please be aware that the command ON will not result in an status ON, because of autoupdate=“false”.

Thanks! Had the same question and found the answer here!

If I try to replicate above solution I run into issues. Firstly as soon as I add “autoupdate=“false”” to the item I get the following error:

Configuration model ‘All.items’ has errors, therefore ignoring it: [89,304]: mismatched input ‘“’ expecting RULE_STRING

Secondly if I delete autoupdate I get no error but nothing happens. I have created a rule with a timer that does what it should -

var Timer stopAlarmTimer = null
rule “Wake ob LAN Vuduo”
when
Item VuduoWOL changed to ON
then
val actions = getActions(“network”, “network:pingdevice:ba2db51b84”)

if (stopAlarmTimer === null)
{
    stopAlarmTimer = createTimer(now.plusSeconds(5)) [|
        stopAlarmTimer.cancel()
        stopAlarmTimer = null
        actions.sendWakeOnLanPacket()
        VuduoWOL.sendCommand(OFF)
    ]
}

end

But I have no indicator if the device is ON or OFF which is very convinient. Has something changed with the binding or what might be the error?

First: Which version of openHAB do you use?
Second: Wrong trigger, should be received command ON instead
Third: You must not cancel the timer within the timer (especially not as the first command…)
Fourth: actions is a local value, defined in the rule. It is not valid within the timer, as the timer context is completely independent from the rule context.

As you are using the network action to send the WakeOnLan, the item would look like this:

Switch VuduoWOL "MyPC" { channel="network:pingdevice:ba2db51b84:online", autoupdate="false" }

If you are using OH3 already, please omit autoupdate=“false” and set this through Metadata instead.

Now the rule:

// global vars are defined on top of the file
var Timer stopAlarmTimer = null

rule "Wake ob LAN Vuduo"
 when
    Item VuduoWOL received command ON
 then
    if(stopAlarmTimer === null) {
        stopAlarmTimer = createTimer(now.plusSeconds(5), [ |
            val actions = getActions("network", "network:pingdevice:ba2db51b84")
            actions.sendWakeOnLanPacket()
            // VuduoWOL.postUpdate(OFF)
            stopAlarmTimer = null
        ])
    }
end

I guess the timer is cancelled elsewhere?

Hi @Udo_Hartmann,

Thanks for your kind reply.

First: openHAB 3.1.0 Build #2146
Second: you are right
Third: That’s what you get when you adapt but don’t seriously know what you’re doing. :slight_smile:
Fourth: Will try to understand that

The magic packet is sent with the right MAC address and the device wakes up. What unfortunately still does not work is that the switch shows the on/off status. And now the packet is sent after 5 seconds instead that the switch is set back to OFF after 5 seconds. My timer should only ensure that I don’t have to reset it every time.

The point is, the switch shows if the computer is on or off, hence the channel ...online linked to the item.
If you only want a switch to switch on the computer, you can simply omit the linking to the channel. But then: why creating a timer at all? it would suffice to do something like that:

Switch VuduoWOL "MyPC" // no linking at all

rule:

rule "Wake ob LAN Vuduo"
 when
    Item VuduoWOL received command ON
 then
    val actions = getActions("network", "network:pingdevice:ba2db51b84")
    actions.sendWakeOnLanPacket()
    VuduoWOL.postUpdate(OFF)
end

Please be aware that it’s also possible to send a shutdown command to your computer (this will need an exec script). So the switch could be used to

  • start the computer
  • shutdown the computer
  • show the actual state of the computer (ON or OFF)

and this is only one Item!

Hi @Udo_Hartmann,

can you please provide more information how to combine these 3 actions (WOL, Shutdown, Show status) in one switch?

I am able to:

  • Start the computer: With a rule that is activated by THE SWITCH changed to “ON”
  • Shutdown the Computer: With a SEPERATE SWITCH and an EXEC-Thing
    … Both are working…

My question is how can i combine both in a single item.
If I am right, I need an Item to interact with the Exec-Thing. This Item can NOT be the sime Item as THE SWITCH so I need two Switch-Items:

  1. Starting the Rule:
  • If switch ON: WOL the computer
  • If switch OFF: Turn on the 2. Switch-Item to start the Exec-Thing
  1. Running the EXEC-Thing (Turn on SEPERATE SWITCH from above)

I never cared about showing the status. My priority is to make it functional.

Many thanks in advance,
Alex

Hi,

you can make switches with mappings, today options.

Have a look here:

In your rule you can look for the value and use an if-condition.

Hope, this helps.
HFM

You have to do a few different things that you can see the current state, that you can power on a device and that you can power off this device.

In openHAB 2 I have used the WOL Binding for power on my device and the Exec Binding to power off this device. I skipped the Network Binding. The disadvantage was that if I power on the device without openHAB (maybe via remote control or on-switch) I had to use a second item for the ping device state. This off course I have done with the Network Binding. What was missing was the linking of WOL, Exec and Network.

The solution for openHAB 3 is to use the Network Binding, the Exec Binding and the executeCommandLineAction. You have to use executeCommandLine for power off a device and not the Exec Binding. The Exec Binding is only used for Reboot a device over SSH and the Network Binding is used for sending a magic packet to do WOL.

Another important step is that you have to allow in /etc/openhab/misc/exec.whitlist your commands and very important in /etc/sudoers the shutdown, poweroff and reboot commands. For the WOL and the poweroff over SSH you have to create a rule. And you have to create things for using the Network Binding and the Exec Binding.

Also important is that from your openHAB device you have to connect once per SSH before running this commands because you have to add the SSH fingerprint to your device.

Change the /etc/sudoers file on the (remote) device

sudo su
<enter password>
nano /etc/sudoers

Then add following line to the end of the file:

<username> ALL=(ALL) NOPASSWD: /sbin/poweroff, /sbin/reboot, /sbin/shutdown

Please change <username> to the user you want to connect to the computer via SSH.

Why? Because you can’t enter the password for a sudo command over SSH!

Edit the exec.whitelist

In your /etc/openhab/misc/exec.whitelist you have to add following:

/usr/bin/sshpass -p <password> /usr/bin/ssh -t -o StrictHostKeyChecking=no <username>@<hostname> '/usr/bin/sudo /sbin/shutdown -h now'
/usr/bin/sshpass -p <password> /usr/bin/ssh -t -o StrictHostKeyChecking=no <username>@<hostname> '/usr/bin/sudo /sbin/reboot'

Please replace the <password> with the password for the user you want to access over SSH and off course the <username> with this username. For <hostname> you have to use the hostname for this device or the ip address.

Please also notice: Remember, however, that if you connected to the computer via the IP address, no SSH fingerprint will be created for the hostname. Then you would have to connect again via SSH with the hostname. Or accordingly vice versa!

Why using sshpass? As I have mentioned you can’t enter any passwords if you run a switch item. There is no input option in openHAB.

Why do I use /usr/bin/, /bin or /sbin etc.? Because sometimes there are problems with the PATH variable. I have one tip for this. You can use which to see how you can run one of the programs you want to use without a PATH variable.

As example:

which shutdown

should deliever

/sbin/shutdown

If multiple users are installed, it is definitely better to be connected to the appropriate user and use which instead of whereis. With whereis you may get multiple paths to an application, but not the path set for the current user to run an application.

Edit your Sitemap

You have to edit your sitemap and add as example following:

    Frame label="Network" {
        Switch item=MyDevice label="Device [%s]"
        Switch item=MyDeviceReboot label="Reboot"
        Text item=MyDeviceResponseTime label="Device Response Time [%s]"
        Text item=MyDeviceLastSeen label="Device Last Seen [%s]"
    }

Off course you can change the names of the items you want to use and off course you can change the representation. You shouldn’t have to use a Frame as example.

Create your Things

You can create your things like following:

Thing network:pingdevice:devicename [ hostname="<hostname>", macAddress="<mac_address>", retry=1, timeout=5000, refreshInterval=60000 ]
Thing exec:command:devicename:poweroff [ command="/usr/bin/sshpass -p <password> /usr/bin/ssh -t -o StrictHostKeyChecking=no <username>@<hostname> '/usr/bin/sudo /sbin/shutdown -h now'", autorun=false ]
Thing exec:command:devicename:reboot [ command="/usr/bin/sshpass -p <password> /usr/bin/ssh -t -o StrictHostKeyChecking=no <username>@<hostname> '/usr/bin/sudo /sbin/reboot'", autorun=false ]

Please replace the <password> with the password for the user you want to access over SSH and off course the <username> with this username. For <hostname> you have to use the hostname for this device or the ip address. Please change <mac_address> to the MAC address you need for WOL. Please notice that on some devices there are maybe more than one Ethernet adapter and MAC addresses. And please ignore the MAC address for WLAN because Wake On WLAN does not work!

As you can see, I have also created a Thing for Shutdown or Poweroff. This would not have been necessary for executeCommandLine, but maybe you want to use another switch to shutdown the device separately.

As you cann see I use the Network Binding with a pingdevice and the Exec Binding for commands. The rest of the Thing name can vary at will. You just have to reuse the same name in your items.

Create your Items

Create a new item file or add to an existing item file as example following:

Switch MyDevice { channel="network:pingdevice:devicename:online" }
Switch MyDeviceReboot { channel="exec:command:devicename:reboot:run" }
Number:Time MyDeviceResponseTime { channel="network:pingdevice:devicename:latency" }
DateTime MyDeviceLastSeen { channel="network:pingdevice:devicename:lastseen" }

To run a command via an Exec Binding you have to add :run as you can see on the MyDeviceReboot switch.

This is the point that definitely works differently than in openHAB 2: You can use Exec Binding only with ON, but no longer with OFF, because run will only run a command when an Item changes to ON.

Create Rules for WOL and Poweroff/Shutdown

The last point is that you have to create rules for WOL and poweroff. The WOL rule will use the Network Binding to send a magic packet which should power on your device. The poweroff rule will use the executeCommandLine Action:

rule "Wake on LAN MyDevice"
when
    Item MyDevice received command ON
then
    val actions = getActions("network", "network:pingdevice:devicename")
    actions.sendWakeOnLanPacket()
end

rule "Poweroff MyDevice over SSH"
when
    Item MyDevice changed from ON to OFF
then
    executeCommandLine("/usr/bin/sshpass","-p","<password>","/usr/bin/ssh","-t","-o","StrictHostKeyChecking=no","<username>@<hostname>","/usr/bin/sudo","/sbin/shutdown","-h","now")
end

rule "Switch Reboot MyDevice off"
when
    Item MyDeviceReboot changed to ON or Item MyDevice changed from ON to OFF
then
    createTimer(now.plusNanos(10000000), [ |
        MyDeviceReboot.sendCommand(OFF)
    ])
end

Please replace the <password> with the password for the user you want to access over SSH and off course the <username> with this username. For <hostname> you have to use the hostname for this device or the ip address.

If you reboot a device the state of this Switch is still on. So after one second this Switch will change to OFF. And to avoid mistakes if the Device is OFF no matter if it is shut down or restarted and also no matter if at all via openHAB or not, this switch gets the status OFF.

Please notice: Another difference to openHAB 2 is that executeCommandLine replaces spaces with a comma. Looks a bit hard to get used to, but it works.

This will work fine because you can endless send magic packets to a device which is ON without any effect.

I used the Network Binding for WOL. The Exec Binding I used for Reboot. And the executeCommandLine Action I used for Shutdown if the Switch which will be ON after WOL or if I started my device without openHAB changed from OFF to ON. What will also work is if you shutdown the device without openHAB that the state changes from ON to OFF because the Network Binding can not ping it. This should currently be detected after one minute as you can see with the refreshInterval=60000 in the Things definition. If you start the device manually without openHAB after one minute at the latest, this device is then recognized as online.

Thanks for this very comprehensive write up!

Did you know that the Network Binding can do WOL in OH3?

Sorry it was late yesterday. In Germany, it was already the middle of the night. I will correct the mistake. I used the Network Binding for WOL. The Exec Binding I used for Reboot. And the executeCommandLine Action I used for Shutdown if the Switch which will be ON after WOL or if I started my device without openHAB changed from OFF to ON. What will also work is if you shutdown the device without openHAB that the state changes from ON to OFF because the Network Binding can not ping it. This should currently be detected after one minute as you can see with the refreshInterval=60000.

I also forget a rule…

rule "Switch Reboot MyDevice off"
when
    Item MyDeviceReboot changed to ON or Item MyDevice changed from ON to OFF
then
    createTimer(now.plusMillis(1000), [ |
        MyDeviceReboot.sendCommand(OFF)
    ])
end

If you reboot a device the state of this Switch is still on. So after one second this Switch will change to OFF. And to avoid mistakes if the Device is OFF no matter if it is shut down or restarted and also no matter if at all via openHAB or not, this switch gets the status OFF.