Simple automation doesn't work but these are my first steps on the forum

Hi all,
My first steps on the forum here.
I’ve been using Fibaro for more then 10 years with some pretty complicated code to get it all running.
Now in HA i can’t even turn off a switch after some motion detection. :laughing:
To me it’s all very different but there is so much to discover that it gets me exicting diving into HA.
So my first question after digging into the forum but no results.
Why doesn’t this work…

Turn on a switch after motion is detected.
When no motion is detected wait for 30 minutes and turn the switch off.

- id: '1679135670475'
  alias: Ventilator badkamer
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.motion_sensor_motion_detection
    to: 'on'
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.double_switch_2
  - if:
    - condition: state
      entity_id: binary_sensor.motion_sensor_motion_detection
      state: 'off'
      for:
        hours: 0
        minutes: 30
        seconds: 0
    then:
    - service: switch.turn_off
      data: {}
      target:
        entity_id: switch.double_switch_2
  mode: single

Conditions are checked immediately, they do not wait. So your “If/Then” will always fail, since the motion was just detected. You would need a Wait action for your “turn off” sequence, but it’s best to avoid long waits. Instead, set a second trigger for the motion being off for 30 min.

- id: '1679135670475'
  alias: Ventilator badkamer
  description: ''
  trigger:
    - platform: state
      id: 'on'
      entity_id:
        - binary_sensor.motion_sensor_motion_detection
      to: 'on'
      from: 'off'
    - platform: state
      id: 'off'
      entity_id:
        - binary_sensor.motion_sensor_motion_detection
      to: 'off'
      from: 'on'
      for: "00:30:00"
  condition: []
  action:
    - service: switch.turn_{{ trigger.id }}
      data: {}
      target:
        entity_id: switch.double_switch_2
  mode: single
1 Like

Your issue is TIMING. In your automation you have it setup to immediately evaluate whether the light has been on for 30 minutes as soon as the light turns on. This will fail everytime. There are a bunch of ways to fix this. The easiest is to add a delay for 30 minutes then have it evaluate the if statement. Doing this his will also cause you problems if you do something like turn the light off and on during that 30 minute period.

To fix that issue you would change your automation type from single to multiple or just use the STATE trigger in a separate automation or the state trigger with trigger IDs.

Perfect !!
Thank you so much guys !
I’m used to program in Lua but Yaml is a different approach.
Al lot to discover and learn but hey…that’s the fun part isn’t it ? :smiley:

I did a reply recently to someone with similar need and posted 3 examples of motion controlled lights

Note how I use wait_template and how I define the automation so that they have mode “restart” so any motion in wait period cancel the actions and restart the automation. The examples take advantage of multiple motion sensors

I will dive into this !
Thank you so much.