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.
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.
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
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 : -
And remember Test, Test, Test
(as finity’s reference was tagged, he may come by to kick the crap out of any errorspolitely point out possible improvements)
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)
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.