Read sunset offset at a given time?

updatet and checked accordingly.

should have been this btw;

  sunset_offset:
    friendly_name: Sunset offset
    entity_id: sensor.time, sun.sun
    value_template: >
      - {{(as_timestamp(state_attr('sun.sun','sunset')) - 
         as_timestamp(now()))| timestamp_custom('%H:%M:%S', false) }}

55

cool.

about the (now) trap, why isn’t this template hindered by that:

  time_template:
    friendly_name: 'Time'
    value_template: >
      {{ as_timestamp(now()) | timestamp_custom("%H:%M %d/%m", true) }}

That’s because template sensors that don’t use the manual entity specification (i.e., the entity_id config parameter) and don’t contain any entities in the template that can be automatically extracted will update on every state change in the system. And since you have sensor.time defined, which causes a state change every minute, this particular template sensor will consequently be updated every minute. But

There is a fairly recent change that I happened to notice that will change that behavior. See PR #17276 and PR #17319. This will cause this type of template to only update once at startup, and more importantly not for every state change in the system.

Interestingly, there is also another change coming that will add a service to manually force an entity to update – see PR #17278.

So, bottom line, it works now, but won’t work soon.

a yes, I’ve seen these PR too.
thanks for listing them here so meaningful. Guess some will need the entity_id in the value_template, like these discussed here. Other might benefit from the manual updating.
As said in the PR comments, some suffer from serious timing issues, and indeed these are very frequent in my logs, so I expect a great benefit.

please let me get back on the template for the offset:

apparently the offset doesn’t work as I hoped after all, since the above template now (after todays sunset) shows the time to the next sunset, while I would have hoped it to show the time passed after todays sunset.

{{(as_timestamp(now()) -
as_timestamp(state_attr(‘sun.sun’,‘sunset’)))| timestamp_custom(’%H:%M:%S’, false) }} does show that, but is wrong when evaluated at a moment before todays sunset.
Guess I need them both:

when now() is before sunset use:

  • {{(as_timestamp(state_attr(‘sun.sun’,‘sunset’)) -
    as_timestamp(now()))| timestamp_custom(’%H:%M:%S’, false) }}
    when now() is after sunset use:
    {{(as_timestamp(now()) -
    as_timestamp(state_attr(‘sun.sun’,‘sunset’)))| timestamp_custom(’%H:%M:%S’, false) }}

would this be the correct way to write that:

{% if as_timestamp(now()) < as_timestamp(state_attr('sun.sun','sunset')) %}
   - {{(as_timestamp(state_attr('sun.sun','sunset')) - 
        as_timestamp(now()))| timestamp_custom('%H:%M:%S', false) }}
{% else%} 
   {{(as_timestamp(now()) - 
      as_timestamp(state_attr('sun.sun','sunset')))| timestamp_custom('%H:%M:%S', false) }}
{%endif%}

well that was sooner than expected…:wink: see the second Time sensor. Updated to 0.81.0b2 today… which is a huge step forward, and much more responsive. breaking change though.

57

Now you’ve got me confused again. I asked if you just needed the template to work between sunset and midnight and you said yes:

But now it looks like you want it to work all during the day. And I’m also confused as to what you want it to show.

I’ll take a stab and guess you want it to show the relative time to today’s sunset, such that before sunset it shows a positive time (i.e., how long it is before sunset), and after sunset it shows a negative time (i.e., how long it’s been since today’s sunset.) Is that it?

If so, how about this:

{% set nw = as_timestamp(now()) %}
{% set ss = as_timestamp(state_attr('sun.sun', 'sunset')) %}
{{ '- ' if nw > ss }}{{ (nw - ss)|abs|timestamp_custom('%H:%M:%S', false) }}

almost: before sunset it should give a negative time (sunset minus time) and after sunset a positive (sunset plus time)

my current template does that, but yours seems so much more sofisticated…:wink:

and yes, I might have been confusing… what I meant was the template should work per 24 day, and not per period between sunsets…

No problem.

Easy enough, just change > to < in the first if statement:

{% set nw = as_timestamp(now()) %}
{% set ss = as_timestamp(state_attr('sun.sun', 'sunset')) %}
{{ '- ' if nw < ss }}{{ (nw - ss)|abs|timestamp_custom('%H:%M:%S', false) }}
1 Like

thank you very much, very nice indeed. One for the manuals!

all of a sudden my windchill template is horribly off, could this have to do with that change too? I would have thought these entities are enough in the template itself?

  jagti_windchill:
    value_template: >
      {% set temp = states('sensor.rsd_temperature')%}
      {% set wind = states('sensor.rsd_wind_speed')%}
      {{(13.12 +0.6215*float(temp) -
         11.37*(float(wind)*3.6)**0.16 +
         0.3965*float(temp)*(float(wind)*3.6)**0.16) | round(2) }}
    unit_of_measurement: '°C'
    device_class: temperature
    friendly_name: Jag/Ti Wchill

