[SOLVED] Virtual Switch State Wake_On_Lan

Hi all!

I have a virtual switch for turning my computer on and off (I first made this using the wake-on-lan switch platform but switched to a normal template platform switch calling the wake-on-lan service for the reason in this post).

All works well but as you can imagine, the state of the switch isn’t reliable as if I turn on by hand it goes out of sync. So I was trying to use the value_template setting to use a sensor provided by HASS.agent to determine the state.

However, I am a tad confused on how the value_template works :confused: I tried the following but this didn’t work:

 - platform: template
     switches:
       computertest:
         value_template: "{{ is_state('sensor.***_activewindow', 'on') }}"
         turn_on:
           - service: wake_on_lan.send_magic_packet
             data:
               mac: **:**:**:**:**:**
         turn_off:
           service: button.press
           target:
             entity_id: button.***_shutdown

I experimented with the on variable but to no joy and the documentation has a surprising lack of detail on how that snippet of code is actually processed so I don’t even know if ‘on’ is saying turn the switch on or if the sensor == on.

The sensor will report the name of the open tab, such as now it would report Home Assistant Community or the name of the program I was running, and when the PC is off, it would display unavailable. I simple want it so that if it is unavailable, show the switch as off and if anything else, show it as on. (one potential issue I saw on another post is if the sensor doesn’t report quickly which this defiantly wouldn’t as the sensor wouldn’t become active till the computer started so it would need to change after the fact)

 - platform: template
     switches:
       computertest:
         value_template: "{{ not is_state('sensor.***_activewindow', 'unavailable') }}"
         turn_on:
           - service: wake_on_lan.send_magic_packet
             data:
               mac: **:**:**:**:**:**
         turn_off:
           service: button.press
           target:
             entity_id: button.***_shutdown
1 Like

I assume you mean this:

To correct that in the WoL switch all you have to do is supply a host: option. e.g.

- platform: wake_on_lan
  mac: "f0:2f:74:**:**:**"
  host: 10.1.1.2
  name: Lounge PC
  turn_off:
    service: shell_command.turn_off_lounge_pc

Miss-understood your post, sorry.

Will give that a try

I tried this using the local ip of my computer (192.168.1.XXX) but it doesn’t work :confused: do I need to do something specific like a port or edit my computer settings somehow?

It works for everybody else. Assuming you got the ip address right, you may have to wait for the ping polling interval for the state to change.

Also FYI: You don’t need to obscure local ip addresses. They are not routable on the internet. No one can use them against you.

1 Like

Looks like my PC isnt responding to any ping for some reason :confused: will look into it and see if works after fix that issue

2 Likes

Update:

Got it to work using host, just had to enable the ping and then actually restart Home Assistant; I had assumed reloading the YAML would of applied addition of host but apparently needed full restart to apply