Round timestamp to the nearest second

I’ve been trying to find a solution to this with no avail. I need to remove the decimal from the seconds in this string:
2023-01-04 19:04:30.997145
Basically I need this value template to work and it seems the decimal throws it off:


{% if (now().today() - strptime(value, '%Y-%m-%d %H:%M:%S')) < timedelta( 
      seconds = 3 ) %}
        on
      {% else %}
        off
      {% endif %}

{% if (now().today() - strptime(value.split(".")[0], '%Y-%m-%d %H:%M:%S')) < timedelta( 
      seconds = 3 ) %}
        on
      {% else %}
        off
      {% endif %}

The format (the stuff starting '%Y-') has to match the string format you have given, and %S doesn’t include stuff after the decimal point. This however seems to work:

{{strptime('2023-01-04 19:04:30.997145', '%Y-%m-%d %H:%M:%S.%f')}}

Note the .%f

see datetime — Basic date and time types — Python 3.8.14 documentation

This appears to work. At least in the template editor. That’s a ton for the quick reply!!