'Switch' that uses wake_on_lan for on, and command_line for off?

Is it possible to create a single “switch” that could use the command_line platform to turn a device off, and the wake_on_lan platform to turn a device on?

Looking at: https://home-assistant.io/components/switch.command_line/ it seems that you’d end up having to create two different switches because command_line and wake_on_lan are different platforms.

Essentially I’d like a single switch that can turn a server on via wake on lan (have this already), but also turn it off. I was hoping to turn it off by calling a URL that would trigger a sleep/hibernate command I already use on the server itself.

Anyone know if this is possible and if so, what kind of yaml construct would do it?

This is what you want! https://home-assistant.io/components/switch.template/

1 Like

Thanks! I was just replying to myself to say possibly this, but that it’s so confusing I don’t understand how to use it. I’ll try reading it for the 4th time and have a play.

Another option is the wake on lan switch that provides a seperate option for the off command.

https://github.com/SilvrrGIT/HomeAssistant/blob/master/switches.yaml#L74

3 Likes

Or actually try @silvrr option. I just remembered that’s what I use :rofl:

1 Like

And yeah template switches threw me for a loop when I first saw them.

lol yes, that would be much easier, especially as I have wake on lan working already. I’ll give it a go and try and make it run a curl command :smiley:

Awesome, working now. Thanks both. Config is:

switch:
  - platform: wake_on_lan
    mac_address: 'B0-4E-D4-79-B7-1N'
    name: nuc
    host: 192.168.0.29
    turn_off:
      service: shell_command.turn_off_nuc

shell_command:
  turn_off_nuc: "/usr/bin/curl -u HomeAssistant:REDACTED http://192.168.0.29:2020/power/off/index.php"

and this works with emulated hue :smiley:

The only problem I’m seeing is that when turning it on, the switch toggles on, but then off for a while, and then back on. Minor issue though that doesn’t really impact anything. At least I can finally turn it off easily :slight_smile:

Hey there,
I’m using the same set up for my Wol, but the switch will not stay in the off position. It keeps turning on.

Any one else encounter this and know how to fix it please?

What are you using to provide the switch the state of your machine? Post your WOL config .

I am using a program called “sleeponlan” running on my pc that receives the shell command.
I can turn on/off my pc fine. Its just the switch stays on and is not giving me the correct status.

switches.yaml:

- platform: wake_on_lan
  name: Beast PC
  mac_address: "90:2B:34:36:B0:76"
  host: 192.168.1.13
  turn_off:
    service: shell_command.turn_off_beast

In my congfiguration.yaml:

shell_command:
turn_off_beast: ‘curl http://192.168.1.3:7760/poweroff

EDIT: I just worked out my error - I had my Pi’s IP address in “host” and not my PC. #usererror

thank you so much. it´s working very well. the switch even detect the correct status of my computer :slight_smile:

it is also possible with a rpc shutdown add-on and a simple shutdown script:

switch:
  - platform: wake_on_lan
    mac_address: "44-8A-5B-89-6F-77"
    name: trevor
    host: 192.168.0.8
    turn_off:
      service: script.shutdown

and the script to run the addon:

shutdown:
  sequence:
    - service: hassio.addon_stdin
      data:
        addon: core_rpc_shutdown
        input: steam

Hope that someone can help. I am trying to create a switch to turn a server on and off.
On is working fine, but the off settings dont seem to work.
Anyone any idea what I am doing wrong?

switch:
  • platform: wake_on_lan
    name: Server
    mac_address: “54:04:a6:2b:c0:b1”
    host: 192.168.178.15
    turn_off:
    service: shell_command.turn_off_Server

shell_command:
turn_off_Server: ‘ssh [email protected] sudo poweroff’

My configuration checks gives this error:
Invalid config for [shell_command]: [turn_off_Server] is an invalid option for [shell_command]. Check: shell_command->shell_command->turn_off_Server. (See /config/configuration.yaml, line 193). Please check the docs at https://home-assistant.io/components/shell_command/"

Check your spacing and names need to be all lowercase in my experience.

It was the capital S in the server name. I was not aware it all had to be lowercase. Thanks!

Couple questions on this:

  1. are you able to add to wake_on_lan your own additional parameters? I am working on a custom linux script for shutting down different kinds of computers and want to pass parameters to it that aren’t in the manual. So obviously I need “host” or IP but also want to be able to define username and password and also the type of computer like “Windows7” or “Windows10” or “Mac” or “Linux” or whatever that will trigger different shutdown options in the same script.

  2. Can you pass parameters like host to the script in the turn_off: area? Hate to create hard coded shell commands for every one of them.

So for me I have a shell script that takes three parameters right now and is called like:

./shutdown.sh -a parameterA -b parameterB -c parameterC

parameterA is the IP of the device
parameterB is the username with ability to shutdown the system
parameterC is the password of parameterB user

Then there will eventually be a parameterD for the type of system or shutdown method if I can add values to the wake_on_lan without it complaining it’s not valid.