A condition of sun being (almost) below the horizon and brightness added into the action - you can also set colour and so on if you look up the light options for your platform.
automation:
alias: Turn on kitchen lights when there is movement
trigger:
- platform: state
entity_id: sensor.motion_sensor
to: 'on'
condition:
condition: numeric_state
entity_id: sun.sun
value_template: '{{ state.attributes.elevation }}'
below: 3.5
action:
service: homeassistant.turn_on
entity_id: script.timed_lamp
script:
timed_lamp:
alias: "Turn on lamp and set timer"
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.timer_off
- service: light.turn_on
data:
entity_id: light.kitchen
brightness: 50
# set a colour if you want
# rgb_color: [255,255,255]
# Set new timer
- service: script.turn_on
data:
entity_id: script.timer_off
timer_off:
alias: "Turn off lamp after 10 minutes"
sequence:
- delay:
minutes: 10
- service: light.turn_off
data:
entity_id: light.kitchen
Thank you! I was thinking that was the answer, but I got caught up with the script in which I added an extra data set and was just about to save and refresh without it then I looked at your comment. I guess I was going in the correct direction then, thank you for confirming!
I have one last question, albeit unsure if you can answer. Now that when the script runs, whenever I turn the light back on, it doesn’t reset to 100% brightness. Would I have to use booleans to make it turn back 100% brightness when the script isn’t run? I’m kinda at a lost.
Sorry - I’m in the middle of watering plants, I should have that automated.
I’m not quite understanding what you are asking.
You have a script that automatically turns the lights on when movement is detected at 50%.
You want to change the default value on an on state to %100 unless otherwise specified?
If so, I think you can set a default in your light platform config but have never used it.
Perhaps you could make it a part of the exit script at the end of the ten mins - you would have a flash of brightness though.
When the motion detector is triggered, it turns my hallway light to a brightness of 20% and then turns off after a minute. However when I turn the light on using HA, the light is still at a brightness of 20% even though the light wasn’t turned on using the script.
Basically what I’m trying to say is I only want the light to be at 20% brightness when that script is ran, after the script is ran and the light is back off if I turn it on using Home Assistant I want it at 100% brightness.
When you instruct home assistant to turn the lights on the rest of the time can you just add brightness to the instruction?
Otherwise see the above modification to the timer_off: bit - just before it turns off it sets brightness to 100 so that will be the last known state of light - give it a try maybe.
I used what you recommended and it works! However one issue is I believe the script is running too fast for HA to catch up and when the light turns off using the script it’s actually off but HA isn’t showing that it’s off.
Edit: Delayed to make the light stay at 100% brightness for 5 seconds and that fixed the issue.
I wonder if there’s a better way to do this though?
Yep. Set the brightness when you turn it on the rest of the time. There are some more complicated methods but you might have to wait until the other side of the world wakes up for help, that stuff is way beyond my understanding.
I don’t know that brightness is a percentage by the way, 0 to 255 are the options.
Set that to be a transition to off of 5 seconds perhaps for something less jarring?
Transition: 5 means it will start at 100% and after 5 seconds will have transitioned to off or 0% brightness. It will be dimming the light across the 5 seconds but doesn’t set the brightness to 0% afterwards.
I think you want to make the brightness level a condition in the automation somehow to differentiate it from normal usage. If you think about the normal usage and this motion detect usage you might see a pattern? I’ll have a think about it because this is something I could use for my midnight wee lighting. Perhaps with booleans or scenes or even both.
Here is what I ended up using, as it’s a bit annoying that it has to go to 100% brightness before it shuts off, it’s working perfectly:
timed_hallway:
alias: "Turn on hallway light and set timer"
sequence:
# Cancel ev. old timers
- service: script.turn_off
data:
entity_id: script.timer_off
- service: light.turn_on
data:
entity_id: light.upstairs_hallway
brightness: 20
# Set new timer
- service: script.turn_on
data:
entity_id: script.timer_off
timer_off:
alias: "Turn off lamp after 1 minutes"
sequence:
- delay:
seconds: 30
- service: light.turn_on
data:
entity_id: light.upstairs_hallway
brightness: 255
- delay:
seconds: 1
- service: light.turn_off
data:
entity_id: light.upstairs_hallway
If I knew more about python, I could probably get it figured out, but I’m just not sure. I’m thinking you could use booleans, but I’m not really sure how to implement them properly.