Have a light/switch on for an adhoc duration?

Hi!

New to home assistant, coming from Domoticz.

What the easiest way to have a light or switch turn on for an adhoc length of time.

In domoticz it was easy as “turn on for 2 minutes” etc.

I’ve created scripts, blueprints like:

blueprint:
name: Turn on light for set period of time.
description: Given a light entity, turn on and then turn it off after a certain period of time.
domain: script
input:
light:
name: Light
description: Select the light to monitor and control
selector:
entity:
domain: light
time:
name: Duration
description: The duration to leave the light on for
selector:
duration:

mode: restart

sequence:

  • service: light.turn_on
    data: {}
    entity_id: !input light
  • delay: !input time
  • service: light.turn_off
    data: {}
    entity_id: !input light

BUT that isn’t exactly adhoc :frowning:

And adding a delay between the on and off in a script or automation make the whole thing wait.

Tried using the run in parallel thing but can’t seem to sus the following:

do action 1,2,3 etc and at the same time

turn on light
delay 2 mins
turn off light

I just would like a simple way of doing this in a script or automation without having to create other scripts/automations/blueprints first.

What I would like is a box in the script/automations and parameter in yaml code to set a duration.

Hope this makes sense :slight_smile:

Keeping it simple is key (many separate automations that can be easily changed instead of one complicated thing that turns into a mess of spaghetti logic). The paradigm I use is to (and you can see how it gets more complicated as you refine it as per below with additional fine tuning if you wish to proceed to the end of the list). All the items below are separate automations:

1a. Helper: Set up a timer, 1b. set up a numeric input helper (for timer duration), have it show as a slider on your dashboard (so you can easily change the duration of the timer when it is running).
2. Automation: When the timer is (re)/started, it turns the light on.
3. Automation: When the timer is finished, it turns off the light.
4. Automation: When someone turns on the light, the timer in #1a is (re)started, with the duration from #1b.
5. Automation: When Motion Sensor detects motion (re)start the timer (#1a) with the duration from #1b (that way the lights stay on while people are in the room and the countdown only really starts when there is no more motion). Also, the less expensive motion sensors are not able to tell very well when there is nobody in the room, or there are people in the room but not moving, hence the timer/s.
6. Helper: A drop-down with the text “Enabled” / “Disabled” that I then go-back and put into the above Automation logic (#4 and #5) that the automation is enabled or disabled.
7. Helper: I ran into a problem where people would turn off a light and then when they then walk out of the room the light would go back on because the motion sensor catches the movement! Therefore - an input text datetime helper just for storing the datetime (then see #8-9 that are the final parts to resolve this issue.)
8. Automation: When the switch is only MANUALLY turned off (there are trigger variables that can be used to determine this - but you have to store information about the current datetime into #7 because after this automation is finished, that data is otherwise lost), set the current date and time into #7 -
9. Update Automation: Update #5 to contain logic that will always check #7, to actually skip calling any automation when motion is sensed if the value in #7 is within the last 5 minutes.
10. Automation: Whenever the slider in #1b is changed, only if the related timer (1a) is active, then (re)start that timer with the duration listed in #1b.
11. Automation: When HA is restarted, check the above that if the automation is enabled and the light is on, that the timer is (re)started if it is not active.
12. Automation: When HA is restarted, check the above that if the automation is enabled and the timer is active but the light is not on, that either the timer is cancelled or the light is turned on.
13. Automation: Update the automations that turn on the light, that sense the amount of the light outside from a weather station or the like, and if the room has alot of windows, (and the weather station does not see total darkenss in the middle of the day because it might be covered with snow for example) you can set the light to be dimmer, or if it is very late at night, you can have the lights come on much dimmer, etc.
14. If there is only one person in the home, you can just cancel the timers for the other rooms when motion is sensed in any one room because there is nobody in the other rooms (and that would turn those lights off, etc.)
15. Automation: If the dropdown for enabled/disabled is changed to disabled, then if you are controlling a smart outlet into which a lamp is plugged, then stop the timer and turn the outlet ON so people will not be annoyed that they cannot turn the lamp on - and a different paradigm should be used if it is for a wall switch, that if the dropdown for enabled/disabled is changed to disabled, to just stop the timer and do nothing else - leave the switch as it is (then change the logic in #3 to only turn the light off if the automation is Enabled).
16. Automation: If an automation is then changed to Enabled, if the related light is on then just start the related timer (1a) with the duration (1b).

The above 16 are somewhat detailed but once you have it working it is pretty bulletproof - and you don’t have to do all 16 but you get the idea. Then, you no longer have to reinvent the wheel as you add more lights into your automations, just duplicate trhe automations above and helpers for each new light you add into the mix. Also, when you do this, look at all the new automations (that were duplicated) under the list of entities, and you will see the entity name does not change properly, so right in that screen you can change the names of those entities so they are consistent.

And doing your automations this way (in many simple pieces) makes maintenance etc. MUCH simpler.

Then for the code that is reapeated everywhere, move it into a script to which you can pass the objects (timer, light, enabled/disabled helper, duration helper etc.) and all that logic can be in one place. Therefore also set all automations to be able to run in parallel (check the mode I make mine all parallel and put a limit of 1,000 or the like).

The above has worked beautifully for me - (then I set up alerts for sensors when their batteries are low, as well as one UI panel the just shows all the battery levels for all the sensors at a glance, etc.)

Thoughts?

Very long story for a basic automation :thinking:

  1. what should trigger the light
  2. which light should turn on
  3. delay for 2 mins
  4. turn the light off again…
description: ""
mode: single
trigger:
  - platform: sun
    event: sunset
    offset: 0
condition: []
action:
  - services: light.turn_on
    data: {}
    target:
      entity_id: light.light_hallway_downstairs
    alias: turn on
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - services: light.turn_off
    data: {}
    target:
      entity_id: light.light_hallway_downstairs
    alias: turn off

ofc the trigger could be the light itself turning on…
that would simplify it al lot:

description: ""
mode: single
trigger:
  - platform: state
    entity_id:
      - light.light_hallway_downstairs
    from: "off"
    to: “on”
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: {{ trigger.entity_id }}

Instead of using {{ trigger.entity_id }}, you can also use the real entity light.light_hallway_downstairs, but i figured to throw it in as example :grin:

Thanks guys. Very in depth :joy: Think I’ll keep it simple with a delay then off command :+1: