may I ask for your help? I have a really simple automation that works with two triggers which turns on my staircase light when motion is detected (trigger 1) and turns off when motion is no longer detected (trigger 2). Does anyone have an idea why this automation stops working when I add an additional condition “after_sunset” and “before_sunrise”? In this case the light is switched on briefly (less than 1 second) and switched off again immediately. See both YAML codes below.
Conditions are ‘and’ by default so you can simply list the conditions and don’t need the ‘and’.
Also if it’s after sunset then it can not also therefore be before sunset on any given one day. It does not span across midnight to the following day. So it’s either after sunset OR before sunrise.
- condition: or
conditions:
- condition: sun
after: sunset
- condition: sun
before: sunrise
- condition: trigger
id: motion-detected
You could use an ‘or’ for the two Sun conditions or as @123 suggested in template form you could use:
- condition: state
entity_id: sun.sun
state: below_horizon
Thank you very much @123. Your solution works very well, but unlike @rossk’s solution, I don’t quite understand it. Unfortunately switching to the visual editor doesn’t help me either, because it can’t display the following lines:
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
Can you give me a short explanation what these lines do?
We only want the State Trigger to listen for state-changes from on to off and off to on. We don’t want it to trigger for other state-changes such as unavailable to off. Therefore we specify a list of desired states in the from and to options.
The Automation Editor has several known limitations (and bugs) and you have encountered one of them. Home Assistant’s scripting language, represented in YAML, is capable of more things than the Automation Editor is able to display in Visual mode.
Ok, the list defines the states to watch for, I understand that, but where is it defined that the light is turned off again when there is no more movement.
When the binary_sensor’s state changes to off the value of trigger.to_state.state becomes off. The Immediate If statement in the following template determines the value of the brightness_pct option.
I’ve read it a few times now but still don’t get it. I will try a little harder tomorrow, now my head is smoking. Anyway many thanks @123 for your support
@123: Can I ask for your help again? How to change the code so that the light is not turned off immediately when no more movement is detected, but remains on for 2 minutes?
alias: Light staircase on movement
description: ""
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_staircase
from:
- 'on'
- 'off'
to:
- 'off'
- 'on'
condition: "{{ is_state('sun.sun', 'below_horizon') }}"
action:
- service: light.turn_on
target:
entity_id: light.staircase
data:
brightness_pct: "{{ iif(trigger.to_state.state == 'on', 40, 0) }}"
mode: single