I would think so, too. It should definitely be finding those entities and watching for their state changes.

It could be that those entities are somehow working differently themselves. See this topic for something similar.

right, thanks!
tested this:

  {% set temp = states('sensor.rsd_temperature')%}
  {% set wind = states('sensor.rsd_wind_speed')%}
  {{(13.12 +0.6215*(temp|float) -
     11.37*((wind|float)*3.6)**0.16 +
     0.3965*(temp|float)*((wind|float)*3.6)**0.16) | round(2) }}

but that results in the same below zero… must check if something else went wrong somehow. Most of the time the jagti windchill is quite accurate

Ok, it was worth a shot. BTW, it would be simpler/cleaner to put the float conversions in the set statements:

  {% set temp = states('sensor.rsd_temperature')|float %}
  {% set wind = states('sensor.rsd_wind_speed')|float %}
  {{(13.12 +0.6215*temp -
     11.37*(wind*3.6)**0.16 +
     0.3965*temp*(wind*3.6)**0.16) | round(2) }}
1 Like

tanks, much cleaner indeed. btw, the float operations were only necessary because at startup it would generate errors about not being able to multiply float by int (from memory here, not exactly that probably).

It always gave a correct value though. Now seems truly off, so maybe the weather sensors changed, ill have another go.

just for info and completeness sake, here the revised sensor (2 variants that do work)

  jagti_windchill:
    value_template: >
      {% set temp = states('sensor.rsd_temperature')|float %}
      {% set wind = states('sensor.rsd_wind_speed')|float %}
      {{(13.12 +0.6215*temp  + 0.3965*(temp - 28.676)*(wind**0.16)) | round(2) }}
    unit_of_measurement: '°C'
    device_class: temperature
    friendly_name: Jag/Ti Wchill
 # https://www.meteo.be/meteo/view/nl/68771-FAQ%27s+over+het+weer.html?view=3268614
  kmi_windchill:
    value_template: >
      {% set temp = states('sensor.rsd_temperature')|float %}
      {% set wind = states('sensor.rsd_wind_speed')|float %}
      {{(13.12 + 0.6215*temp - 11.37*(wind**0.16) + 0.3965*temp*(wind**0.16))|round(2)}}
    unit_of_measurement: '°C'
    device_class: temperature
    friendly_name: Jag/Ti Wchill KMI

Any chance of an updated version of that? The test for > or - doesn’t seem to work anymore. Probably a python3 change? I get a unknown error in the template editor and TypeError: unsupported operand type(s) for -: 'float' and 'NoneType' in the log.

That template assumes the use of my custom sun component. If you’re not using that then as_timestamp(state_attr('sun.sun', 'sunset')) will return None since normally sun.sun does not have a sunset attribute.

1 Like

Thanks :sunrise: Although the normal sun.sun has next_setting, so as_timestamp(states.sun.sun.attributes.next_setting) works as well. That’s how i ended up with the next_setting initially. I thought i can access the “Next setting in x hours” info that’s on the (normal) sun popup, but couldn’t get it to work. Your solution is more precise :slight_smile:

I don’t know what you’re trying to do with the information, but just be aware that the next_setting attribute changes at sunset. So for most of the day it indicates when the sun will set today. But at sunset it changes to tomorrow’s sunset. In some use cases that may be fine, but in many it’s not, which is the whole reason I came up with my custom sun component. It provides a sunset attribute which stays constant the whole day and indicates today’s sunset, even if it has already occurred.

1 Like

Man, that’s what i like so much about home automation in general, you realize there is so much more under every stone you turn. For me, i was simply thinking about an automation to hide the offset at sunset since i deemed the information useless after the sun has set. Of course it makes sense for different applications than my narrow use case. I’m using it simply to show on a card how much sunlight i have left to go on a run/ride. :+1:

I’m trying to do something similar but much less complicated.

I’m simply want to change the offset if it’s a sunny day or not.
I’ve already made a helper binary sensor that determines if it’s sunny or not.
I’m just not sure how to change the offset using that binary.
This is my automation so far, it always erases my logic for the offset so I think I need to do this another way. I’m not sure.

alias: Daily Lights
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: {{ '-1:00:00' if is_state('binary_sensor.is_sunny', 'off') else '+0:00:00' }}
    id: sunset
  - platform: sun
    event: sunrise
    offset: "-03:00:00"
    id: sunrise
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - sunrise
        sequence:
          - service: script.all_off
            metadata: {}
            data: {}
      - conditions:
          - condition: trigger
            id:
              - sunset
        sequence:
          - service: script.all_on
            metadata: {}
            data: {}
mode: single