State of entity for wake-on-lan

Hello!

I have the following entity set up:

switch:
  - platform: wake_on_lan
    name: "PC"
    mac: "<mac-address>"
    broadcast_address: "<ip-address>
    turn_off:
      service: mqtt.publish
      data:
        topic: iotlink/workgroup/pc/commands/suspend
        payload: ""

As you can see, I’m turning it on via WoL, and disabling it via IOTLink. Unfortunately, the state of this entity does not depend on whether or not the computer is turned on or not:


In reality, the PC is running. It does yellow only after I explicitly click it ON on the dashboard.

That causes issues with scenes - if HA thinks it’s disabled, it doesn’t call the turn-off service.

How can I work around this issue (i want to use scenes feature to set proper state of this machine).

You need a sensor to determine the state of the PC. Like a ping binary sensor.

Then you can create a template switch that has state feedback to determine the device state using a value_template

Actually, it’s built in the WOL switch.
If you add the host option, it doubles as a ping sensor.

3 Likes

Thanks @koying, came across the very problem and obviously didn’t read the documentation too in-depth. Working now. Also: Great to see such a coding celebrity (Team SPMC here!) lurking around here :wink:

Edit: Technical question: I already included the broadcast_address in my yaml beforehand. When would broadcast_address and host not be the same to require this extra config?

1 Like

I already included the broadcast_address in my yaml beforehand. When would broadcast_address and host not be the same to require this extra config?

A computer can have more than one IP address. Imagine a situation where you use one VLAN to route WoL traffic and another for regular IP traffic.

But yeah - very niche situation :slight_smile:

Sadly mine doesn’t pick up the state. Any ideas why?

switch:
  - platform: wake_on_lan
    mac: "xx:xx:xx:xx:xx:xx"
    host: "192.168.0.5"
    name: "office-pc"
    turn_off:
      service: hassio.addon_stdin
      data:
        addon: core_rpc_shutdown
        input: office-pc

Double check if 192.168.0.5 is the IP of your PC. Also, make sure it replies to ping requests, you can do it by executing ping 192.168.0.5 from your command line. Most likely, it won’t reply, probably due to firewall rules. If you’re on windows, you can follow this tutorial to allow this traffic.

3 Likes

Thank you! Once I enabled the inbound rule for “File and Printer Sharing (Echo Request – ICMPv4 – In)” it worked! This should probably be included in the documentation.

2 Likes

I have a similar problem, I use a 5gbe to USB network adapter, it responds to ping requests even when the computer is asleep. I’m curious if there is an alternative way to determine if the pc is asleep or not?

Edit: solved this on my own. Used the links below to create a virtual ethernet adapter and set a seperate IP and mac address for it. Then used that IP address for the host setting in my yaml. Everything works as expected now.

Impressive example of over-engineering :slight_smile: I love it!

I would try however looking through BIOS settings - maybe there’s an option for native NIC not to reply to echo/ping requests when PC is on standby.

edit: ahh, it’s a USB dongle… Hmmm, then maybe looking through the device settings for that USB device?

Pretty sure I tried all of that but it was some time ago. Recently upgraded to windows 11 and this seems to have broken things so thanks for the reminder on how I did this the first time

very nice :slight_smile: this worked perfectly. thanks.

This switch worked well before installing a 10gpbs network card, now it is always on, it responds to pings even when suspended.

image

How could I make the switch state be the state of the HASS agent monitorpowerstate → PowerOn/PowerOff?

I have tried with

value_template: “{{ is_state(‘sensor.ryzen_monitorpowerstate’, ‘PowerOn’) }}”

But I don’t know where to put the code, it gives an error, I’m a little rusty with yaml.

  - platform: wake_on_lan
    mac: "xx-xx-xx-xx-xx-xx"
    name: "Ryzen"
    broadcast_address: "192.168.1.6"
    broadcast_port: "9"
    host: "192.168.1.6"
    turn_off:
        service: button.press
        target:
            entity_id: button.ryzen_sleep

Edit: got it!

  - platform: template
    switches:
      ryzen_test:
        friendly_name: Ryzen
        value_template: "{{ is_state('sensor.ryzen_monitorpowerstate', 'PowerOn') }}"
        turn_on:
          - service: wake_on_lan.send_magic_packet
            data:
              mac: "xx-xx-xx-xx-xx-xx"
              broadcast_address: "192.168.1.6"
              broadcast_port: "9"
        turn_off:
          service: button.press
          target:
            entity_id: button.ryzen_sleep
        icon_template: mdi:desktop-classic
1 Like