Strange problem with sunset times

Hi,

I’m having a weird problem with an automation that’s meant to trigger either at sunset/sunrise or when a light is switched on. The idea is to change the light to a warmer colour in the evening. The problem I’m having is that it seems there are two types of sunset (?) so my automation isn’t working. The automation itself is triggered but the colour doesn’t change, I’m guessing because when the automation runs the sun is not considered to be below the horizon.

Whatever the cause, it is showing up in the form of this weirdness in the logbook, where the automation is triggered by the sunset, then ~5 minutes later the sun sets.

Screenshot 2021-01-10 184753

Here’s the automation itself:


# Light Colour
- id: kitchen_colour
  alias: Kitchen Light Colour
  trigger:
  - platform: sun
    event: sunrise
  - platform: sun
    event: sunset
  - platform: state
    entity_id: light.kitchen_light
    to: 'on'
  condition:
    condition: state
    entity_id: light.kitchen_light
    state: 'on'
  action:
  - delay:
      seconds: 1
  - service: light.turn_on
    data_template:
      entity_id: light.kitchen_light
      color_temp: >
        {% if is_state('sun.sun', 'below_horizon') %}
            370
        {% else %}
            250
        {% endif %}

Any help would be much appreciated!

Thanks,
Tom

The only clue I could find to this behavior is a line from the documentation:
“Since the duration of twilight is different throughout the year, it is recommended to use sun elevation triggers instead of sunset or sunrise with a time offset to trigger automations during dusk or dawn.”
That does seem to imply that sunset and sunrise aren’t as exact as elevation.

What you really want is probably dawn and dusk rather than sunrise and sunset, but fewer systems distinguish those extra settings. Try asking Google, “Hey Google, what time is dawn/dusk today?” vs. “what time is sunrise/sunset today?”. Alexa doesn’t distinguish.

I had the same issue with sunset routines and as you have said the method of determining sunset in HA is now based on the angle of elevation or so. It was earlier based on time but they revised it about an year back. So now I use a template to check the current time with the next setting time to start any routines at sunset. I also use the sunset as a second trigger as a backup if the template trigger fails because of any cause.

{{ as_timestamp(states.sensor.date_time_iso.state) == as_timestamp(as_timestamp(state_attr('sun.sun', 'next_rising')) | timestamp_local) }}

For now I don’t really mind which I use of dusk/sunset (not totally clear on the difference tbh), but I’m mainly curious as to why there is the two sunsets as seen in the logs. According to the documentation sunset and below horizon should be the same but it seems they are not. I found this thread with the same issue so I will try the proposed solution and update here if it works.

If I have time at some point I will dig into the codebase and see where the difference between sunset and below horizon is coming from since I think they should be the same (and if not the documentation needs to be updated).

Sunset is defined astronomically as when the upper limb of the sun is below the horizon.

I’m guessing elevation is taken from the centre of the non-point-like disc of the sun. This disc has an apparent diameter of 32 arc-minutes, or half a degree. As the sun moves at 15° an hour, at the equator it would take ~60 seconds from the centre of the sun reaching 0° to the upper limb reaching 0° (neglecting atmospheric effects).

At latitudes other than the equator the sun sets at an angle, rather than vertically, thus it takes longer.

Thus the sun’s elevation is at 0° before sunset, and the time between the two depends on your latitude.

One of your sunsets (the one that triggers first) is using the wrong definition of sunset (0° elevation).

Sunset / sunrise is when the sun is at -0.86° elevation (as you alluded)
So you can use that as a trigger
Dusk / dawn is normally civilian Dusk / Dawn which is at -6° elevation

