Cut main power off after PC shut down

Hi,
I’m trying to set up a script which would cut the main power after the desktop pc is shut down. I have build this computer desk with all fancy LEDs, pc and monitors etc. All equipment is powered from the same extension lead, which is connected to Sonoff POW. I have configured the POW in HA, so I can easily power off everything with a switch in HA. And here is the problem: The POW always cuts off the power before the desktop pc properly shut down. So I’m trying to set up a script which would automate the process. So far I have set up two scripts:
One to shot down the pc and the second one switch of the POW. Now I’m trying to combine these two scripts into one, which can be triggered by the button from the dashboard. The goal is to shut down the pc first, and then (when the pc is off) execute the script to switch of the POW. My script configuration looks like this, but it is not working. The POW script does not execute. I added the condition based on the state of the binary sensor (to check whether the pc is off or not), but still the POW won’t turn off.

  turnoff:
    alias: TurnDeskOFF
    sequence:
    - service: script.my_computer_shutdown
    - condition: state
      entity_id: binary_sensor.desktoppc_connected
      state: 'OFF'
    - service: script.turnpowoff
    icon: mdi:power
    mode: restart

From the first look, there is a small issue with syntax in the above script. The state must be in lower case. Try this.

  turnoff:
    alias: TurnDeskOFF
    sequence:
    - service: script.my_computer_shutdown
    - condition: state
      entity_id: binary_sensor.desktoppc_connected
      state: 'off'
    - service: script.turnpowoff
    icon: mdi:power
    mode: restart

But here also there could be a problem. The first action is to shutdown the PC. As soon as that script is called, the script will check the binary_sensor state which I believe could be a ping sensor to PC IP address. I dont know what is the update interval set for this sensor but please not that, if this a ping sensor it is highly unlikely that this changes to of state as soon as the shutdown script is executed. So you need to add a delay in between the calling shutdown and checking condition. This should be equal to or greater than the update interval of the sensor. Hope you have got it.

Rather than a delay, wait for the shutdown to occur:

  turnoff:
    alias: TurnDeskOFF
    sequence:
    - service: script.my_computer_shutdown
    - wait_for_triiger:
      - platform: state
        entity_id: binary_sensor.desktoppc_connected
        to: 'off'
        for:
          seconds: 10 # just in case the network disconnects before fully shut down, you may not need this. 
    - service: script.turnpowoff
    icon: mdi:power
    mode: restart

You can also add a time-out to do something if the computer does not shutdown after a certain time (notify you for example) See: https://www.home-assistant.io/docs/scripts#wait-timeout

1 Like

Tom,
I tried your modification to the code, and the POW switch turned off about 10s after the PC shutdown was initiated. However, at that time the pc shutdown was not completed yet.
So one solution would be to extend the wait time long enough (i.e couple of min, just to be on the safe side). But what I would prefer is that the script for switching off the POW switch would trigger only when the pc is completely shut down (binary_sensor.desktoppc_connected state = off)

That means your sensor binary_sensor.desktoppc_connected is changing to the off state before the pc is shut down. Otherwise it would not pass the wait_for_trigger.

How are you generating this sensor?

I have set up the IOT Link on the desktop pc, and a binary sensor in the HA. here is the code of the sensor:

  - platform: mqtt
    name: "DesktopPC - Connected"
    unique_id: desktoppc_connected
    state_topic: "iotlink/parallaxvu/marosdesk/lwt"
    value_template: "{{ value }}"
    payload_on: "ON"
    payload_off: "OFF"
    device_class: "connectivity"
    qos: 1

Did you correct the typo in tom’s code?

two i’s in trigger

1 Like

yes, i noticed that and used correct spelling…

It seems your IOT link sensor is one of the first things shut down.

Try using a ping binary sensor instead.

Thanks Tom, that works well. I just needed to increase the delay interval to 30s, as it seems that my network card become "unresponsive " to ping about 10s before the pc is completely shut down. But changing the binary sensor to ping works well. Now I just need to figure out how to integrate it into button…
Thanks