Switch off lighting after a certain amount of time (cancelqueuedcommands)

Hi,

i am moving from domoticz to home assistant and have difficulty getting a certain automation right.

Basically i am converting this dzVentz code to an automation:

if (item.name=='PIR Garage') then|
  if (item.active) then
    SwitchOn('Garage') 
   else
      domoticz.devices(Garage).cancelQueuedCommands()
      domoticz.devices(Garage).switchOff().afterMin(GarageTimeout)
    end
end

So i created an automation in the UI, which looks like this in the YAML editor

alias: Garage gestopt met detecteren beweging
description: Zet de lamp weer uit als er geen beweging meer is
triggers:
  - type: no_motion
    device_id: 325a1d8a9ff64e49f6c5771072a25c5b
    entity_id: 84d165e793530f962c73867164ab21b8
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - type: turn_off
    device_id: 4783fd1c2866e1e8a286f49c234515a2
    entity_id: 54fec7f7e96e17e864cc6403c8c0d1f7
    domain: switch
mode: single

Issue is that if within the 10 mins, the pir starts and stops detecting motion again, the events are crossing each other, sometimes causing the light to switchoff when someone is still in my garage.

In domoticz i prevented this by using the cancelqueuedcommands call. How can i do this in Home Assistant?

mode: restart

This will remove the previous instance of the automation each time the device goes to no_motion and wait for 10 minutes

Tx, 2 good hints. I will give both a try!

I tried to use this template, so i adjusted

alias: Garage PIR
description: Handle de PIR events van de garage af
triggers:
  - entity_id: binary_sensor.garage_pir_garage_garage_pir_garage_motion
    to: "on"
    trigger: state
  - entity_id: binary_sensor.garage_pir_garage_garage_pir_garage_motion
    to: "off"
    for:
      minutes: 2
    trigger: state
actions:
  - target:
      entity_id: switch.garage_lamp_garage_lamp_switch
    action: light.turn_{{ trigger.to_state.state }}
mode: restart

But the effect is nothing happens. when i check the logs i get a

2024-10-22 20:33:45.086 WARNING (MainThread) [homeassistant.helpers.service] Referenced entities switch.garage_lamp_garage_lamp_switch are missing or not currently available

The entity " switch.garage_lamp_garage_lamp_switch " definitely exists. So do you have a clue?

UPDATE:

Never mind, i found the solution “light.turn_{{ trigger.to_state.state }}” needed to change to “switch.turn_{{ trigger.to_state.state }}” in the yaml and now it works…

tx for the help!