This is a part of a larger automation. The rest of this automation works fine, but those lines does not seem to execute at all. It’s something with how I do the time comparison.
I have tested the condition in the Template validator in the frontend and I got the result I expected.
How do I get this part of the automation to work, i.e. to turn the two lights on if the time is before 21:30?
False turns your timestamp into local time. True (or omitted) turns it into UTC. You’ll want False. But to be quite honest, that conversion is doing nothing because ‘21:00’ is not a timestamp.
For safety, you really should be converting the time and your other time to a single mathematical unit and compare them that way as @tom_l said. Relying on string comparisons is not the way to go because it doesn’t work the way you think it does. I.E '9' is not less than '10' in the string world:
For me, and for future readers, whats the best way to convert that timestamp to UNIX-time or something similar in order to be able to do that comparison?
not sure how that is working because now().strftime(’%T’) is a string where strptime(‘21:30:00’, ‘%T’) is a datetime object. They are not comparable. You may have problems with this in other ways and you shouldn’t mark it as a solution as it will steer other people in the wrong direction. In fact, the strptime isn’t actually converting it to a datetime, it’s keeping it a string. In the string world, 9 is greater than 10.
@petro I removed the “Mark as solved” on this issue. Np.
Well, you are right in all the ways I can think of, but it does work. I have been running this for a few days now just in order to be sure it does what I want, and it sure does.
no harm in leaving the thread ‘unsolved’, i’m leery of having bad time information out there because it’s the hardest templating thing in HA to deal with.
While this is indeed true, it doesn’t apply here because you won’t be comparing 9 and 10 but 09 and 10.
One of the great things about ISO 8601 times (and dates and datetimes) is that they are lexicographically comparable because they are ordered from largest to smallest unit of time and has leading zeroes.
You can see how and why this works with this short example:
In [1]: import datetime
In [2]: datetime.datetime(2020, 3, 18, 9, 00, 00).strftime("%T")
Out[2]: '09:00:00'
In [3]: datetime.datetime(2020, 3, 18, 9, 00, 00).strftime("%T") < "10:00:00"
Out[3]: True
So {% if now().strftime('%T') < '21:30:00' %} will work without issues.
Sure, but you can compare what you get from strftime("%T") with a string as long as you include the leading zeroes. You seem to imply that this shouldn’t be done since you disqualify string comparisons with your example of comparing 9 and 10.
He was very close to the actual solution, which looks like:
The best solution to fit his needs would have just been a string comparision of %H:%M if he’s not using seconds, and place that in a separate conditional check prior to the service call. But you don’t even need that because he can use the normal, non template condition which many people understand better.
For future reference, if you use variables within your template, it becomes easier to read and maintain. For example:
value_template: >-
{% set power = states('sensor.power') | float %}
{% set pinv = states('sensor.pinverter') | float %}
{% set pgrid = states('sensor.pgrid') | float %}
{% set t = now().hour %}
{% if power > 50 and pgrid > -50 and 7 <= t < 18 %} 1
{% elif pinv > 50 and pgrid < -50 and 7 <= t < 18 %} 2
{% elif power < 50 and pgrid < -50 and 7 <= t < 18 %} 3
{% elif power > 50 and pgrid < -50 and 7 <= t < 18 %} 4
{% elif power < 50 and pinv > 50 and pgrid < -50 and 7 <= t < 18 %} 5
{% elif power < 50 and pinv > 50 and pgrid > -50 and 7 <= t < 18 %} 6
{% elif power < 50 and pinv < 50 and pgrid < -50 and 18 <= t < 22 %} 7
{% elif power < 50 and pinv > 50 and pgrid > -50 and 18 <= t < 22 %} 8
{% elif power < 50 and pinv > 50 and pgrid > -50 and t >= 22 %} 9
{% elif power < 50 and pinv > 50 and pgrid > -50 and t < 7 %} 9
{% elif power < 50 and pinv < 50 and pgrid < -50 and t >= 22 %} 10
{% elif power < 50 and pinv < 50 and pgrid < -50 and t < 7 %} 10
{% elif power < 50 and pinv > 50 and pgrid < -50 and t >= 22 %} 11
{% elif power < 50 and pinv > 50 and pgrid < -50 and t < 7 %} 11
{% else %} NONE
{% endif %}