Sun condition not working

Hello,

I have a KaKu motion sensor that need to enable a light when there is motion after sunset and before sunrise. After a minute the lamp needs to go off again.

The automation is working properly but only when I don’t have the sun condition configured.
For some reason the sun condition is not working although my home assistent shows the state of sun properly. What is going wrong?

This is my current code:

alias: Light on Motion Hall Upstairs
trigger:
  - platform: state
    from: 'off'
    to: 'on'
    entity_id: binary_sensor.kaku_motion_sensor_hall_upstairs
condition:
  - condition: and
    conditions:
      - condition: sun
        before: sunrise
        after: sunset
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.kaku_dimmer_hall_upstairs
        - light.kaku_dimmer_hall_downstairs
    data:
      brightness_pct: 67
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    target:
      entity_id:
        - light.kaku_dimmer_hall_upstairs
        - light.kaku_dimmer_hall_downstairs
mode: single

I first had it without the AND condition but that wasn’t working either. Any ideas?

I know ‘time’ wraps round midnight - not sure sun does, try : -

  - condition: or
    conditions:
      - condition: sun
        before: sunrise
      - condition: sun
        after: sunset

As a quick test

Haven’t test this yet, but why two time before sunrise?

Do you have the correct time zone set in HA?

I suggest you use a simple State Condition like this:

condition:
  - condition: state
    entity_id: sun.sun
    state: 'below_horizon'
6 Likes

I’m in agreement with 123 Taras here. I believe that sun currently wraps around to the next day so you actually can do after sunset and before sunrise, but that always triggers my “maybe not” worrier. The below horizon takes care of that ambiguity.

Sorry, cut and ‘screw up’ error, meant two conditions before sunrise and after sunset
I’ll edit my post

Yes, I as mentioned HA is showing the state of sun properly.

Good suggestion, will try this tonight. Thanks

The docs specifically mentions this is not possible like this. Just read it and you’ll understand why :wink:

1 Like

Thank you, I’ve read it again and see something important that explains why mine was not working :wink:

to cover time between sunset and sunrise one need to use after: sunset and before: sunrise as 2 separate conditions and combine them using or.

1 Like

This sunrise/sunset conditional error occurs fairly often. It’s natural to assume one must logically AND the two conditions, only because most people are unaware of how “midnight” is handled (serves as a dividing line).

The documentation attempts to clarify it in the section’s second paragraph but the accompanying YAML example doesn’t appear immediately after the paragraph but at the very end of the section.

Anyway, if the goal is to simply make the condition confirm it’s between sunset and sunrise, then it’s easier to use a State Condition that looks for below_horizon.

Thank you for you explanation and solution. This below_horizon works perfectly fine :grinning:

1 Like

below_horizon ‘works’, but you can’t specify an offset. Something with sunrise/sunset is supposed to support.

Update:
Found out you can’t create one single condition and use both before after.

This works for me: (activates lights when dark)

condition: or
conditions:
  - condition: sun
    after: sunset
    after_offset: '-00:30:00'
  - condition: sun
    before: sunrise
    before_offset: '00:30:00'

1 Like

Nevertheless, this is what Home Assistant is suggesting when you select Sun as the Condition Type.
It then should better not be possible to select it, to prevent one to be led to confusions.

The automation editor doesn’t make any suggestions, it just shows the items which can be configured. I think it is unwanted to not allow certain configurations because some users expect it to work differently while it can still be useful for others.

Anyway it doesn’t make sense to post these comments in a topic which is solved. Better keep these topics “clean” and make a feature request if you think things should be done differently.

1 Like

That works perfectly fine, but it’s a recent addition to the code. Your issue is somewhere else.

Instead of bashing people you don’t know, how about you create a separate post with a question? This isn’t a video game forum and posts like this will not be tolerated.

1 Like

use the entity state condition. set the entity to “sun.sun” and set state to “below_horizon” or “above_horizon”, it worked for me.

ymal code:

condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
condition: <-- this is needed apparently
  condition: or
  conditions:
    - condition: sun
      after: sunset
      after_offset: '-01:00:00'
    - condition: sun
      before: sunrise
      before_offset: '01:00:00'

Yes. That’s the condition field in the auotmation. The rest is the actual condition.