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?
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.
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
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.
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 ?
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 }}