Wait Template with a Time out Condition

I have an automation that powers the power socket of my computer, when motion is detected in the office.
When the PC is powered down and no further motion is detected the power socket should be turned off again. As the motion detector is not detecting me sitting at the desk, I am checking if the pc is detected in my network. That device tracker has a little lag, which is why I have included a timeout variable.

I believe the automation was running fine, but I have recently noticed that the power is being turned off immediatly, when the motion detector goes back to ‘no motion’, as the devices tracker has not yet noticed the PC has been powered on (i.e. the time out is somehow not running correctly).

Office Power Socker Automation
- alias: Power - Switch Office Power ON/OFF
  id: 'power_sockets_office'
  - platform: state
    entity_id: binary_sensor.motion_office
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_office
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: device_tracker.pc
    to: 'not_home'
  max_exceeded: silent

  condition: []

  - choose:
    - conditions:
      #Turn office power sockets on 
      # if motion in office is detected
      - condition: template
        value_template: "{{ states('binary_sensor.motion_office') == 'on' }}"
      - service: switch.turn_on
        entity_id: switch.socket_pc
    - conditions:
      #Turn office power sockets off 
      # if no more motion in office is detected and
      - condition: template
        value_template: "{{ states('binary_sensor.motion_office') == 'off' }}"
      # ... the PC is powered off
      - wait_template: "{{ is_state('device_tracker.pc', 'not_home') }}"
        timeout: "{{ states('input_number.no_motion_wait_office') | int }}"
        continue_on_timeout: false #stop here, if PC is detected running ...
      - service: switch.turn_off
        entity_id: switch.socket_pc
    default: []

input_number.no_motion_wait_office is currently set to 600 (s).

Any reason why the timeout is not applied?

I think the time out part needs to be formatted like this:

  milliseconds: "{{ states('input_number.no_motion_wait_office') | int }}"

However, I’ve not tested this.

The value supplied to timeout follows the same rules as for delay. It requires the value to be in this format: HH:MM:SS or by supplying an additional time option (hours, minutes, seconds, milliseconds) with an appropriate numeric value.

You said the input_number reports 600 seconds so use the seconds option.

      - wait_template: "{{ is_state('device_tracker.pc', 'not_home') }}"
          seconds: "{{ states('input_number.no_motion_wait_office') }}"
        continue_on_timeout: false

Nope, unfortunatly that was not it.
I do have other automations, where I have a the exact same template (different helper) in a delay-statement. There it is working correctly, without the specifcation. I believe seconds is the default value if only one integer is provided.

I have updated this automation, regarding your feedback. The result is still the same.
The log shows:

  remaining: 600
  completed: true

But the full automation had a runtime of 0.28 seconds. So obviously it did not wait the full 600s.
And as it is turning off the plug, it did also not stop the automation.

… Writing these words I am thinking there is a logical flaw in the automation.
It is covering the case that I turn off my computer and leave the office.
When the motion detector stops detecting motion, my router may still list the PC as home, as there is always a lag with the device tracker. This automation will now wait for 600s and turn off the plug only if the PC is set to not_home within this threshold.

It does not however cater for the lag, when powering the computer. It seems that it takes longer for the computer to be recognized as home, as the motion detector to stop detecting motion. However this case is currently not covered at all by my automation …

If the entire automation took a mere 0.28 seconds to execute, it means the wait_template didn’t have to do any waiting because the device tracker’s state was not_home.

Post the automation’s trace.

As said, I have written the automation some time in the past and it seems that this part of the automation was meant to turn off the plug, when the PC is powered down and the room is left, whereas the motion detector will start detecting motion. When the motion is no longer detected, the device tracker is usually still tracking the PC as being online. As such I have included a wait_for condition.

However I have now noticed that the device_tracker is also not fast enough to notice the PC is online, by the time I sit at my desk and the motion detector no longer sees motion, however the automation was not build for this. I need to go back to the drawing board.
Potentially I will use the power metering instead of the device_tracker, as that should be immediate. Just need to measure the power consumption on the various electrical items connected to it.