Turn off input_boolean with a delay fails

I’m trying to switch an input_boolean to ‘on’, leave it on for a few seconds and then switch it off.
Therefor I use this automation:

alias: Motion buiten verzamel
description: ''
trigger:
  - platform: state
    entity_id: group.motion_buiten
    to: 'on'
condition:
  - condition: state
    entity_id: input_boolean.motion_buiten_verzamel
    state: 'off'
action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.motion_buiten_verzamel
  - delay:
      hours: 0
      minutes: 0
      seconds: 7
      milliseconds: 0
  - service: input_boolean.turn_off
    entity_id: input_boolean.motion_buiten_verzamel
mode: single

This is created in Gui.
For some reason I do not understand, this input_boolean is switched on and directly switched off again. Without delay. What is wrong with this code?

Looks ok to me. What does the automation trace show?

Lets take a step back, what are you trying to do with this? The overall picture, not just this single automation.

I have another automation that looks like:

  • Trigger: input_boolean.motion_buiten_verzamel = on
  • condition: input_boolean.sound_on (this is a switch to turns the sound on/off)
  • start a script that plays a sound on a bluetooth speaker.

If the trigger is just the ´motion sensor group´ then the sound is played too many times.
I want to create a delay by using an input_boolean that stays on for a few seconds.

Just make an automation that only fires the sound when the motion group has been on for 7 seconds.

alias: Motion buiten verzamel
description: ''
trigger:
  - platform: state
    entity_id: group.motion_buiten
    to: 'on'
    for:
      seconds: 7
action:
- ...

I just discovered the trace-timeline! That is wonderful!
I noticed that the delay is working, so I increased the time, and now it works better.

With ‘for: seconds: 7’ I will miss some fast movements like a riding bike. If I then use an input_boolean the movement does trigger and play a sound, but just once. I hope. I will see if this is working better.
Thanks all!

So what you are trying to achieve is to ignore repeated triggers within a 7-second period?

In other words, when the automation is triggered, it should ignore subsequent triggers that may occur in the next 7 seconds?

Because if that’s what you want, it can be achieved with a Template Condition (no need for an input_boolean or a delay).


condition: "{{ (now() - this.attributes.last_triggered).total_seconds() > 7 }}"

Can I just use this code? Or should I change this.attributes. or something? like:

condition: "{{ (now() - group.motion_buiten.last_triggered).total_seconds() > 7 }}"

I’m relative new to home assistant. Coming from Domoticz

Tell me why you think it’s different from what I proposed.

Because this.attributes. sounds like a: sftp://your-username@ip-address construction to me. As I said I’m not very familiar with home assistant yet.
I will try this out.

From the documentation:

Variable this is the state object of the automation. State objects also contain context data which can be used to identify the user that caused a script or automation to execute.

In this case, this.attributes.last_triggered represents the last time the automation was triggered. The automation is triggered by the group entity so it represents the last time the group changed state and triggered the automation.