Automation triggered by sunset but sun was not below_horizon?

I made an automation that is supposed to keep my blinds closed at night and also while a timer is running and open otherwise:

trigger:
  - platform: sun
    event: sunrise
    offset: 0
  - platform: sun
    event: sunset
    offset: 0
  - platform: state
    entity_id:
      - timer.blinds_timer
    from: idle
    to: active
  - platform: state
    entity_id:
      - timer.blinds_timer
    from: active
    to: idle
action:
  - service: cover.set_cover_position
    metadata: {}
    data:
      position: >-
        {{ 0 if (is_state('sun.sun', 'below_horizon') or
        is_state('timer.blinds_timer', 'active')) else 100 }}
    target:
      entity_id: cover.blinds_living_room

But instead of closing the blinds at sunset, it opens them, and indeed in the trace I can see that the position evaluated to 100 instead of the expected 0:

image

That seems to indicate that is_state('sun.sun', 'below_horizon') was false when the automation was triggered.

If I evaluate the position template now (during the night) it will evaluate to 0, just as expected:

What’s going on?

supposed to keep my blinds closed at night and also while a timer is running

But your image in the template editor says an OR
It seems like you want an OR based on your post but not sure what the timer is for exactly. What does the history log say for the sun the and timer at the time?

I got off HA native automations long time ago and moved to NodeRed before they added the traces …et al.
How does this trace till you it was the sun vs the timer that failed the OR, cause it could have had to been either/both?

That’s just the difference between English and code. I want the blinds closed when the sun is down and I also want the blinds closed when the timer is running. In code that’s “if sun down or timer running then closed”.

How does this trace till you it was the sun vs the timer that failed the OR, cause it could have had to been either/both?

If is_state('sun.sun', 'below_horizon') had worked as expected (i.e. been true), the position would have been 0, no matter what the timer value is. So no, it could not have been the timer value that made this fail.

Do you have the sun elevation attribute extracted to its own sensor?

If so, check the value at the previous sunset time.

Astronomically sunset occurs when the sun is about a degree below the horizon but due to refraction this looks like the sun has just disappeared.

This does not help explain your case though. Are you sure your location, time and time zone are set correctly?

I’m now logging elevation, the sun.sun state every minute as well as the sunset event. Tomorrow at sunset we’ll know more.

Great tip about the elevation attribute by the way because I can use it to adjust what I consider “sunset”.

Are you sure your location, time and time zone are set correctly?

Those shouldn’t matter because I’m not concerned about syncing up my automation with the real sun.

They very much matter. If they are incorrect you will not get a correct sunset prediction.

But I don’t really care (yet) when the sunset automation triggers. I care that the state of sun.sun is wrong when it triggers.

I have more information now.

[2024-04-23 20:01:00 +0200]
triggered by time_pattern
not below_horizon
elevation = 5.77
[2024-04-23 20:43:33 +0200]
triggered by sunset event
not below_horizon
elevation = -0.36
[2024-04-23 21:01:00 +0200]
triggered by time_pattern
below_horizon
elevation = -2.97

When the sunset event fires, the elevation is -0.36, which is not low enough (it would have to be < -0.833) to be considered “below_horizon”.

I’m not sure how the “sunset” event is triggered, I think this is the code that calculates the exact time of the next sunset event, but I don’t know enough Python to understand it. (What does _AstralSunEventCallable do?)

Do you have a height set in your location settings?

Apparently this should be set to your height above ground level (so 0 to 5m in most cases), not the height of your location as stated in the HA docs.

See: Next_rising and next_setting still shows appr. 2 minutes too late (GMT+1, central europe, Slovenia,) - #21 by pnbruckner

Nope.

image

You should just change the automation triggers (above) to:

  - platform: state
    entity_id: sun.sun

You’re testing the state in the actions, so you should trigger on when the state actually changes, not something that should indicate when the state changes (but actually doesn’t.)

And you should try the custom_component Phil (pnbruckner above :wink: ) has developed, it’s called Sun2:

No understatement necessary here, @pnbruckner , your integration is so great, you should advertise it! :slight_smile: :+1:

1 Like