Switch to retain state?

Good Morning,

I have a template switch that I use to turn my computer on and off (well to sleep).

They work well with my hue bridge so I can say “alexa, turn on computer” or “alexa, turn off computer” and it works a treat. I have this switch in it’s one group on my main screen called Knotty PC. Problem is I can turn on my pc with the switch but it revents back to the off possiition after 2 seconds. So I can only really ever turn my computer on from the Hass.io Dashboard.

Switch code below (I use pushbullet to send the command to my pc and listen with Evenghost, I prefer it this way as I don’t like keeping my computer password anywhere in a digital format).

  pc_contol:
    value_template: off
    friendly_name: "Computer"
    turn_on:
      service: switch.turn_on
      data:
        entity_id: switch.wake_on_lan
    turn_off:
      service: notify.computer
      data:
       message: gotosleep

I have seen a couple of posts on here with similar issue and using input booleans to help retains state.
I tried messing with this but with no joy and deleted it out of frustration. However from memory what I tried was defining a ninput_boolean for the pc and had an automation to be triggered by the boolean state from on to off and off to on (2 automations) each triggering parts of the above in an automation rather than a switch. However this just ended with the same results.

I’ve not tested that setup, but I’m using the standard wake_on_lan component and it works great for me. I can turn on (Wake), off (Sleep - using Airytec Switch Off) and see the current state at all times. The added bonus is I’m not relying on any external services, so if the internet goes down, I don’t lose functionality. No usernames or passwords need to be stored within the HA configuration either, just the IP and MAC Address of the target PC.

@Sitrate, does that work with Hass.io though? I thought it had to call shell commands and didn’t know if that was possible in hass.io? I know it is in hasbian.

Edit: I do use the standard wake on line (defined elsewhere in my config) for on.

It requires a simple one line CURL command like this:

curl -k http://192.168.0.100:8585/?action=System.Sleep

From what I understand CURL commands work fine in hass.io.

As you’re already using the wake_on_lan component the state should be set anyway. Have you specified a “host” parameter when creating the WOL switch? HA will ping this host to determine if it’s online or not.

I’ve just tested on a hass.io VM and this works for me:

switch:
  - platform: wake_on_lan
    name: My PC
    mac_address: "70-4E-7B-24-57-07"
    host: 192.168.0.100
    turn_off: 
      service: shell_command.suspend_pc

shell_command:
  suspend_pc: 'curl -k http://192.168.0.100:8585/?action=System.Sleep'

Feel free to amend and use if you wish.

@Sitrate, first of all thanks for your help and input.

Something odd is happening, my original switch (for WOL) was:

- platform: wake_on_lan
  mac_address: "FC:AA:22:06:AE:3A"

When I update it to:

  - platform: wake_on_lan
    name: "Knotty-PC"
    mac_address: "FC:AA:22:06:AE:3A"
    host: 192.168.0.144
      turn_off: 
        service: shell_command.suspend_pc

The switch voids and is no longer visible in states.

Just to help anyone who is googling this issue and came here:

First of all the above wasn’t working because of the “name:” field (I believe it was the speachmarks but I ended up deleting the whole line.

I got the switch to retain state by using input boolean (not the most elegant approach but it works).

I defined the input_boolean:

input_boolean:
      pc_power_on:
        name: Computer
        initial: on
        icon: mdi:desktop-tower 

Then created two automations based on the state of the input_boolean:

All done in browser (I’m typing this up for idiots like me, pretty obvious stuff when it clicks):

  • Goto automation
  • Goto “+”
  • Name it (I called the first one ‘Turn PC On’)

Trigger:
Trigger Type: State
Entity: input_boolean.pc_power_on
From: Off
To: On
Action:
Action Type: Call Service
Service: switch.turn_on
Service Data:
{
“entity_id”: “switch.pc_contol”
}

The second on was just the above but swapped round and service changed to switch.turn_off.

I used my switch from above and it all works nicely now.

1 Like