Hey guys!
Could u please help me with these template for calculating current time and time in sun.sun
next sunrise?
I want to get a sensor that will now show the time remaining until the next sunrise.
Hey guys!
Could u please help me with these template for calculating current time and time in sun.sun
next sunrise?
I want to get a sensor that will now show the time remaining until the next sunrise.
Next sunrise is this many seconds away:
{{ as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(now()) }}
Thanks, but I can’t add custom timestamp in your template for human readable value. Could u help me?
This is overkill as days aren’t needed but I just cut and paste a known good template as I’m in a rush:
value_template: >
{% set time = (as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(now())) | int %}
{% set minutes = ((time % 3600) / 60) | int %}
{% set hours = ((time % 86400) / 3600) | int %}
{% set days = (time / 86400) | int %}
{%- if time < 60 -%}
Less than a minute
{%- else -%}
{%- if days > 0 -%}
{{ days }}d
{%- endif -%}
{%- if hours > 0 -%}
{%- if days > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ hours }}h
{%- endif -%}
{%- if minutes > 0 -%}
{%- if days > 0 or hours > 0 -%}
{{ ' ' }}
{%- endif -%}
{{ minutes }}m
{%- endif -%}
{%- endif -%}
Sorry, It was my mistake, here is working for me:
{{ (as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(now())) | timestamp_custom('%Hh %Mm %Ss') }}
Btw, how can I directly take into template the time zone? Since the time turns out to be an hour less, right now I got utc+3 summer time.
In the template editor which one of these is incorrect?
{{ state_attr('sun.sun', 'next_rising') }}
{{ now() }}
Actually I do something not understandable for me at all, but, I received what I needed in my ask, time before 1 hour 'till next sunrise:
{{ ((as_timestamp(states.sun.sun.attributes.next_rising) | int - as_timestamp(now())) - 60*240) | timestamp_custom('%Hh %Mm') }}
But it seems to me that these are experiments with not fully understanding the template.
My next sunrise is tomorrow at 5:58 AM
My current time is 2:53 PM
My sensor is returning a value: 14:05
If we add it up it turns out 4:48 AM - 1 hour 'till sunsire.
Next rising is in UTC just like the documents say (I should have checked):
Try this:
{{ (as_timestamp(state_attr('sun.sun', 'next_rising')) - as_timestamp(utcnow())) | timestamp_custom('%Hh %Mm %Ss',false) }}
The local_time
option was the issue. It was adding 3 hours.
Thanks, now my template looks like this:
{{ ((as_timestamp(states.sun.sun.attributes.next_rising) | int - as_timestamp(now())) - 60*60) | timestamp_custom('%H %M %S', false) }}
This isn’t needed:
if you use utcnow()
as in my template.
Doing that means you won’t have to change the template at the end of daylight savings.
This is necessary if I want to subtract 1 hour, as I wrote above.
And here I started thinking …
Maybe I misinterpreted at first, my goal was to get the time 1 hour before sunrise.
Oh right. I missed that. Sorry.
Actually I just tested it. Using now()
or utcnow()
makes no difference. Which is strange.
So, this sensor, it get me what I need:
{{ ((as_timestamp(states.sun.sun.attributes.next_rising) | int - as_timestamp(now())) - 60*60) | timestamp_custom('%H %M %S', false) }}
time before sunrise -1 hour.
Do you think this should depend on the DST? Or I misread your answer.