#Change state of switch without sending its commands

105 messages · Page 1 of 1 (latest)

ebon sage
#

I have a switch that sends wake on LAN/shutdown commands to my pc depending on its respective on/off state. However, if I switch off the pc any other way i.e. via the start menu, the switch's state doesn't change to off.

I can do this using a device tracker's state as a condition but I just want to change the state without sending a command.

At the moment if the pc is offline then it changes the switch state to off which also sends a shutdown command that gets backlogged. It only arrives when the pc is back online, causing it to receive a shutdown command the next time I switch it on. This obviously leaves me in an infinite loop of an unusable pc.

crude steppe
#

Hey @ebon sage you should modify the switch behavior so that the state change doesnt trigger the shutdown.

#

you can try with templete switch.

#

if u interested in this method, let me know.

ebon sage
#

Would there be another way to do it?

dreamy widget
#

How do you have the switch setup today? Is it a template switch? Input bool?

ebon sage
dreamy widget
#

Can you add a command_state to indicate whether the host is on or not?

ebon sage
#

i'm a poor old noob

dreamy widget
#

That's another attribute of your switch. Can you give it a command to determine if the PC is on (needs to return 0 directly or via the value_template)

#

That will auto-update the switch's state to on/off based on that command

ebon sage
#

ohhh i guess that's what i've been looking for

dreamy widget
#

That way, if you turn the PC on/off external to that switch, it'll still be accurate

ebon sage
#

so how would that be implemented? is command_state added in the automation?

dreamy widget
#

Just to be clear, this isn't an automation, it's just part of the command_line switch entity that you're configuring

#

Some examples are here

ebon sage
#

got it, thanks

#

so when the state is updated using command_state then it won't actually run the command_off command?

dreamy widget
#

Correct

ebon sage
#

got it, thanks

ebon sage
# dreamy widget Correct

hi just some follow-up questions, I'm struggling to understand how this will correlate with the automation that checks for the computer's status

#

apologies if it's obvious 😅

dreamy widget
#

Tell me more about the automation.... share the automation, too

ebon sage
#
description: ""
trigger:
  - platform: time_pattern
    seconds: /30
condition:
  - condition: state
    entity_id: device_tracker.my_pc_tracker
    state: not_home
action:
  - service: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.pc
mode: single
``` this is my automation which I had - in case it helps
dreamy widget
#

What are you concerned about

ebon sage
#

how to implement it with command_state and value_template

dreamy widget
#

That automation doesn't do anything with the actual state of your switch.... all it does it turn it off every 30m if you're not home

ebon sage
#

right so the way it tracks the pc home state is by pinging it every 30 seconds - so when it turns off then the next time it pings with no response and says not_home i just want it to change the state to off

#

that's my issue - switch.turn_off is also sending a turn off command but i'm not sure how to implement the command_state attribute

#

again apologies if it's obvious, it's my first time making my own switch using the command line

dreamy widget
#

These are 2 parallel things .... command_state is intended to be an actual command line command that will be run to determine the state of your PC

#

That's used to keep the state of your switch up-to-date

ebon sage
#

so could I run a ping command under command_state and see if there's a response? could I then fire that command under an automation?

dreamy widget
#

You could... but that's 2 separate things still

#

I'd suggest using the switches state after you get the set up to know the state of your PC

sleek bronze
#

There is a WoL switch, which can handle the WoL call, the shutdown command and the ping state

dreamy widget
#

You won't need to re-use that command

ebon sage
#

ahh i'm having a brain melter and can't understand it, really sorry

#

i would add command_state: "ping 1.1.1.1"? and then find some way to determine the state if it returns a value?

#

I found this online:

lusty oxideBOT
ebon sage
#

or can it be fired manually outside of the switch?

dreamy widget
#

You're really concerning yourself with the details too much, but it updates periodically according to the scan_interval, it's not a 1 time thing. That's how it captures state changes outside of HA

#

Like Jorg mentioned, you could use the WOL switch instead of the command line switch. That will handle ping state for you along w/ WoL power on

#

But the command above seems reasonable too, probably would be fine w/ -c 1 instead

ebon sage
dreamy widget
#

Yeah, it can do that

ebon sage
#

right

dreamy widget
#

But what you have w/ the command line switch is nearly there too

ebon sage
#
#

so I guess in my case I would replace the broadlink switch with the one I have in the command line

dreamy widget
#

If that won't work for you... I'd suggest transitioning to the WoL switch integration then instead of trying to compound workarounds

#

I think the actual issue is that it's using echo as opposed to exit. The command line switch, while not using a value_template, considers the exit code and not stdout

ebon sage
#

what does it mean by "expected ->0"?

#

should I be writing an actual HA action in there instead?

sleek bronze
#

Turn off has to be an action

#
    turn_off:
      action: shell_command.turn_off_TARGET
ebon sage
#

ah I see

#

thank you

sleek bronze
#

You can put the curl command in a shell_command and call that in the turn off action

#

And the host IP needs to be the one of the computer. Not Cloudflare's.

ebon sage
#

yeah i'm just using 1.1.1.1 as a placeholder 🙂

#
  - platform: wake_on_lan
    name: pc_toggle
    mac: "AA:BB:CC:DD:EE:FF"
    host: "1.1.1.1"
    turn_off:
      action: shell_command.shutdown_pc

shell_command:
  shutdown_pc: "curl http://1.1.1.1:5001/shutdown"
``` so like this?
#

or should the shell command be indented inside the switch?

sleek bronze
#

This is correct. Shell_command is its own integration.

ebon sage
#

I see - I'll give this a whirl and hopefully it should update the switch state on its own when I turn off the pc manually

#

thanks again for all your help, I've clearly bitten off more than I could chew 😅

ebon sage
#

I tried various methods but I'm not sure what the proper syntax is

sleek bronze
#

How is your config looking now?

ebon sage
#

i tried it as turn_off: action: shell_command.shutdown_pc

sleek bronze
#

There is a _ missing between turn and off

ebon sage
#

ah yeah it's in there i just typed this out manually

sleek bronze
#

Does the action shell_command.shutdown_pc exist in the dev Tools?

ebon sage
#

yes it appears under services

sleek bronze
#

If there is no typo or bad indentation in your real config, I see no reason this shouldn't work

ebon sage
sleek bronze
#

If it wouldn't work, I wouldn't expect a config check error.

ebon sage
sleek bronze
#

Where did you get that error message?

ebon sage
#

it was where you check the config.yaml file before restarting home assistant in the dev tools page

sleek bronze
#

And what HA version are you using?

ebon sage
#

Core 2024.7.3

sleek bronze
#

That explains it. The syntax with the action instead of service was introduced in 2024.8

ebon sage
#

Ahhh I see

#

I've been away for a while and didn't check for updates

sleek bronze
#

Now everything is working as expected?

ebon sage
#

It worked like an absolute charm thank you so much

#

Really really appreciate it

#

It does take some time to update the state of the button but I can absolutely live with that

sleek bronze
#

It is likely a compromise to prevent flooding the network with pings.

ebon sage
#

Absolutely

#

Thanks again

#

🙏

sleek bronze
#

Now the time has come, that we ask a service of you

#

Please add the "✅Resolved" tag in this post's ... menu

ebon sage
#

Will do 🙂