Motion-activated light between dusk/dawn (with offsets) only if ambient LUX is low. Includes adjustable timer

Please comment. Thank you

intelligent_staircase_automation.yaml

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

1 Like

Is this a blueprint you are ready to support and share, or is it a work in progress.
IE if you are debugging something, we should change the category.

If you want to share it, you might like to add a my link so others can easily load it.

This is fantastic, but I think you also need a motion or occupancy sensor, to avoid waste energy if no one on the room

1 Like

I use it in a production environment, so it is maintained.

Can you assist me to ad a mylink, please?

2 Likes

I added a screenshot.

@Beerlesklopfer Hello, thank you for the blueprint, it looked to be exactly what I was looking for, a simple motion controlled light automation without all the complexity of some of the others I have seen.

Have been trying it, but hit a problem and what looks to be some coding issues.

First the problem. My lights are controlled by Shelly smart relays so are switch.xxxxx not light.xxxx control entities. Consequently as the blueprint looks for entity names in the light domain I can’t pick the right control entity.
However I was able to edit the automation config to use the right switch entity name, so if this works, then this will be a workaround. But would be better if the picker can choose from both switch and light domain:

alias: Upstairs Hall Light 2 Motion
description: ""
use_blueprint:
  path: Beerlesklopfer/intelligent_staircase_automation.yaml
  input:
    motion_sensor: binary_sensor.hall_upstairs_2_epl_occupancy
    light_entity: switch.upstairs_hall_light_2
    lux_sensor: sensor.hall_upstairs_2_epl_illuminance
    dusk_offset: -30
    dawn_offset: 30
    light_duration: 10

Edit: running the automation manually, my use of a switch entity didn’t work as the automation calls light.turn_on / light.turn_off so unless it can be made more generic, I’m out of luck with this.

Edit 2: turns out there is a way round this, create a helper of type ‘change device type of a switch’ to make it a light domain entity, and job job, when I put the new light entity into the automation it works :+1:

Now the bug. When the automation is triggered by the motion sensor and I look at the automation trace I can see that the automation fails with what appear to be data type conversion errors:

now() returns a date/time object eg 2025-08-23 22:05:43.496542+01:00

and

as_timestamp(state_attr(‘sun.sun’, ‘next_dusk’)) returns a Unix epoch time (time in seconds since 1/1/1970) - a float value

so these are not directly comparable

The template should be using

as_timestamp(now())

@Beerlesklopfer further problems with using your blueprint …

I took control of my automation created from your blueprint so I could correct the condition templates from using now() to as_timestamp(now()); which stopped the automation crashing.

Waited until after dark then triggered the motion sensor, but the light doesn’t come on.

Looking more closely at your condition code, it looks to be flawed and as written will never work.

Specifically, the condition code checks:

          {{ as_timestamp(now()) >= (as_timestamp(state_attr('sun.sun',
          'next_dusk')) + (dusk_offset * 60)) }}

with the intent that its checking that its after the sun has done down

and also

          {{ as_timestamp(now()) <= (as_timestamp(state_attr('sun.sun',
          'next_dawn')) - (dawn_offset * 60)) }}

and that the sun hasn’t risen yet.

BUT as soon as the sun has gone down, state_attr(‘sun.sun’, ‘next_dusk’) will return the date/time OF THE NEXT DUSK, i.e. tomorrow’s dusk NOT today’s

So right now, at 21:36 on 25/8/25, sun.sun attributes are:
next_dawn: 2025-08-26T04:24:29.653954+00:00
next_dusk: 2025-08-26T19:38:59.713211+00:00

So the next_dusk test will always fail and the light not come on after dark.

I suspect there may be similar issues with the next_dawn and the code during the daytime.

Am wondering how you tested this blueprint?

Looking at other blueprints to see how they handle this, the couple I looked at do not use next_dawn and next_dusk, instead they use the sun elevation

e.g. :bulb: Sensor Light - Motion Sensor - Blueprints Exchange and read this FAQ Why did you choose to use sun elevation?

For now I will disable the template conditions for dawn and dusk and rely on only the lux sensor. The blueprint needs rewriting to handle operation after dark, sun elevation might be the easier route to go down rather than complex date/time manipulation based on the sun position and today/tomorrow dusk etc