Issue: Triggering Automation for Flashing Office Lights Based on Motion

I’m working on my first automation in Home Assistant. The goal is to flash the lights in my office when the motion sensor outside detects motion. How the lights flash will differ based on their current state. I have already created and tested the necessary scripts (i.e., script.flash_office_light), and they work great.

Now, I need to trigger this automation based on two conditions:

  1. Motion detected by binary_sensor.meerkat_motion.
  2. The other light in the office (light.small_ikea_ceiling_light) must be in the ON state.

Additionally, I want the automation to reset after no motion is detected or 10 seconds, whichever happens first.

When I enter the following configuration into an automation and run it, nothing happens. I’m not seeing any error messages either.

How can I test this and troubleshoot the automation? Below is the automation I’m using:

Automation:

alias: Trigger Flash Office Lights on Motion
description: >-
  Triggers office light flash script when motion is detected and ceiling light is on
triggers:
  - entity_id: binary_sensor.meerkat_motion
    to: "on"
    trigger: state
conditions:
  - condition: state
    entity_id: light.small_ikea_ceiling_light
    state: "on"
actions:
  - action: script.flash_office_light
    data: {}
  - wait_for_trigger:
      - entity_id: binary_sensor.meerkat_motion
        to: "off"
        trigger: state
  - delay: "00:00:10"
mode: single

What I’ve tried so far:

  • I’ve confirmed that the script works manually.
  • The motion sensor is being triggered correctly, and the light state condition is set properly.
  • The reset (either no motion or after 10 seconds) should allow the automation to be triggered again.

Can anyone help me figure out why it’s not working, and suggest how to troubleshoot it?

What happens if you run the automation manually? If nothing happens, something is wrong with your actions. If it properly goes through the actions, there is something wrong with your trigger/condition.

How are you testing the trigger? A state trigger will only fire when the state changes. If the sensor is already on when the automation is (re)loaded, it will never fire until it toggles to off and then back to on.

That is not what your actions are programmed to do. They will always wait until the sensor turns off, then begin the additional delay. Replace the delay action with a timeout property on the wait_for_trigger. Better yet, also replace wait_for_trigger with a wait_template, as if the sensor switches to off in the split second that it takes for the automation to trigger and run its previous actions the state trigger will never fire for reasons already explained above.

Here is how I understand what you are saying:

alias: Trigger Flash Office Lights on Motion
triggers:
  - entity_id: binary_sensor.meerkat_motion
    to: "on"
    trigger: state
conditions:
  - condition: state
    entity_id: light.small_ikea_ceiling_light
    state: "on"
actions:
  - action: script.flash_office_light
  - wait_template: "{{ is_state('binary_sensor.meerkat_motion', 'off') }}"
    timeout: '00:00:10'  
    continue_on_timeout: true  
mode: single

How am I testing? I assumed that if I manually run the automation, it would automatically check that the conditions are correct and then proceed to execute the actions. To test, I went into the automation in the UI and used the “Run” option to trigger it.

That menu items is literally labelled “Run actions”, so that is all it does – it does not care about conditions or triggers. Conditions can be tested from the … menu next to each condition, but triggers can really only be tested by accomplishing the state for real or by using the Developer Tools > State to fake them.

Modifications to the actions look good, and I see nothing wrong in the trigger or condition – assuming that sensor and light are correctly reporting their states back to HASS everything looks like it should work.

Please post the script’s code.

If the script is designed to toggle the light indefinitely, you should call the script in a non-blocking manner.

Non-Blocking

This calls the script in a non-blocking manner. It starts the script and then immediately proceeds to execute the next action (which is the wait_template). In other words, execution is not blocked until the script finishes.

actions:
  - action: script.turn_on
    target:
      entity_id: script.flash_office_light
  - wait_template: "{{ is_state('binary_sensor.meerkat_motion', 'off') }}" 
    timeout: '00:00:10' 

Blocking

This calls the script in a blocking manner. It runs the script and waits for it to finish before proceeding to execute the next action. In other words, execution is blocked until the script finishes.

actions:
  - action: script.flash_office_light
  - wait_template: "{{ is_state('binary_sensor.meerkat_motion', 'off') }}" 
    timeout: '00:00:10' 

Reference

Scripts - Waiting for a script to complete

Thank you all, I got it working, the final issue I had was one of renaming scripts. Once I got that sussed out, it is working as desired!!!

Here is the full set of scripts, I want the light to flash differently based on whether or not it is on and how bright it is:

flash_when_off:
  alias: flash_when_off
  sequence:
  - variables:
      was_off: '{{ states(''light.floor_lamp'') == ''off'' }}'
  - repeat:
      count: 3
      sequence:
      - target:
          entity_id: light.floor_lamp
        data:
          brightness: 255
        action: light.turn_on
      - delay: '00:00:00.5'
      - target:
          entity_id: light.floor_lamp
        action: light.turn_off
        data: {}
      - delay: '00:00:00.5'
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ was_off }}'
      sequence:
      - target:
          entity_id: light.floor_lamp
        action: light.turn_off
        data: {}
  description: ''
flash_when_low:
  alias: flash_when_low
  sequence:
  - variables:
      original_brightness: '{{ state_attr(''light.floor_lamp'', ''brightness'') |
        int(0) }}'
  - repeat:
      count: 3
      sequence:
      - target:
          entity_id: light.floor_lamp
        data:
          brightness: 255
        action: light.turn_on
      - delay: '00:00:00.5'
      - target:
          entity_id: light.floor_lamp
        data:
          brightness: 64
        action: light.turn_on
      - delay: '00:00:00.5'
  - target:
      entity_id: light.floor_lamp
    data:
      brightness: '{{ original_brightness }}'
    action: light.turn_on
  description: ''
flash_when_high:
  alias: flash_when_high
  sequence:
  - variables:
      original_brightness: '{{ state_attr(''light.floor_lamp'', ''brightness'') |
        int(0) }}'
  - repeat:
      count: 3
      sequence:
      - target:
          entity_id: light.floor_lamp
        data:
          brightness: 64
        action: light.turn_on
      - delay: '00:00:00.5'
      - target:
          entity_id: light.floor_lamp
        data:
          brightness: 255
        action: light.turn_on
      - delay: '00:00:00.5'
  - target:
      entity_id: light.floor_lamp
    data:
      brightness: '{{ original_brightness }}'
    action: light.turn_on
  description: ''
main_flash_office_light:
  sequence:
  - variables:
      is_off: '{{ states(''light.floor_lamp'') == ''off'' }}'
      original_brightness: "{% if not is_off %}\n  {{ state_attr('light.floor_lamp',
        'brightness') | default(0) | int }}\n{% else %}\n  0\n{% endif %}\n"
  - choose:
    - conditions:
      - condition: template
        value_template: '{{ is_off }}'
      sequence:
      - data: {}
        action: script.flash_when_off
    - conditions:
      - condition: template
        value_template: '{{ original_brightness <= 128 }}'
      sequence:
      - data: {}
        action: script.flash_when_low
    - conditions:
      - condition: template
        value_template: '{{ original_brightness > 128 }}'
      sequence:
      - data: {}
        action: script.flash_when_high
  alias: main_flash_office_light
  description: Main Flash Office Lights Script
  mode: single

You can do that with a single script that employs a snapshot scene.

Before flashing the light, you make a snapshot scene of its current state, proceed to flash the light, then restore the snapshot scene (which restores the light to its original state/brightness).

Scenes - Creating scenes on the fly

Also, if your lighting technology supports the flash option, then the repeat count isn’t necessary.

Anyways, glad to hear it’s working now.