Is there a way to switch PC ON Wake On LAN using Smarthub?

Hi, below you will find a very simple code for a smart app that you can create on your own. Make sure to create a virtual button first (In the example below, the virtual button’s name is “WOLButton” and its function is one of a “Momentary Button Tile”.

Here is the code:

/**
 *  wakeonlancustom
 *
 *  Copyright 2018 Mihai Floares
 *
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *  in compliance with the License. You may obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *  for the specific language governing permissions and limitations under the License.
 *
 */
 
definition(
    name: "wakeonlancustom",
    namespace: "psecto",
    author: "Mihai Floares",
    description: "wake pc on lan",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")



preferences {
    section("WOLButton") {
        input "WOLButton", "capability.switch", required: true, title: "button?"
        }
    }
    
def initialize() {
	subscribeToCommand(WOLButton, "push", myWOLCommand)
}

def myWOLCommand(evt) {
sendHubCommand(new physicalgraph.device.HubAction (
        "wake on lan YOUR_DEVICE_MAC_ADDRESS_without_columns_or_spaces",
        physicalgraph.device.Protocol.LAN,
        null,
        [:]
    ))

    
    }


That’s it. Enjoy. More details on https://github.com/psecto/SamsungSmartThings

Mihai

Ran across this the other day:

I created my smartapp using your code and inserted the MAC address as instructed (just the letters and numbers). The button push registers under Live Logging as an event posted to the device (the Hub?). However, nothing happens. I can send a WOL packet just fine using a linux machine on the network. But the smartapp won’t actually send the command over and wake up my machine.

Any ideas? Thanks.

It’s strange, in my case the code works.

  • Make sure the that the label of your device is the same to the one your are using in your code. Personally, I’ve set all device’s identifiers the same (in my case wolPC, see attached image).

  • Then, make sure you target the correct button to be used, in the correct location

Now, you’ve said you can send wol packets… is your machine receiving them? On the target device, you can use (for test purpose) a wol sniffer (port 9), I personally used one called “Depicus”.

I hope this helps…

Wow, thanks for taking the time to include pictures! I tried it again and when I clicked Install on the simulator to test it gives me this error:

4:53:02 PM: error Wake On Lan PC DOES NOT RESPOND TO INSTALLED HANDLER

I used the momentary button tile as the handler so I don’t know why I’m getting the error. I will test it using the actual smartapp on my smartthings app shortly.

From the linux machine I am able to wake the PC by using ether-wake. Thanks for letting me know about Depicus.

I posted my version of this to github. Wrote if for myself a while back so I edited a bit to make it more user friendly…Very similar to @psecto’s…I use a momentary button tile for my setup as well.

2 Likes

I’ve ended up using $appSettings. … your version is even better :slight_smile: , thanks for posting.

@mannyfresh2500 you should add the button device under “My Devices”, not “My Device Handlers”.

1 Like

I too tried the revised smartapp, by stephack. I like the ability to enter/change the MAC address on the phone itself.

The button I added was under My Devices. I ran a packet capture capture looking for activity on ports UDP 7 or 9 from my linux machine using tcpdump but I didn’t see anything when I activated the button. This is what is registered on live logging: “Info postEventToEndpoint: event successfully posted.” So the button is executing the code in the smartapp but for some reason my hub is not sending the WOL magic packet.

I double-checked my MAC address of the “sleeping” computer. And I double-checked again that I can wake it up from another machine. Both the smartthings hub and the target machine are on the same switch, same subnet, as is the linux box that can send the WOL packet just fine.

I’m missing something but I’m not sure what it is. Do you have to turn on something on the hub to enable to access the newer API commands? I’m at firmware version 20.00017.

I’ve been using Virtual Switches and a CoRE Piston for this for a while now. I haven’t tried it in webCoRE because I am a firm believer in, “If it ain’t broke…” Doesn’t work with WiFi connected PCs…I use Smart Plugs to turn them on.

I use Host Pinger to check the status of network connected devices.

2 Likes

HI @mannyfresh2500, make sure that your MAC address is in CAPS perhaps?

I’ve not tested the below, but it should work… this is a device handler, install it make sure to select a location and hub when adding it, otherwise it wont work!.. once it is added you can add in the MAC address without the colons or hyphens in the device settings…

Give it a go let me know if it works… worth a try I expect!

// Device MUST be associated to a location for it to work.

metadata {
	definition (name: "WOL", namespace: "jebbett", author: "Jake Tebbett") {
		capability "Actuator"
		capability "Switch"
		capability "Momentary"
    
	}

	tiles {
		standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true) {
			state "off", label: 'Push', action: "momentary.push", backgroundColor: "#ffffff", nextState: "on"
			state "on", label: 'Push', action: "momentary.push", backgroundColor: "#53a7c0"
		}
		main "switch"
		details "switch"
	}
}

preferences {
input("myMac", "text", title:"MAC Address without colons", required:false, displayDuringSetup:true)
}

def push() {
	def result = new physicalgraph.device.HubAction (
    "wake on lan ${myMac}",
    physicalgraph.device.Protocol.LAN,
    null
)
log.debug result
}

def on() {
	push()
}

def off() {
	// nothing
}

I think you need to use sendHubCommand(result) for the HubAction to work.

1 Like

Codes taken from the official ST documentation: (not saying that’s not wrong btw)

http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-device-type.html#wake-on-lan-wol

You need to either return the HubAction object from your method or use sendHubCommand().

I was just thinking about using WOL as a hack in your Host Pinger smart app to test if the hub is offline, but it’s probably not going to work. I have dual WAN set up on my ASUS router, and I use your app and my Bash script to detect the primary and failover WAN status. When the failover happens I want to turn on an orange light behind my TV so that I can stop streaming video using the secondary WAN (a mobile hotspot). The problem is during failover the hub is disconnected from the cloud and won’t reconnect till minutes later. The device status has been changed in the cloud and I can get a text message right away, but the hub seems to either miss the notification or receive it much later (sometimes with the next status change), and the light doesn’t get turned on.

Sorry to go off topic here, but as a newbie I am not allowed to add more replies to your Host Pinger thread.

This is driving me nuts. I just can’t get it to work. I am very grateful of all the help on from all of you. This is what I’ve done to troubleshoot, so far:

I can successfully wake the PC by sending a magic packet from my ethernet-connected Mac using “wakeonlan”, installed via brew. The Mac, the sleeping PC, and the smartthings hub are all on the same switch, same subnet, etc.

I can wake the PC using an app called Fing on my Android phone which is connected to my router via wifi, same subnet, etc. The router and switched are connected together (different floor).

The linux machine that can also send a WOL packet to the PC is also on the same subnet, switch, etc as the Mac and the smartthings hub.

I verified the transmission of the magic packet from my Mac and my smartphone by using wireshark on my Mac with a filter on UDP port 9 and “wol” as the protocol. The packet was captured and I verified the source.

I am not seeing the magic packet being sent from the smartthings hub, even though the debug output in the logging of the device handler says “wake on lan 001122334455” (substitute those numbers for my PC’s MAC address). I tried both CAPS and lower case but got nothing. It’s almost like the hub is either being blocked externally from transmitting the packet or something in software isn’t running the actual API call to produce the packet.

Feeling really stupid right now. Feel free to suggest anything, no matter how obvious. And thanks again for taking the time to help me.

My code uses the sendHubAction so I’m not sure why it isn’t working for @mannyfresh2500. I have my MAC as all caps but I doubt that matters…worth a shot though.

When I hit my momentary button I don’t see anything in the logs but it just works…so I’m not sure why your logs are generating that event.

Also the App should remove any “:” or “-” automatically.
@mannyfresh2500, the virtual device you are using is a momentary button tile, correct?

I’m not sure I understand what you are saying here. This should be installed as a smartApp not a device handler.

I’m using the other version, by @Entityxenon

The line “log.debug result” is what’s making the output that says "wake on lan " . I’m actually learning quite a bit as I’m fighting with this.

The virtual device I used is a momentary button tile. Even @Entityxenon’s version which uses a device handler is based on that type of device.

I’m going to remove the device handler, smartapp, and virtual button and start over. Again, I’ll try all the methods given so far, one at a time to see what works and will report back.

Ahh…must have missed that post…now I understand @camedia’s post. He is correct, @Entityxenon DTH probably won’t work because you would need the sendHubAction.

See excerpt from documentation below:

When you create an instance of a HubAction, you provide details about the request, such as the request method, headers, and path. By itself, HubAction is little more than a wrapper for these request details.

It is when an instance of a HubAction is returned from a command method that it becomes useful.

When a command method of your Device Handler returns an instance of a HubAction, the SmartThings platform will use the request information within it to actually perform the request. It will then call the device-handler’s parse method with any response data.

Without sendHubAction it would have to be returned from a command to execute. I’m not a programmer…just know enough to be dangerous…so I could be misinterpreting the documentation.

I just tried @psecto’s smartapp. Still getting the above error when setting the momentary button tile up as in the 2nd screenshot from 2 days ago. I installed the smartapp on my phone and selected the button tile as the device and activated it. Nothing happened. Didn’t see any WOL packets sent by the hub.

Now I’m going to remove that one and start over with @stephack’s version.

I get nothing. I’ll power-cycle the hub. Still got nothing.