Wol & exec binding -> power On and Off Pc

Tags: #<Tag:0x00007efec7868388> #<Tag:0x00007efec78682c0> #<Tag:0x00007efec78681f8>

Hi!

Now turn on works with wol binding, and turn off works with exec binding, but now only with two switches.

Switch mpcon "MicroPc On" { wol="192.168.0.255#D0:50:99:35:6E:BB" }
Switch mpcoff "MicroPc Off" { channel="exec:command:micropcoff:run" }

Please help me, how can I put it together in one swich item, ON and OFF state?

you need to create a third virtual switch item to send commands to the other two:

Item:
Number PCONOFF "PC ON/OFF"

rule

when item PCONOFF received command
then
switch (receivedCommand){
     case 1: {
            mpcon.sendCommand(ON)
     }
     case 2: {
             mpcoff .sendCommand(ON)
     }
}
end

sitemap
Switch item=PCONOFF mappings=[1="ON",2="OFF"]

HTH,
-OLI

Hi!

Thanks for your help, but something is wrong.
Here is my code:

sitemap:

Switch item=MPCONOFF mappings=[1="ON",2="OFF"]

items:

Switch mpcon "MicroPc on" { wol="192.168.0.255#D0:50:99:35:6E:BB" }
Switch mpcoff "MicroPc off" { channel="exec:command:micropcofft:run" }
Switch MPCONOFF "Micro Pc"

rules:

rule "MicroPc onoff"

when item MPCONOFF received command
then switch (receivedCommand)
	{
	case 1: { mpcon.sendCommand(ON) }
    case 2: { mpcoff.sendCommand(ON) }
	}

log:

2017-04-08 09:05:35.089 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at ‘items/MPCONOFF’ with an invalid status value ‘1’.
2017-04-08 09:05:35.808 [WARN ] [thome.io.rest.core.item.ItemResource] - Received HTTP POST request at ‘items/MPCONOFF’ with an invalid status value ‘2’.

Uh…item needs to be a Number:

Number MPCONOFF "Micro Pc"
1 Like

Thats I figure out too, but still not doing anything.

No error in the log :frowning:

fixed errors in original post(also the missing “end” in the rule)
other than that i don’t see anything obvious wrong…
Maybe add some logging in the rule where appropriate, like:

logInfo("check.rules", "switch case 1 triggered")

so you can be sure sendCommand is called.

Where can I put in this line, is this good?

rule "MicroPc on/off"

when item PCONOFF received command
then
switch (receivedCommand){
     case 1: {
            mpcon.sendCommand(ON)
     }
     case 2: {
             mpcoff.sendCommand(ON)
     }
}
logInfo("check.rules", "switch case 1 triggered")
end

New error int the log:
2017-04-08 10:56:26.359 [WARN ] [.rule.jvmmodel.RulesJvmModelInferrer] - Duplicate field: ‘mpcoff’. Ignoring ‘org.eclipse.smarthome.core.library.items.SwitchItem’.

The error is missing after a reboot, but the switch not working. :unamused:

rule "MicroPc on/off"

when 
      Item PCONOFF received command
then
switch (receivedCommand){
     case 1: {
            mpcon.sendCommand(ON)
logInfo("check.rules", "switch case 1 triggered")
     }
     case 2: {
             mpcoff.sendCommand(ON)
logInfo("check.rules", "switch case 2 triggered")
     }
}
logInfo("check.rules", "finished rule")
end

what is in events.log if you switch mpcon and mpcoff via sitemap? the command sent then must be the same then sent via the rule.

events.log

2017-04-08 12:23:23.100 [ItemCommandEvent ] - Item ‘PCONOFF’ received command 2
2017-04-08 12:23:23.111 [ItemStateChangedEvent ] - PCONOFF changed from 1 to 2
2017-04-08 12:23:30.261 [ItemCommandEvent ] - Item ‘PCONOFF’ received command 1
2017-04-08 12:23:30.273 [ItemStateChangedEvent ] - PCONOFF changed from 2 to 1

openhab.log

nothing

odd. Designer should have spotted “item” instead of “Item”. try:

when
      Item PCONOFF received command
then

with a capital I in “Item”…

It works!!!

Thank you very much! :smile:

Here is the full working code:

sitemap:

Switch item=MPCONOFF mappings=[1="ON",2="OFF"]

items:

Switch mpcon "MicroPc on" { wol="192.168.0.255#D0:50:99:35:6E:BB" }
Switch mpcoff "MicroPc off" { channel="exec:command:micropcofft:run" }
Number MPCONOFF "Micro Pc"

rules:

rule "MicroPc onoff"

when Item MPCONOFF received command
then switch (receivedCommand)
	{
	case 1: { mpcon.sendCommand(ON) }
    case 2: { mpcoff.sendCommand(ON) }
	}
end

things:

Thing exec:command:micropcofft [command="net rpc shutdown -I 192.168.0.2 -U user%%pass -f", interval=0, autorun=false]
1 Like

Hi, got the some requirement for my Kodi Media Center. I resolved it with following

items :

String wz_Kodi_systemcommand "Systemcommand"         { channel="kodi:kodi:bb8285a3-8aac-bcdd-9fe4-a16af591dbc8:systemcommand" }
Switch Fernseher_Wohnzimmer     "Fernseher Wohnzimmer"   (Status, Network) [ "Switchable" ]     { wol="192.168.1.255#XX:XX:XX:XX:XX:XX", nh="192.168.1.xxx:8080" }

rules :

when
        Item Fernseher_Wohnzimmer changed
then
 if (Fernseher_Wohnzimmer.state == OFF) {
        wz_Kodi_systemcommand.sendCommand("Shutdown")
  }
end

Hello SzilagyiR,
I’m trying to use your approach. OpenHAB is sitting on the RPi 192.168.0.4 and I’m trying to ON/OFF computer 192.168.0.2
I did install exec binding and wol binding.
Copy and past your code to correct files.
I did change user%%pass to my windows 10 login and password.
Thing is pointing to 192.168.0.2 and wol="192.168.0.2#MAC of the computer.

Is it correct approach?

Hi!

Yes it is seems to be correct! In user%%pass you must keep double % between the user and the pass!

Hello, I tried it the same way as you did

added
Switch item=MPCONOFF mappings=[1="ON",2="OFF"]
to my sitemap

added this items in my .items file
Switch mpcon "MicroPc on" { wol="192.168.1.255#D4:BE:D9:A4:38:90" }
Switch mpcoff "MicroPc off" { channel="exec:command:micropcofft:run" }
Number MPCONOFF "Micro Pc"

created a .rules file with this:
rule “MicroPc onoff”

when Item MPCONOFF received command
then switch (receivedCommand)
    {
    case 1: { mpcon.sendCommand(ON) }
        case 2: { mpcoff.sendCommand(OFF) }
    }
end

and created an things file with this content:
Thing exec:command:micropcofft [command="net rpc shutdown -I 192.168.1.255 -U user%%pass -f", interval=0, autorun=false]

ofcourse i changed user and pass with my windows login details

Now the WOL is working fine but nothing happens when I try to use the OFF button.

Any suggestions on how I could fix this problem ?

Your rule is sending ON in both cases…

Thank you for your input Hans!

I tried to change it to OFF for the second case but it is still not working.

Wake on Lan is working perfectly, but shutdown is not, any suggestions, on what I could try next ?

try if your command is working by entering it in the shell