Datetime comparison issues

I am comparing two dates formatted as date and time. One is
{{(as_timestamp(now()) | timestamp_custom("%A, %d %h %H:%M")) }}
which renders Wednesday, 27 Mar 11:27

and
{{(as_timestamp(now())+ states.sensor.time_delta_front_shrubs.state | int) | timestamp_custom("%A, %d %h %H:%M") }}
which renders Friday, 29 Mar 11:27

if I compare them then apparently Wednesday is greater than Friday.

`{{(as_timestamp(now()) | timestamp_custom("%A, %d %h %H:%M"))  > (as_timestamp(now())+ states.sensor.time_delta_front_shrubs.state | int) |   timestamp_custom("%A, %d %h %H:%M") }}`

renders True.

In fact if I try and compare
{{(as_timestamp(now()) | timestamp_custom("%A, %d %h %H:%M")) > "Wednesday, 27 Mar 12:20"}}

I get False, but trying any other time that is a day or more in the future I get True.

What am I missing?

You are testing strings, not integers. As soon as you format the timestamps they becomes strings.

Try performing the test with unformatted timestamps.

I thought about that, but I came across a complication that I’m yet to resolve.

As I’m sometimes setting the next run time manually, via input_sliders, that is then a payload to MQTT, such as
{{(as_timestamp(now() ) ) | timestamp_custom("%A, %d %h ")}}{{states.sensor.front_shrubs_timer.state}} where {{states.sensor.front_shrubs_timer.state}} is clock time from input_value, I’d need to have some method of actually calculating what the datetime float is. Or calculating back from the Date string formatted as above its float.

Yeah input datetimes without dates are tricky to compare to timestamps (that include the date). You have to add today’s date to them. Here’s a value template for a binary sensor I use to check if the current time is within a window specified by two input datetimes that may point you in the right direction:

      value_template: >-
        {% set d = now().strftime("%Y-%m-%d ") %} # date now only
        {% set t = now().timestamp() %} # time now, with date timestamp
        {% set am_start = strptime(d + states('input_datetime.dining_am_on_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {% set am_end = strptime(d + states('input_datetime.dining_am_off_time'), '%Y-%m-%d %H:%M:%S').timestamp() %}
        {{ am_start <= t <= am_end }}
1 Like

This has been a long winded process requiring refactorring of the whole irrigation timer component. It was brought about by wanting to test future and past dates. Up until then I was only testing equality, which with strings worked fine.

@tom_l thanks for your help. It was actually your input_datetime.dining_am_on_time component that made me twig why I can’t cast value_inputs into time and date very easily (or not at all), epecially when they are concatenated.

Also these two resources helped The EPIC Time Conversion and Manipulation Thread! and https://www.home-assistant.io/components/input_datetime/

Whilst I am not very keen on the appearance of the value_datetime component (the time input is ugly and not input friendly), arithmetic operations are much simpler now.