Round an input_time to 5 or 0

Probably this is a really simpel one, but i cannot get it working.

I want a input_datetime to have a 30 minutes added to the current time, but want to round it to the next 5 or 0 minutes.

The template I have now:

{{ (now().strftime("%s") | int +(30*60)) | timestamp_custom("%H:%M") }}

This gives me a brilliant 30 minutes after now.
For example it is now 9:36, adding 30 minutes gives 10:01, but I want the result to be 10:00 or 10:05…

What to add to the template??

Try this in the template editor:

{% set x = 33 %}
{{ x - ( x % 5 ) }}

x % y returns the remainder of x/y

Not sure how to work that into your template. Maybe by converting the time to a timestamp (in seconds) doing the math as timestamp % 300 (since 300 seconds = 5 minutes) then converting back to a time.

I came up with this template:

{{ (now().strftime("%s") | int +(30*60) - ((now().strftime("%s")) | int ) % 300 ) | timestamp_custom("%H:%M:%S") }}
1 Like