Simple Right? Struggling with a simple motion on after sunset

I am trying so hard. Spinning my wheels on this simple automation. I’m trying to avoid the blueprint version in an effort to better understand YAML. It works just fine for me without the wait for trigger. I cannot get it to wait and then shut off. I know I’m missing something here an hoping that an answer will give me the kick start I need to getting to all the HomeAssistant goodness. Sincere thanks in advance.

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
  - platform: state
    entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: on
    to: off
  - delay: 10
    # hours: 0
    # minutes: 0
    # seconds: 10
  - service: light.turn_off
    target:
      entity_id: light.end_table
  mode: single

wait_for_trigger's State Trigger needs to be indented.

Example

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: 'on'
      to: 'off'
  - delay: 10
  - service: light.turn_off
    target:
      entity_id: light.end_table
  mode: single
1 Like

Still does not work

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
    - platform: state
    entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: on
    to: off
  - delay: 10
    # hours: 0
    # minutes: 0
    # seconds: 10
  - service: light.turn_off
    target:
      entity_id: light.end_table
  mode: single
#   max_exceeded: silent
#   mode: restart

You didn’t indent it properly.

Look at the example given:

  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: 'on'
      to: 'off'

now look at yours:

  - wait_for_trigger:
    - platform: state
    entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: on
    to: off

Which part?

The part you didn’t copy correctly from my example?

What I have now. Still does not work.

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: on
      to: off
    - delay: 10
    # hours: 0
    # minutes: 0
    # seconds: 10
    - service: light.turn_off
    target:
      entity_id: light.end_table
  mode: single
#   max_exceeded: silent
#   mode: restart

your indentation is still off:

yours:

action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: on
      to: off
    - delay: 10
    # hours: 0
    # minutes: 0
    # seconds: 10
    - service: light.turn_off
    target:
      entity_id: light.end_table

what it should be:

action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: on
      to: off
  - delay: 10
  - service: light.turn_off
    target:
      entity_id: light.end_table

Still not indented correctly.

I respectfully suggest you consider using the Automation Editor, in Visual mode, because manually composing YAML isn’t working out for you.

1 Like

This now gets the light turned on but it never turns off.

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.end_table
#mode: single
  ###############################
  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: 'on'
      to: 'off'
  - delay: 10
  - service: light.turn_off
    target:
      entity_id: light.end_table
  mode: single

are you sure the follwing is actually happening?

  - wait_for_trigger:
    - platform: state
      entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
      from: 'on'
      to: 'off'

How would I check that. Please bear with me…

The logbook shows the motion event fires, the lamp turns on.

Look at the entity in states and confirm it turns off

You can also (and should also) be checking the automation’s trace

Ok, I think it is working. If I change the state of the motion detector from off to on, the lamp lights. If I then change the motion detector state from on to off, the lamp goes out. I am doing this remotely(not in the house). How do I mimick the way the motion detector would see motion and then not see motion. I mean mimick the way it would work were I in the house? Or am I doing that? Thank You!

That is what you’re doing. All you need to do now is confirm that the motion sensor itself changes state correctly.

How would I test the motion sensor itself remotely? I mean after changing its state from off to on, how could I watch it’s state change back to off to be sure it is doing what it should be doing? Hope that makes sense.

Well, you need somebody to walk past the sensor.

If you manually change HA’s perception of the state the sensor isn’t going to know you’ve done that. Nothing really changed, so it’s not going to tell HA that it’s stopped detecting motion…

If you’re interested, your automation can be reduced to this:

- id: '1676993059605'
  alias: TEST ON WITH MOTION
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.z_wave_motion_sensor_motion_detection_2
    from: 'on'
    to: 'off'
    for:
      seconds: 10
  condition:
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 90
  action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.end_table
  mode: single
  • The light is turned on whenever motion is detected (and the condition is fulfilled).

  • The light is turned off whenever no motion is detected for at least 10 seconds (and the condition is fulfilled).

1 Like

Thank You so very much! I am so loving this software. HA is amazing and this community is great!

2 Likes