Hi,
Just joined the community.
I have been searching community for simple detecting sunset and Sunrise (Not Dusk or Dawn).
I have seen lots of codes provided from using below and above horizon to Sunset and Sunrise event of sun and elevation and azimuth. And they did not work for me. I wanted to get exact sunset and sunrise bases on its time. I see SUN entity has such attribute and I am trying to use that for my automation.
I have a toggle Helper where I like to set it to on when it is Sunset and off when it is Sunrise. This Helper will be used by several automations in my home assistant…
So far I have created the following template value by using the next_setting attribute of sun entity.
However, the result is not local time. I tired to use timepstamp_local filter before float and it did not work. I tried to use as_local() in replace of as_timestamp and that did not work. If I use the following code I get the correct time
So, the trigger started and action was taken but at a wrong time. I checked the time zone in configuration and it is set correctly. What I do not understand is the time stored in sun.attributes. It does not make sense to me.
Here is the Sun attributes information on my system
Okay… really off the wall here but… when you drop down to root on your HA server and enter “date” at the command prompt… do you get an accurate local date and time?
Hi Tara,
Thank you for your suggestion. I found out that the Sun attributes times are not correct. So, even template would not help when I pull in the Sun next setting attribute value. I am going to search to see if anyone else had issue with SUN attributes value. If I fix the issue, then I think my current automation will take care of it.
Then the clock and or timezone is not configured correctly on your system or you live north of the Arctic Circle or the elevation option is incorrect. In other words, something isn’t set correctly because the sun integration normally reports accurate values.
Me thinks 123 is on to something. You should have something like this in your configuration file: Your numbers will vary. I got mine from doing a Google Earth on my home:
Hi Taras,
Thanks for your reply. I thought you are on to something with elevation value. The configuration section had an elevation = 0 so, I changed it to elevation of my location. However, it did not change the sunset attributes values.
here is the result from template code you ask me. It seems right to me.
and here is the sunset attributes values.
There are 7 hours difference between UTC and Local time and I don’t think these times are in UTC. IF I subtract 7 hours from Dusk time it would 3:55 pm and Dusk in our place happens at 5:55 pm. if I subtract 6 hours that is close.
If we fix the attributes time then we should be ok to use Sunset and Sunrise trigger.
Your time zone is set to America/New_York which is 5 hours less than UTC (even says so in the template results you posted above, -05:00, which is UTC minus five hours). So why do you believe there should be 7 hours difference?
If the time where you live is actually UTC-07:00 then you have selected the wrong time zone.
Sorry my bad. You are right, it is 5 hours difference. So, we are back to square one. We know the trigger runs on UTC time for SUN attributes. Only option is to create a template to convert those attributes to local time and then added to trigger of automation.
I need a template to convert the attributes value to local time and apply offset in format of hh:mm and compare it with current time. If it is match then true and if not is then false
The two sensors are updated at the start of every day (midnight) and whenever Home Assistant is restarted or Template Entities are reloaded. I set the offset to +300 minutes but change it to whatever you require.
Create an automation that is triggered by the two sensors. The offset sunset sensor turns the input_boolean on and the offset sunrise sensor turns it off. Change the name of the input_boolean to whatever you require.
alias: example sunrise offset
- id: 'on'
platform: time
at: sensor.sunset_offset
- id: 'off'
platform: time
at: sensor.sunrise_offset
condition: []
action:
- service: "input_boolean.turn_{{ trigger.id }}"
target:
entity_id: input_boolean.whatever