Hi All,
I have been tinkering with this for allmost 3 weeks now and i think it is time to ask for help…
I have been trying to turn on lights but only when it is after sunset and before sunrise.
Below is my automation.
alias: Verlichting woonkamer inschakelen
trigger:
- entity_id: binary_sensor.motion_sensor_158d00015a7998
platform: state
to: 'on'
condition:
- condition: state
entity_id: light.sfeerlamp_woonkamer
state: 'off'
- condition: state
entity_id: light.sfeerlamp_woonkamer_2
state: 'off'
- condition: sun
before: sunrise
- condition: sun
after: sunset
action:
- data:
entity_id: light.sfeerlamp_woonkamer_2
service: light.turn_on
- delay: 00:00:01
- data:
entity_id: light.sfeerlamp_woonkamer
service: light.turn_on
- data:
message: HA test message
title: Motion detected by Xiaomi motion sensor.
service: notify.home_assistant
I also tried doing this via the UI.
alias: Sunset test
trigger:
- at: '23:00'
platform: time
condition:
- condition: sun
after: sunset
- condition: sun
before: sunrise
action:
- data:
message: 'Het is nu 23:00, de zon is onder en de mail wordt goed verzonden. '
title: Sunset test script.
service: notify.home_assistant
When testing this automation the action runs as expected. When removing the sunset/sunrise stuff the automation runs but in some way the automation does not run when adding the sunset and sunrise stuff…
Could somebody point out to me what i am doing wrong?
Thank you all! Will try the solution Calle and Marius offered. (below horizon state of sun.sun) If i am correct i cannot use any offset there, so i might need to go to the numeric_state function Calle suggested of the TOD sensor Patrick suggested.
Hope it works tonight
I think it would have worked using your original conditions but only if you understand how the sunset/sunrise events are processed.
“after sunset” is any time between sunset and the following midnight. So if sunset is at 6 pm the “after: sunset” condition is true for any time between 6pm and midnight.
Likewise, “before sunrise” is any time between the previous midnight and sunrise.
By default all conditions are ‘and’. so in your original automation you are requiring that the time be both between sunset and midnight, and also between midnight and sunrise simultaneously. I think you should now see why it never worked.
to get it to work correctly you needed to make those two conditions as ‘or’. So then it would have worked if the time was either between sunset and midnight or between midnight and sunrise. Effectively making the condition being between sunset and the next sunrise which is what you ultimately wanted.
Yes, but I took the opportunity for a teaching moment.
More information is never a bad thing.
what if the check wasn’t for the state of the sun? what if the check was for a certain time - like say between 11pm & 5am? then the same limitations hold true and you need the two checks because you can’t do that with just one.
And it gave the OP the opportunity to learn how to use the ‘or’ conditions as well.
Thank you all! The lights are turning on correct but a little late.
As there is no offset to the sun.sun entity i am now testing the OR condition. (also for learning purposes. )
Will see tonight if my automation runs as expected.
alias: Sunset test
trigger:
- at: '23:00'
platform: time
condition:
condition: and
conditions:
- condition: state
entity_id: light.sfeerlamp_woonkamer
state: 'off'
- condition: state
entity_id: light.sfeerlamp_woonkamer_2
state: 'off'
- condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
action:
- data:
message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
title: Sunset test script.
service: notify.home_assistant
not wanting to be a pita, but, when you trigger an automation in our hemisphere, the sun is always under at 23:00… so these conditions won’t be of any added value.
for styling purpose: you could (I daren’t say should) take out 2 spaces before the individual condition dashes, like:
- condition: or
conditions:
- condition: sun
before: sunrise
- condition: sun
after: sunset
and add 2 spaces in the trigger and action blocks, like:
action:
- data:
message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
title: Sunset test script.
service: notify.home_assistant
i’d even suggest using:
action:
- service: notify.home_assistant
data:
message: 'Het is nu 23:00, de zon is onder, de sfeerlampen in de woonkamer staan uit en de mail wordt goed verzonden. '
title: Sunset test script.
to have it be more readable.
that way your complete automation has the same indentation.
I am using this trigger to test if the condition is working correctly for test purposes only.
Right now, i know i have to recieve the e-mail at 23:00. When working like i want it to i can use the condition part in other automations where there is a different trigger.
Because I live in a heavily wooded area, sunrise and sunset are not practical triggers for automation. With ESPhome, a Wemos D1 Mini, and BH1750 ambient light sensor, I read daylight conditions, and trigger outdoor lights on when sensor Lux reading is < 40.
- id: '0010'
alias: Outdoor lights ON if dark
trigger:
- below: '40.0'
entity_id: sensor.bh1750_illuminance
for:
minutes: 2
platform: numeric_state
action:
- entity_id: switch.Front_Porch, switch.Garage_Lights
service: homeassistant.turn_on
initial_state: true
cool, thought as much but you should have a second automation at a time when the sun is still up, to be able to determine if the condition works for you.
I’m not at home, or I’d embed my ESPHome configuration yaml file for the device. The other nice thing is it’s very small, and can be easily placed in a window. It’s also automagically discovered/configured by HA since it’s an ESPHome device.