Wondering if anyone can help me with the following. I’m trying to run the following automation with a condition of it only happening between 22:30 and sunrise the next day. I’m unclear as to HA’s internal logic with time-stamps, so I’m guessing this is failing as sunrise is taken as the current day? Can anyone confirm?
- id: '1526002858051'
alias: if someone goes away between 22:30 and sunrise, turn outside lights on
trigger:
- entity_id: sensor.user1_status
platform: state
to: Away
- entity_id: sensor.user2_status
platform: state
to: Away
condition:
condition: and
conditions:
- condition: time
after: '22:30'
- condition: sun
before: sunrise
action:
- data:
entity_id: group.outside_lights
service: homeassistant.turn_on
My guess is that once the clock strikes midnight it’s no longer after 22:30 and thus the condition isn’t met. Changing to OR should resolve that, but could also leave you open to meeting the condition before 22:30 as well if the sunset occurs before then. Based on the table in the docs, I believe once the sunset occurs it automatically becomes before sunrise regardless of the day. If you end up with unexpected results that could be why.
An alternative could be using a funky template workaround like this:
condition:
condition: and
conditions:
- condition: time
after: '22:30'
- condition: template
value_template: '{{ as_timestamp(now()) | int < as_timestamp(states.sun.sun.attributes.next_rising) | int }}'
Note that I haven’t tested that, but do something very similar. Happy to help if it were to throw an error.
I just create a binary sensor called night that I use for such conditions. Works well and nice to have it available in one place to use in various automations as allows you to change it as well.
For example if the house is empty it reverts to sunset to sunrise; if we’re home some automations delay to between 11:30cm and sunrise.
Could you expand on what makes it the best route? Always looking to do things the best/most efficient way.
Unless my interpretation of the docs inaccurate (please correct me if so!), seems like in reality using an OR would have the same effect as not including the 22:30 condition at all? It’s all based on location of course, but for me at least, currently the sun sets around 21:15, which means from that point on the condition of before sunrise would be true, correct?
No. “before sunrise” is from midnight until sunrise. You need to think of the time periods during a single day, from midnight (this morning) to midnight (tonight.) Starting at midnight, it’s before sunrise, and it’s also before sunset. Then at sunrise it becomes after sunrise, and it’s still before sunset. Then at sunset it’s still after sunrise, and it becomes after sunset. Then at midnight the cycle starts again.
So, saying it’s before sunrise or after 22:30, is saying it’s either between midnight (this morning) and sunrise, or between 22:30 and midnight (tonight.)