jchh
((not John))
June 20, 2023, 8:50am
1
Hi,
I have searched and there are similar questions to mine (eg: here ) but mine is a little different.
I have managed to get a time off a website in the format HH:MM
(eg: 09:30
) which I know is UTC. How can I convert that to local time?
I’ve tried using timestamp_local
, but get invalid input
.
What is the correct format for a time-only input for timestamp_local
(is there one)?
How can I get it from HH:MM
?
Olivier1974
(Olivier Toussaint)
June 20, 2023, 9:52am
2
Have a look at this library, created by our well known community member @petro
I’m pretty sure that you’ll find what you need, and if not, you can ask him for an update
1 Like
jchh
((not John))
June 20, 2023, 9:55am
3
much appreciated! I will take a look later this morning
RogTP
(Roger Selwyn)
June 20, 2023, 12:45pm
4
A time in a format such as HH:MM isn’t a valid UTC format for converting. You will see an example on this page - Date and Time Formats
1 Like
jchh
((not John))
June 20, 2023, 1:00pm
5
Thanks.
That probably explains why hh:mmZ
, Thh:mmZ
, hh:mm+01:00
or Thh:mm+01:00
didn’t work when I tried them (should have mentioned that in my opening post).
123
(Taras)
June 20, 2023, 1:23pm
6
In the following template, replace sensor.website_utc
with the entity_id of your entity that reports UTC time in HH:MM
format.
{{ '{} {}+00:00'.format(now().date(), states('sensor.website_utc'))
| as_datetime | as_timestamp | timestamp_custom('%H:%M') }}
To test the template, copy-paste it into the Template Editor.
Here’s another way to do the same thing:
{% set (h,m) = states('sensor.website_utc').split(':') | map('int', 0) %}
{{ utcnow().replace(hour=h, minute=m, second=0)
| as_timestamp | timestamp_custom('%H:%M') }}
1 Like
jchh
((not John))
June 20, 2023, 1:38pm
7
wow - that’s awesome - thanks!
as_local
and '{} {}+00:00'.format(now().date()
are both new to me. Do you know where I can find out more about them (for my own education)?
edit:
OK, got it - played with it in templates and worked out what they call mean - fantastic stuff.
petro
(Petro)
June 20, 2023, 1:42pm
8
there’s about 800 ways to do this.
{{ ((now().date() ~ ' ' ~ states('sensor.website_utc') ~ "+00:00") | as_datetime | as_local).strftime('%H:%M') }}
1 Like
123
(Taras)
June 20, 2023, 1:43pm
9
format
is a method of strings (in python).
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
as_local
is a Jinja2 filter described in the documentation here . However, I removed it from the template example because it wasn’t needed.
1 Like
jchh
((not John))
June 20, 2023, 1:45pm
10
Thanks!
Do the ~
mean concatenate? How is it different from ‘+’?
petro
(Petro)
June 20, 2023, 1:46pm
11
yes, it ensures everything that’s “added” is a string. + does not do that
1 Like
jchh
((not John))
June 20, 2023, 1:47pm
12
fantastic - thanks for the pointer and edit
jchh
((not John))
June 20, 2023, 1:49pm
13
I think I need to learn Python - have always intended to, but never got round to it …until HA
petro
(Petro)
June 20, 2023, 1:49pm
14
That’s not python, that’s jinja. Python does not have the ~ operator
jchh
((not John))
June 20, 2023, 1:50pm
15
Got it - thanks!
(I always thought jinja was a part of Python)
petro
(Petro)
June 20, 2023, 1:55pm
16
Jinja is based on python. The ~ operator from python is not usable in jinja because jinja uses it for concatenating strings. The ~ operator in python is seldom used. I don’t even think its covered in the python docs but it’s mentioned.
you can read up on the python version here
python, operators
1 Like
jchh
((not John))
June 20, 2023, 1:56pm
17
I really appreciate all the help from everyone. Lots to research and learn now