Hi. How do you code it when you only want the light to turn on for a certain amount of time then turn off automatically?
For example, I want to do the following automation: If the front gate door (binary_sensor.front_gate_door) opens and it is night, then open flood light (light.back_yard_flood) only for 2 minutes.
FWIW, @lolouk44’s solution will probably suit your needs just fine. However, there is one issue with this - if the front gate door is closed and opened again before the two minutes is up, the light will probably immediately go off. This is because triggering an automation while it’s actions are still running from a previous trigger event usually doesn’t work very well.
A more robust solution would be to turn the light on and start a two minute timer. If the front gate door is closed and opened again before the timer expires, the timer will just be restarted. Then, when the timer finally expires, trigger another automation that turns the light off. Like I said, the simpler solution will probably be fine. But if you’d rather account for the scenario I described, and you need help writing a corresponding implementation, just let me know.
Thanks @pnbruckner! I am so new to this I need all the help I can get. I would love to see then how you would write the “code” with the two minute timer. Appreciate it!
Thanks for your input, though I don’t see how opening and closing the door quickly will turn the light off immediately?
There doesn’t appear to be an automation (or a request for one) to turn the light off when the door is closed. It’s purely “turn on the light FOR 2 min WHEN I open the door IF it’s night”
Or have I missed something?
So, this is based on observation, not documentation. I’ve found (through helping other people and my own experience) that if you trigger an automation with a wait or delay in the actions, while it’s still running from a previous trigger event, that the wait/delay effectively gets skipped. (I don’t know why, and I haven’t taken the time to try and figure it out. It’s just what I’ve observed.) So in this case, the light would be turned on (which it already is), then the delay gets skipped, and then the last step will run which turns the light off. Hence, the light goes off immediately.
In general I try to avoid automation action sequences that include waits or delays. Either I put them in a script which I cancel (in case it’s already running), then start, from the automation action, or I do something like I described above.
I don’t know for sure that your solution would “fail” in this way in the described scenario, I just think it probably will.
Thanks for the insights… I have only a small number of these so will run some tests and see if I can reproduce (though they’re usually short like 1 sec to allow for a light transition to take place)
FWIW, I think I found why this happens - i.e., if an automation’s actions are running, and “sitting” in a delay or wait_template, and the automation gets triggered again, the delay/wait effectively gets skipped past.
If the script is “turned on” again while it’s already “on” (i.e., run again while it’s already running), and it’s in a wait for a delay or wait_template, the delay/wait simply gets canceled, and the script picks up with the next step!
I know this is an old thread by maybe somebody can help me. I am trying to do the same thing as OP but can’t seem to get it to work, the light will turn on but I can’t get it to turn off after 2 minutes. I have tried:
Unfortunately there are a LOT of topics describing this same issue and solutions, but this one doesn’t seem to have a solution posted.
The basic idea is to move the actions into a script, and then in the automation stop the script (by turning it off) in case it is running, then start the script. So the basic outline is:
I don’t know when the mode was added, I didn’t know you could use it in a script. Per:
I think you want restart rather than single so that it just kills the original one rather than not running a new instance - so for example if motion was detected again within 3 minutes the light will stay on another 3 minutes.
I just set up something like this and used restart but haven’t fully tested it.
Then I will add my two cents
I’ve created a helper switch to determine if the lights were turned on by motion.
In combination with hue lights this is working good for my situation.
When motion detected and it is between certain times → check the trigger ID and if the lights are off. Then turn lights on, and set helper switch to on.
If helper switch is on for 10 minutes, turn lights off.
I did not had the situation yet that there is a need to override the helper switch yet.
alias: Beweging beneden
description: ""
trigger:
- platform: state
entity_id:
- binary_sensor.motion_woonkamer_motion
to: "on"
id: Beweging_Gedetecteerd
- platform: state
entity_id:
- input_boolean.ingeschakeld_door_beweging_woonkamer
from: "off"
to: "on"
for:
hours: 0
minutes: 10
seconds: 0
id: 7 minuten na motion
condition:
- condition: or
conditions:
- condition: state
entity_id: person.x
state: home
- condition: state
entity_id: person.y
state: home
- condition: or
conditions:
- condition: time
after: "00:00:00"
before: "08:00:00"
- condition: time
after: "23:00:00"
before: "23:59:58"
- condition: state
entity_id: sun.sun
state: below_horizon
action:
- if:
- condition: trigger
id:
- Beweging_Gedetecteerd
- condition: state
entity_id: light.beneden
state: "off"
then:
- parallel:
- service: scene.turn_on
data:
transition: 20
target:
entity_id: scene.beneden_dagelijks
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
- if:
- condition: trigger
id:
- 7 minuten na motion
then:
- parallel:
- service: light.turn_off
data:
transition: 30
target:
entity_id: light.beneden
- service: input_boolean.turn_off
data: {}
target:
entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
mode: single
Thanks to the details analysis of @pnbruckner I understand why my script was not doing what I wanted it to do.
But in Homeassistant 2024.1.5 you now need to add an additional target element, to define which script to be stopped: