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:
Motion detected by binary_sensor.meerkat_motion.
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.
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.
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.
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.
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).