Timestamp gymnastics assistance

For those a bit more code savvy:

I’m trying to use a REST sensor (calling to an API) to gather data on lake temperatures. The GET request takes a UNIX timestamp, but the data is quantized to the hour – IE: if I send a timestamp for today at 12:00, it returns valid results, but any timestamp not on the hour returns an error.

I can’t figure out how to take time data provided by now() and strip it back to the nearest hour (or otherwise arbitrarily manipulate it, ultimately bouncing it back into UNIX timestamp format for sending to the API.

Anybody have any input?
Thanks!
Justin

First failed attempt:
Thought I could get away with dividing the now() timestamp by 3600 (getting the number of hours) and then round away the decimal. HA doesn’t seem to want to return an integer despite my best efforts.

{{ now().strftime('%H:00') }}
1 Like

Or if you need the full timestamp:

"{{ now().replace(minute=0, second=0) }}"
1 Like

Full utc timestamp

{{ as_timestamp(now().replace(minute=0, second=0, microsecond=0)) | timestamp_utc }}
2 Likes

Oh damn. Microseconds too.

1 Like

and proper utc :wink:

EDIT assuming he needs utc when he says:

2 Likes

Also possible with

{{ utcnow().replace(minute=0, second=0, microsecond=0) }}
1 Like

The convoluted version:

{{ as_timestamp(as_datetime(now().strftime('%Y-%m-%dT%H:00')))|int }}

:smiley:

1 Like

Y’all are amazing. Got it licked and learned a bit in the process. Thanks to all!

Be careful with your spelling :slight_smile: