{% set d1 = strptime('07:32', '%H:%M') %}
{% set d2 =strptime('30', '%M') %}
{% set t1 = d1.hour*3600 + d1.minute*60 %}
{% set t2 = d2.hour*3600 + d2.minute*60 %}
{{ (t1+t2) | timestamp_custom('%H:%M:%S') }}
Simply put, strptime('07:32', '%H:%M') does not create a proper datetime object, it just stores what you give to in into appropriate fields but things like .timestamp() wonât work as itâs incomplete.
if you manually create a âtimestampâ you can then feed it to timestamp_xxx for conversion.
Have a look.
I needed to add a âlocal_booleanâ⌠(timestamp_custom(format_string, local_boolean)
Something to do with timezones I think but to be honest I am not sure. Iâm on GMT here which is currently UTC so why it needed a flag set I donât know? Will it work in DST? And what is it adjusting for when the flag is true?
And feel free to tear apart any part of this. I really am growing to dislike working with dates and times in Python.
#=== This is included only for completeness. It simply sums some input
#=== numbers to give a total in minutes which I then convert to HH:MM
{% set ns = namespace(duration = 0) %}
{% for zone in states.input_number if zone.entity_id.startswith('input_number.irrigation_cycle') and
zone.entity_id.endswith('duration') %}
{% set ns.duration = ns.duration + zone.state | float %}
{% endfor %}
{% set hrs = ((ns.duration * 60) // 3660) | int %}
{% set min = (((ns.duration * 60) // 60) | int % 60) | round %}
#============================================
{% set duration = '{:02}:{:02}'.format(hrs, min) %}
Duration - {{ duration }}
{% set start = states('input_select.irrigation_cycle1_start_time') %}
Start - {{ start }}
{% set d1 = strptime(start, '%H:%M') %}
{% set d2 =strptime(duration, '%H:%M') %}
{% set t1 = d1.hour*3600 + d1.minute*60 %}
{% set t2 = d2.hour*3600 + d2.minute*60 %}
{{ (t1+t2) | timestamp_custom('%H:%M') }}
{{ (t1+t2) | timestamp_custom('%H:%M', false) }}
Sure, but I think it should be True/False - try it out with False.
Iâm on GMT, too. Think it should work in DST.
Have no idea what it adjusts. Also fed up a bit with all wrappers etc as we usually donât deal with normal python and itâs pita to find out why on earth it works that way. Oh, wellâŚ
I think it might have to do with what the function of the + sign is.
the + sign can be either an mathematical operator when using it with numerical data types or it can be a concatenator if used with strings but since a datetime object (even an incomplete one like here) is neither a number or a string then I think the renderer canât figure out what you want to do with it.
The - sign doesnât come with that baggage. At least thatâs what I think in my very un-expert opinion.
+
Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2 .
~
Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is set to âJohnâ) Hello John!.
My understanding is + and - are functions and their result depends on the data you pass to it and some implicit type conversions that might happen. And the type error it generates speaks for it I think.
Some data types may even have â+â but not â-â (if it does not make sense) and vice versa.
âIn the land of the blind, the one-eyed is kingâ
@123, @AhmadK
Maybe this isnât the right place but Iâve often come across the term âPythonicâ to describe the Python language structure wrt deciding how to implement it. As I understand it this describes the principle that Python should be readable to non Python programmers.
This ambiguous use of the â+â operator perturbs me especially in light of the single use of itâs opposite and natural âcompanionâ, â-â and it just feels, wrong. As a Python novice, I just wonder how you two feel about this?
Itâs purely an intellectual question, nothing moreâŚ
I haveât finished my first book on Python yet so canât tell you what exactly âPythonicâ is (perhaps each person has its own understanding when they use this term) but I slightly doubt that it means âreadable to non Python programmersâ.
Yeah, I had a quick look-up and you are probably right that is not accurate, although I have definitely seen that said or at least implied, in the past.
I found this:
"Exploiting the features of the Python language to produce code that is clear, concise and maintainable.
Pythonic means code that doesnât just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used."
Compare the example I posted here for finding the last item in a list, using pythonâs [-1], versus the previous post in the same thread that employs Jinja2âs length filter. Basically, âpythonicâ means doing it the âpython wayâ and taking advantage of its strengths.
Quick question - if I have several sensors I want to format in a particular way, do I have to create the same template sensor each time with a different entity, or is there a way to create a âtemplateâ template sensor which can be reused by just passing the entity each time? Kinda like the way we can template Lovelace cards, but for the sensor itself, if you see what I mean?
Thanks to @finity for this thread, and also to @pedro who also added some very useful info on this Date formatting thread, both have been very helpful and enlightening, so thank you both!
Aaah! Interesting (Iâd heard of the yaml anchors, but never looked into them further so didnt realise I could use them in the sensors (rather than in the lovelace side of things). Thank you @123, that should help somewhat
Just wanted to thank you again for this thread @finity.
Every time I get stuck on something time/date related the answer is always right here! I bet you if you were to organize it all formal like and submit it, it could become part of the official docs.