Value template - Sun elevation with time offset

I know it’s possible to use time offsets for sunrise/sunset. However I want to use that for elevation.

So on the example page we have:

- alias: 'Turn a few lights on when the sun gets dim'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state_attr('sun.sun', 'elevation') }}"
    below: -18
  action:
    service: scene.turn_on
    entity_id: scene.background_lights

Is there a way for me to adjust this with a time offset. So for example in the morning as the sun is rising, 30 minutes before reaching an elevation of -18, I switch the lights on.

The sun component calculates where the sun is given your latitude, longtitude, altitude and time (utc) it will also recalculate next noon, midnight, sunset and sunrise.(and display them in local time)
They are done using binary division to get a best guess solution (till the time division is less than 1 sec). .
This is not done lightly and requires special programing.
What you want is to calculate the time for an arbitrary elevation at your bespoke geolocation and then offset from that.
This is not a trivial thing.
The standard sun component won’t do that, your best bet would be to explore the enhanced sun component (may be called sun2) as that has a bit more added to it.
I can’t tell you more as I haven’t used it.

1 Like

That’s pretty cool! It’s got astronomical dawn tracking. Now just need to figure out how to create a trigger that is offset from that point

What latitude are you at ?
The reason I ask is that I’ve ssen someone who at “a period of the year” does not see the sun dip below 18 degrees so it doesn’t trigger.
So you will also need to check that the “next astrnomical dawn” is less than (say) 19 hours in the future and/or no more than 19 hrs in the past.
(because I think Phil’s component updates at midnight.

My latitude is 52.638207.

I’ll be checking if the astronomical dawn is between certain hours

{% if as_timestamp(states("sensor.astronomical_dawn") | timestamp_local) - as_timestamp(now() | timestamp_local) < 60 * 60 * 1000 and as_timestamp(now()) | timestamp_custom('%H %M', True) > "01:00" and as_timestamp(now()) | timestamp_custom('%H %M', True) < "03:00"-%}
{%- endif %}

so I’ve got this as my automation template… wondering whether that could do a job?

The trigger is:
if the time now is less than one hour till astronomical dawn & the time is between 01:00am and 03:00am

With that lat you won’t have a problem with achieving less than 18 degrees.
So you can simplify the template.
You get the time for astrnomical dawn convert to timestamp subtract 30*60 round with // 60 * 60 (to get to the whole minute) and the get HH:MM from that and compare sensor.time with that value.
It will then check every minute for a match

I’ve lost you on the calculations there :sleepy:

Okay, I typed it out for you
admittedly I had to consult finity’s Epic Thread : -
:meh:

Which should give you pretty much everything you need to know about time …

  - alias: Wake Me Up Before The Astronomical Dawn (Sung to the popular 'Wham !' tune) (I'm in a Deadpool Mood !)
      trigger:
        - platform: template
          value_template: "{{ states('sensor.time') == (as_timestamp(states('sensor.astronomical_dawn')) - 30 * 60) | timestamp_custom ('%R') }}"

Because we were going for a custom timestamp I could discard the //60 * 60 and the %R substitutes for ‘’%H %M’’
Now remember I don’t have this sensor so I’m extrapolating from known states so test this in your template editor
example test templates : -

"{{ as_timestamp(state_attr('sun.sun', 'next_dawn')) | timestamp_custom('%Y-%m-%d %H:%M:%S') }}"
"{{ states('sensor.z_next_dawn') }}"
"{{ as_timestamp(states('sensor.z_next_dawn')) | timestamp_custom ('%R') }}"
"{{ (as_timestamp(states('sensor.z_next_dawn')) - 30 * 60) | timestamp_custom ('%R') }}"
"{{ states('sensor.time') == (as_timestamp(states('sensor.z_next_dawn')) - 30 * 60) | timestamp_custom ('%R') }}"

( NOTE: MY sensor is called z_next_dawn - yours Won’t Be )
Produces : -

And remember Test, Test, Test
(as finity’s reference was tagged, he may come by to kick the crap out of any errors politely point out possible improvements) :rofl:

Another comment is your quotes were all over the place, A single line template (like this) Has double quotes on the outside and ALL single quotes inside (there are other ways but try to do this as it’s common on this forum, and standard means less errors when you cut and paste)

1 Like

Why not using the way it is shown in the firste example:

Awesome. Thank you so much.

`platform: template
value_template: >-
  {{ states('sensor.time') == (as_timestamp(states('sensor.astronomical_dawn'))
  - 45 * 60) | timestamp_custom ('%R') }}`

I put that in and got it working. Putting entity id in was giving me an error?

And sorry about the quotations. I’ll bare that in mind! I’m a bit careless with the way I do my quotation marks as you can see :face_with_hand_over_mouth:

If you read the OP then you will see he wanted it as a time offset from -18 degrees (astronomical Dawn) the sun component does not provide astronomical Dawn so he’s moved to Phil’s enhanced sun component. I’m not sure that Phil’s component provides offsets (unless you have it and have tested it otherwise, but in that case, why are you pointing to standard sun.sun documentation ? )

Somehow I think you may have missed the point of the thread. Please correct me if I’m wrong.

My fault. You’re right. Didn’t get it the first time and missunderstood that. Sorry :see_no_evil:

1 Like

is there a way for me to add the astronomical dawn time as card on the dashboard formatted to just hh:mm:ss rather than as a timestamp?

Yep, you’d have to create a template sensor and show that, see the top example of the test templates I gave you and work it from there

1 Like

emmpii, when you get a solution you normally have to do two things.

  1. Post the code for that solution (not necessary here as I posted it for you, but some people only need a little guidance and take it from there)
  2. Mark the thread as solved, usually by marking the post that helped you most to get there.

In combination these help the next guy looking for a solution to a particular problem