Need suggestions for creating a sunset trigger

Hello,

I created the trigger below to turn on lights 20 minutes before dusk. It was has been working for years but stopped firing sometime after version 0.115.0. So I’ve been stuck on 0.115.0 for a while.

  trigger:
    # Trigger 20 minutes before dusk
    - platform: template
      value_template: '{{ (( as_timestamp( states.sun.sun.attributes.next_dusk ) | int ) - 1200) < ( as_timestamp( now() ) | int ) }}'

I reported this as a bug on GitHub but I’m not getting much of a response.

(One of the major downsides of HA is the sometimes cavalier approach to updates. I don’t understand why we have new versions being released every two weeks - I personally would prefer an operating system model where you have core updates released slowly and you focus on foundational stability and “app” or integration updates released when needed, e.g., every two weeks - but I digress.)

Does anyone have suggestions on how I can create a new a trigger that fires 20 minutes before dusk?

Doesn’t dusk start when the sun sets? Then you could use the sun trigger with an offset. Otherwise you could maybe also use the elevation of the sun as a trigger.

Also you got a response on your issue, the problem is that your template doesn’t include anything that changes that could cause it to trigger. From what I remember, in the past a trigger with now() was evaluated every minute and this changed, you could use a time pattern trigger to trigger every x mins and put your current trigger as a condition.

Dusk is defined as : -
Civilian dusk -6 degrees below the horizon
Military dusk -12 degrees
Astronomical dusk is -18 degrees

Different people ascribe different names to the periods between the different ‘instants’ of solar progression. Some are incontravertable whereas ‘dusk’ is extremely ambiguous.
Best to describe the period by the events bracketing it.

It seems that the OP merely requires 20mins before minus 6 degrees elevation.

The issue above is that the sun.sun integration calculates this ((binary division iteration) to the nearest second) Though his calculation uses “<” so should flip on the 1 minute calculation performed as a workaround as you described above.
:man_shrugging:
I’m not at a workstation so can’t investigate further

Edit: sun gives local time, but like now() it should be converted to (as a timestamp) UTC (both) may be worth checking

1 Like

Okay I used the sun.sun next_dusk and tested it, it seems only to produce local time (which is pretty damn surprising but) both quite convienient and simple
I don’t see the reason HA needs to bother with UTC (except as a refence for calculating sun position) All it’s outputs should be local. Unfortunately MOST of the World has DST which is a major fly in our Utopian Ideal as you would possibly get two triggers on one day for say a 00:28 time trigger and NO triggers in a day about 5 or 7 months later (depending if you are North/South Hemisphere)

Anyway I try my best not to use now() apart from maybe conditions.
sun.sun next dusk is calculated on restart and about 4 secs after the last update (so 1/day on average)
now() is a temporary workaround that sort of became permanent.
For your usage we can keep it ALL in local time so the template trigger becomes : -

"{{ (as_timestamp(state_attr('sun.sun', 'next_dusk')) - 1200) | timestamp_custom('%H:%M') == states('sensor.time') }}"

PLEASE ensure you have the time and date sensors set up

I haven’t tested this, I’m not waiting around for “20 mins before dusk” (-6 degrees elevation, for non-amiguity) but it should work, comeback if not.

Your construction of the template trigger was a little unusual, the only thing odd about it (that I could see) was casting a timestamp to int AND then doing the same with now() before comparison, though simple testing suggests this ‘should’ work (don’t have time to look into the detail (ie why it doesn’t))

Edit: Not sure why you are using time offsets on sun events (I generally prefer to use ‘elevation’ triggers as @Burningstone suggested, as these are more consistent throughout the year in terms of light levels). Anyway, each to their own.

1 Like
alias: Living Room light on
trigger:
  - event: sunset
    offset: -00:20
    platform: sun
condition: []
action:
  - service: switch.turn_on
    entity_id: switch.wall_plug_switch_current_value

That’s not 20 mins ‘before dusk’

maybe not…but it certainly answers the question asked in the subject line :slight_smile:

3 Likes

To the letter of the law regarding the title, I would have to agree
:rofl:

Using elevation is a good and a clean suggestion. I should have checked the help docs before posting; I’ve been using this trigger since 2017 and I think it may predate the elevation documentation that was released in June of 2017.

I also gravitated towards time originally because it is more consistent throughout the year and the trigger is location agnostic. However, in all reality, I would never notice because the difference would only vary by seconds through the year or anywhere in the world.

As for the now() evaluation, I don’t recall reading that in the breaking notes logs but I probably overlooked it.