Thank you Taras.
All set. Template has been added to config and automation has been created. Let’s see if it triggers at the sunset. Finger crossed!
The trigger did not work. when I test the sensor for sunset, this is what I get
Should not we subtract the timedelta?
Add or subtract, that’s your choice. Originally you said you wanted to add the offset so that’s how I composed it.
How much negative/positive offset do you want for sunrise and sunset times?
in my original, I was thing of converting the time to local first then adding the offset butt in your formula you included the offset and conversion of time in on operation. So I just used subtraction and now the offsets are correct. Will see if the trigger happen on sunrise.
The funny thing is that I was looking at the Logbook and I saw this entry:
The time is sport on. And when I click on entry SUN, it shows this:
All times are in EST time. However in Automation it does not execute on the right time.
All I can tell you is that I have several automations that are triggered based on sunset and I also live in the Eastern time zone and all the automations trigger at the correct time.
The example I posted above is based on another example I provided for another user who needed the offset to be random (as opposed to fixed) every day. It worked for that user.
The UTC datetime shown in your screenshot, 2022-01-28T03:23:28+00:00
is 2022-01-27 22:23:28-05:00
in your local time (EST). Basically that’s today’s sunset time plus 5 hours (300 minutes) to produce 22:23
(EST). It’s currently 18:58
(EST) so it’s obvious why the automation hasn’t triggered yet.
If you want me to continue helping you, I need the information I requested in my previous post.
Thanks Taras. Let’s forget about the offset. Let say we use the actual sunrise or sunset time.
Here is the value of SunriseOffset sensor we created in my system
This is the trigger for the Sunrise
-id: 'off'
platform: time
at: sensor.sunrise_offset
However, instead of trigger gets executed at 7:36 am, it was executed at 2:36 AM
Why HA shows one time outside of trigger and different time inside trigger for the same sensor?
Did you set elevation properly for your HA location?
IIRC, elevation will wildly change the sunrise sunset. Lat/lon will change your times by about an hour in relation to your position in your Timezone.
Without all those set properly, your sunset/sunrise will be wrong
also keep in mind that elevation is in meters not feet. so… did you put feet in as meters?
This UTC time:
2022-01-28T07:36:34+00:00
is the same as:
2022-01-28T02:36:34-05:00
In other words, 7:36 UTC is 2:36 EST and the trace shows the automation successfully triggered at the scheduled time.
Except the automation is being triggered by sensor.sunrise_offset
. According to your screenshot, that sensor is not reporting today’s actual sunrise time. Post the sensor’s current configuration.
I have correct elevation stated in meter in HA.
Sorry Taras,
I am confused. Are you saying that the SunriseOffset is in UTC time and not EST? The whole discussion was to convert next_setting attribute (which is in UTC) to EST time so Time trigger would execute on that time.
This is the code for sensor
- trigger:
- platform: time
at: '00:00:00'
- platform: event
event_type: event_template_reloaded
- platform: homeassistant
event: start
sensor:
- name: 'Sunrise Offset'
unique_id: 'sensor_sunrise_offset'
state: "{{ state_attr('sun.sun', 'next_rising') | as_datetime - timedelta(minutes=300) }}"
device_class: timestamp
- name: 'Sunset Offset'
unique_id: 'sensor_sunset_offset'
state: "{{ state_attr('sun.sun', 'next_setting') | as_datetime - timedelta(minutes=300) }}"
device_class: timestamp
I subtracted 300 minutes (5 hours) to change the UCT to EST) that is why the test shows 7:36 am
sun.sun’s attributes are fully fleshed out UTC timestamps. If you subtract 5 hours, you’re actually incorrectly changing the time by 5 hours.
Just to clarify, a utc timestamp…
2022-01-29 12:26:16.413162+00:00
is identical to a timestamp local to your TZ, but it has the offset built into it
2022-01-29 07:26:16.413162-05:00
What you did is took
2022-01-29 12:26:16.413162+00:00
and subtracted 5 hours…
2022-01-29 07:26:16.413162+00:00
which is
2022-01-29 02:26:16.413162-05:00
which explains why you’re 5 hours off and it’s happening at 2 am.
That confirms my suspicion that you purposefully set back the time by 5 hours and explains why the automation triggered at 2:38 EST (instead of 7:38 EST or its equivalent of 12:38 UTC).
Respectfully, this entire issue appears to be due to your misunderstanding of how time is handled in Home Assistant, specifically time zones.
Petro has provided a clear explanation of what you have been misinterpreting.
So, I should remove the the Delta subtraction from the equation. If so, won’t the Sensor and Next_Setting attribute be the same?
Sun Sunset and Sunrise attributes did not work in my trigger. That is why I started the whole conversation to create a sensor template so I can use it in my automation.
Now that I saw the Sunset shows up correctly in Logbook I have created a test trigger with no offset to see if it is working now or not. We will see. Hopefully it will trigger at 5:30 pm and I can close this conversion and not taking anyone’s time.
yep, it will be identical
It’s evident, from your recent tests with my example, that your Home Assistant system is capable of triggering at a specified time.
Sunset is 12:36 UTC (07:36 EST) but you subtracted 5 hours thereby making it 07:36 UTC (02:36 EST) and the automation triggered right on schedule at 07:36 UTC (02:36 EST).
I will keep the code the same but I change the minutes from 300 to 10 mins. I like to keep it for future offset changes I may need. I will let you know.
That’s a reasonable offset. As I mentioned previously, this example is based on a previous one I suggested to another user. In their cases, they wanted the offset to vary (every day) by a random amount ranging from 25 minutes before to 25 minutes after sunrise/sunset.
I gave a good news. Changing the 300 to 10 minutes did the trick and Trigger executed 10 minutes early as expected. Now I can play with offset to get a better result.
The other triggers using Sun entity was by mistake set for sunrise and not Sunset. I will keep using the sensor until tomorrow to see if SUN entity will work properly or not.
Taras and Petro and everyone else who replied, thank you for your support and help.