No this is always true, but it’s along the sun arc, which is not always (actually very rarely) perpendicular to the horizon (unless you are a flat earther, then I have no fecking clue how you calculate this s41t # :rofl: )

@tsharris - But that automation looks okay to me, what seems to be the problem with it ?

You’ll have to explain that !

If you are concerned about this (and the issue that 1 second after the sunsets, the value it then displays is for tommorows sunset (same with sunrise) you may be better increasing your delay to (say) 2 mins (that should deal with a considerable amount of precession (unless you are really close to the arctic/antarctic circles ???) )

I was referring to the way the log book shows that an automation is triggered by sunset, then 4 minutes later says that the sun has set.

The problem for the automation is that although it is triggered, the light changes to the wrong colour. From the fact that we see two sunsets in the logbook I’m guessing it’s because the is_state below is returning false (i.e. the sun is not yet below the horizon at sunset, but is 4 minutes later when the “Sun set” entry appears in the log).

      color_temp: >
        {% if is_state('sun.sun', 'below_horizon') %}
            370
        {% else %}
            250
        {% endif %}

Either way I guess there’s a bug somewhere (even if just in the wording in the log) as it doesn’t make sense in the log book to have an entry for an automation triggered by the sunset happen 4 minutes before the entry for the sun setting. They should occur at the same time.

I forgot to mention the delay is a bit of a red herring - it’s there because we sometimes use the wall switch and it takes a second for the lightbulb to “boot up” before it accept commands.

I have another automation that’s exactly the same for a standing lamp except without the delay and it has the same issue.

The issue maybe that calculations for sun set and rise are dependent upon utc, latitude, longitude and elevation (eg Marius lives below sea level) That should self resolve.
You are also using two different attributes for your automation.
I haven’t checked but I suspect that below the horizon is anything below 0 elevation (so sunset hasn’t happened or with sunrise, happened 0.86 degrees ago)
Make your automation more consistent using consistent triggers and conditions.
Create a binary sensor ‘below_horizon’ then both trigger and test from that

I did no such thing. I said:

Where did you get that 0.86 value from?

If anything it should be 16 arc-minutes, which is pretty close to a quarter of a degree (0.2667).

Which is what I said.

It was a simplification that I then expanded upon.

The take away is the last sentence.

It’s a combination of half the apparent angular diameter of the sun and the atmospheric distortion caused by the atmospheric lens effect. ie the upper limb is already below the horizon but the light is bent round so that it is still visible. There’s a very good article on it over on Wikipedia. Sorry, perhaps I thought you knew more about this subject than you do.

So No.

I don’t get it, you are arguing with me because I agreed with you ? But I said it in a different way and because I gave the actual elevation for accepted sunset/sunrise ?
:man_shrugging:

Edit:

It’s about 3/4 of the way down and I should correct myself above as they list it as - 0.83 degrees

I’m not arguing with you. As usual you are just confusing the subject. I deliberately said:

Then you go and introduce exactly this without explaining it and confusing the matter.

Honestly sometimes I wish you would just pull your head in and shut the hell up. 9 times out of 10 your posts only confuse the matter or make incredibly unfunny cringey “jokes”.

I must apologise for the delay in response as I had to deal with a real issue at home, but also the shock in receiving your such message.
This is by definition is a personal opinion for a character assassination.
You ‘could’ hold a vote for people who find me irritating, but again doing so would be speaking more about ‘you’ than about me.
This is meant to be a ‘community’ and as such more than just questions and ‘abrupt’ responses, sterilised and free from any hint of the personal.
But this can’t be true as I have read innumerable posts (quite a few by you) where they deride the OP for their question / ignorance. Or their inability to follow (what the commenter considers) ‘simple instructions’.
I think that people with questions can be put at ease when the person answering proves ‘to be human too’.
Still I don’t expect you to agree and yet I choose to express my opinion anyway as not doing so would be yield to your perceived intent to deprive me of it.

As Mutt said, you’re using 2 different attributes of the sun (sunset/sunrise and below_horizon) in your trigger and condition. They don’t happen at the same time so your automation can’t work. I took a screenshot to illustrate it (I don’t have the exact elevation for sunrise as the datapoint is missing).

Using a single sun attribute in your trigger and condition should fix your color temperature issue.

As for this, it looks like a bug in HA. I see that you have already reported the issue on github (#27068)

1 Like

No need. The PMs I get about your posts are enough.

It’s not character assassination if it’s true. It’s just criticism you should learn from.

:rofl:
No doubt you were ‘robbed of the election’ also

Tom Harris (to ensure full distinction)
I must apologise for this personality clash being run on your thread and my part in it.
@jay_p managed to show the problem graphically (1 picture == 1,000 words) :+1:
I hope this clarifies the issue and you manage to solve your problem
Cheers

Hi

I too have the insteon issues. I have the 2245-222 hub, I installed the HA on a new raspberry pi4. I keep getting the usb device path on the insteon integration page.I cannot get past it. Would you know what link I can use or if there is a way I can contact Tom Harris. I know he has a package that you can load in HA for insteon, I just dont know how to do it.Any help would be greatly appreciated.Thanks.