Sun next rising issue

Hi, I have a weird problem. My Sun next rising is at 07:09 according to the template.

I created a switch for sunrise to use in automations. However this is not working since it is flipped 2 hours too early. When I checked code

{{(states.sun.sun.attributes.next_rising|as_datetime|relative_time).strftime(“%H:%M”)}} the result is 05:09. Checked my location settings, they are correct, also system time is correct? Any idea what could be wrong?

i think it’s giving you utc time. add as_local

{{(states.sun.sun.attributes.next_rising | as_datetime | as_local).strftime('%H:%M')}} 

you had relative_time in your template. what’s the purpose of that? relative_time is only used for past time to know the delta between a past time and current. next sun rising is future time so relative_time doesn’t do anything.

1 Like

That I dont know. I’m not a programming genius. I had to google how I could convert an attribute date to just the time and this was the solution I found. At least yours works and I updated my trigger. now it works. Thanks you so much

1 Like

I hope I can ask you another question. I’m trying to set a number variable input_number.time_delta_offset with a random value at midnight. I tested {{ range(1,10) | random | int }} is generating a random value when I refresh but I can’t get it in the variable.

i’m not sure what you’re asking. how to set the value of that helper? if so use this format:

service: input_number.set_value
data:
  value: >
    {{ range(1,10) | random | int }}
target:
  entity_id: input_number.test_number

oh, you wanted to do it at midnight… so something like this?

description: ""
mode: single
trigger:
  - platform: time
    at: "00:00:00"
condition: []
action:
  - service: input_number.set_value
    data:
      value: |
        {{ range(1,10) | random | int }}
    target:
      entity_id: input_number.test_number

Wow. It works thanks. The syntax is difficult with no manual available. I had exactly that and it didn’t work. I didn’t use the | in my code at the line value. Is that that the reason why it didn’t work ?

did you have that verbatim except the | ?

the value: needs something to instruct it whether to take your input as literal input or to try to interpret it.

in this particular case you could do:

      value: "{{ range(1,10) | random | int }}"

that’s how you tell it to treat it as a template but do it all on 1 line.

or you can do what i did above or also:

      value: >
        {{ range(1,10) | random | int }}

this means to treat the multiple subsequent lines as a template to be interpreted. most people seem to do this only when they have a long multi-line template. but i prefer to do it pretty much for all templates. i don’t like the above " " method.

the difference between > and | is only if you have a multi-line output… so more important for text things like notification messages.

   message: >
      this is a random number:
      {{ range(1,10) | random | int }}

may yield something like:

this is a random number:  5

whereas using a | would yield something like:

this is a random number:  
5
1 Like