Sun automation condition not working correctly

I have the following conditions to switch on a smart light on one of my automations:


The problem is, as soon as it hits midnight, it no longer works (triggers the light on), any idea why that is? Should it not continue to turn on my light until sunrise happens?

This is the AE rather than raw code so I’m guessing but you say after sunset.
00:00 marks the start of a new day so it’s no longer after sunset

Here is the code:

alias: Lights - Turn on kitchen lights when there is movement
  trigger:
  - entity_id: binary_sensor.motion_sensor_motion
    platform: state
    to: 'on'
  condition:
  - after: sunset
    after_offset: '-30'
    condition: sun
  action:
  - entity_id: light.bottom
    service: homeassistant.turn_on

what do you think? Add another condition underneath that says before sunrise to make it work after midnight?

This condition allows 30 mins before sunset to midnight
(why not use the before_offset: ?)
So yes add another condition for before sunrise:
But these conditions will need to be 'or’ed rather than the standard ‘and’
Consult the docs for that

30 Before sunset doesn’t work (the lights don’t come on) makes no sense should be the same as -30 after?
Also tried the OR and again same thing, nothing turns on on motion:


Logically it makes sense to me but mustn’t be right

I dont play with AE so my opinion means nothing
They are still finessing the AE and it will support more features soon, until then ‘hand crafted’ is your only option.
Your automation should look like this : -

#### or condition
alias: Lights - Turn on kitchen lights when there is movement
  trigger:
  - entity_id: binary_sensor.motion_sensor_motion
    platform: state
    to: 'on'
condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
      before_offset: '30'
    - condition: sun
      before: sunrise
      after_offset: '30'
  action:
  - entity_id: light.bottom
    service: homeassistant.turn_on

You will probably have to cut and paste this over your existing auotmation, DO NOT use the AE for this.
But by all means look at it in the AE afterwards just to see what it makes of it

Thanks for that, I think I might have another solution if it works, based on the state:

- condition: state
    entity_id: sun.sun
    state: below_horizon

It is not as configurable since I can’t add the offsets but it might do the trick.
Will try your suggestion as well since I think if I wait for the below_horizon state, it might already be dark when that happens

Yeah, you can fine tune that with : -

  - condition: template
    value_template: "{{ state_attr('sun.sun', 'elevation') | float < -4.2 }}"

This says ONLY if sun is 4.2° below the horizon, tweak if you are unhappy with the light level that gives (the lower angle. the less times the bulb switches on and saves both bulb life and power cost, double win) Angle is a much better offset than time as the light is more consistent.

Ideally I shouldn’t need the sun at all but the lux value of the aqara sensor is not enough as it is as low as full darkness even during the day (which sort of makes it useless for the main reason I have bought it)

Close, but it should be:

#### or condition
alias: Lights - Turn on kitchen lights when there is movement
  trigger:
  - entity_id: binary_sensor.motion_sensor_motion
    platform: state
    to: 'on'
condition:
  condition: or
  conditions:
    - condition: sun
      after: sunset
      after_offset: '-30'
    - condition: sun
      before: sunrise
      before_offset: '30'
  action:
  - entity_id: light.bottom
    service: homeassistant.turn_on

Many have been confused by this. after_offset is the offset that goes with the after option, and before_offset is the offset that goes with the before option. Positive offsets mean after the after/before event, and negative offsets mean before the after/before` events.

I should also add, an offset of 30 means 30 seconds, not 30 minutes, which is what I suspect you wanted.

(As expressed in YAML), the after_offset and before_offset parameters, like most “amount of time”, or “time period”, options can accept a value in any one of these formats:

SECONDS
'HH:MM'
'HH:MM:SS'

or a “dictionary” like the following, where you can supply one or more of the key/value pairs:

days: DAYS
hours: HOURS
minutes: MINUTES
seconds: SECONDS
milliseconds: MILLISECONDS

So for an offset of -30 minutes, you could express that in any of these ways:

after_offset: -1800
after_offset: '-00:30'
after_offset: '-00:30:00'
after_offset: 
  minutes: -30
1 Like

The docs (which I consulted for this as I only ever use elevation) was less than clear on the before/after syntax)
I have to admit on the time thing I just went with the OP’s format and didn’t check, my apologies.

Yep, as I said, “Many have been confused by this.” I suppose I should spend some time and try to make the wording better. I’m also thinking about changing the sun conditions so they can span midnight, just like the time condition allows. I think that would make it much easier for most people to use (rather than have to write the complex “or” condition, which is also confusing to most, at least at first.) Be clear, though, I didn’t write the sun condition or its documentation in the first place. lol

1 Like

Ahhh but you are forgetting the old addage that it’s the responsibility of whoever looked at it last.

But I quite like the sun component as it is, I’m aware of your enhanced and sun2 but they don’t give me much, it just makes it easier for some to write code without considering the consequences (not always a good thing) And I must stress I mean no disrespect. And that is also not to say I won’t find a use for it tomorrow.

Given that there are so many inconsistent approaches within HA (you can use a template icon for this but not for that, customisation for this but not for that, icon colour for this but not for that, you can use before for this but must after for that AND the sign of the number inverts that anyway but I’m not going to tell you that.
The list goes on
So I’d rather they fix the inconsistencies, write a general spec all entities must comply with and then start making it happen.
But I’m not a dev