Ensuring that PC turns on

The purpose of this automation is to ensure that my PC turns on. I use a Switchbot bot that presses on the PC power button every weekday at 8:45 am. However, sometimes the PC won’t turn on, so it takes a few more tries to get it to turn on.

I attempted to make this automation, but I think something is missing or it’s wrong? It didn’t fire correctly when it was time for it.

alias: Turn PC on
description: ''
trigger:
  - platform: time
    at: '08:44:00'
condition:
  - condition: time
    before: '08:46:00'
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
    after: '08:44:00'
  - condition: and
    conditions:
      - condition: not
        conditions:
          - type: is_plugged_in
            condition: device
            device_id: 24b369d0bced7aa81e0ffd4a282977e2
            entity_id: binary_sensor.desktop_93js9fs_power_status
            domain: binary_sensor
            for:
              hours: 0
              minutes: 0
              seconds: 40
action:
  - type: turn_on
    device_id: 1708007650dbf521391a1821daf32932
    entity_id: switch.pc_turner
    domain: switch
mode: single

I added the 40 seconds in the end so that it can retry within a reasonable time, instead of spamming the turn on command, if the PC status is not plugged in.

I’m using IoTLink and MQTT Broker for establishing connections between HA and my PC.

Any suggestions is greatly appreciated!

Ok there are a couple of things here:

This automation will only trigger once at exactly 0844 hours, there is no retry or repeat in the automation.

As there is only one trigger of 0844 hours there is no need to have before and after times here as it will always be true based on the initial time trigger, so this can be simplified somewhat by:

  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri

All conditions are automatically ‘AND’ so there is no need for the above and, you simply need:

condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  - condition: not
      conditions:
        - type: is_plugged_in
          condition: device
          device_id: 24b369d0bced7aa81e0ffd4a282977e2
          entity_id: binary_sensor.desktop_93js9fs_power_status
          domain: binary_sensor
          for:
            seconds: 40

However as mentioned, there is no repeat action here so this will trigger once only at exactly 0844 hours.

If you want to have the automation repeat the action until a certain state is true, in this case the power_status of plugged in, then you need to use a repeat until, something like this

action:
  - repeat:
      until:
        - condition: device
          type: is_plugged_in
          device_id: 24b369d0bced7aa81e0ffd4a282977e2
          entity_id: binary_sensor.desktop_93js9fs_power_status
          domain: binary_sensor
      sequence:
        - service: switch.turn_on
          target:
            entity_id: switch.pc_turner
        - delay:
            seconds: 40

This will trigger once at exactly 0844 hours and action only if day is a weekday and will switch on the plug every 40 sec’s until the state of your binary sensor is on.

Its generally better practice to use states rather than devices, so you could set the condition to simply be:

  - repeat:
      until:
        - condition: state
          entity_id: binary_sensor.desktop_93js9fs_power_status
          state: 'on'
1 Like

I’m just wondering…doesn’t the PC support WakeOnLan??
Most PC’s do, and it would make things a lot easier… no additional hardware needed, only a magic WOL package to the MAC address…

1 Like

but Wake on Lan is when PC is on sleep. PC boots from complete shut down

no…wake on lan is for PC’s that are shutdown.
So unless the power is out, it will wake up.
(so the network’s led should show an orange light when in shutdown)

(at least the 1000+ pc’s at my customers work like that)

I tried it before, but PC wouldn’t wake up on LAN

You might need to enable Wake on LAN in the BIOS first.

Also your conditions might prevent the automation from reaching the action part:

trigger:
  - platform: time
    at: '08:44:00'
condition:
  - condition: time
    before: '08:46:00'
    after: '08:44:00'

So when it triggers at 08:44:00 for a whole second long the condition „after 08:44:00“ will not be met. (Only at 08:44:01 „after“ 08:44:00 will be true)

i concur…it needs to be enabled in the BIOS.
There are several tools available to generate the magic WOL package. F.e. you can use WOLcmd from Windows machines.

Powered-down or turned off computers capable of Wake-on-LAN will contain network devices able to “listen” to incoming packets in low-power mode while the system is powered down. If a magic packet is received that is directed to the device’s MAC address, the NIC signals the computer’s power supply or motherboard to initiate system wake-up, in the same way that pressing the power button would do.

See also Wake-on-LAN - Wikipedia

But not all PC’s support WOL. Mine does, my son’s does not. Spent hours in the BIOS and in Windows, never got it to work. In the end, I bought this little gadget:

Hey @francisp, that’s what I said :yum:

1 Like

@francisp Some older computers still required a wired link between mobo and networkcard:

It has integrated Ethernet, and no connector for using a network card with WOL.

1 Like

I have it enabled in BIOS and I have magic packet enabled in device manager too.

Worth to take a look at https://www.howto-connect.com/how-to-activate-on-wake-on-lan-windows-10/