Time Manipulation
This page is a cheat sheet for working with datetime objects in Home Assistant.
The code blocks below can be copied and pasted directly into the template editor in Home Assistant (your_HA_URL:8123/developer-tools/template
) to see how the jinja syntax is evaluated. In the block below, only the content between the mustache braces {}, including the braces, are jinja.
Basic Date & Time:
1) {{ now() }} => 2024-07-02 11:24:17.517844-07:00
2) {{ utcnow() }} => 2024-07-02 18:24:17.517844+00:00
3) {{ today_at() }} => 2024-07-02 00:00:00-07:00
4) {{ as_timestamp( now() ) }} => 1719944760.209791
- Returns the current local datetime value (note “-07:00” at end).
- Returns current UTC datetime value (note “+00:00” at end).
- Returns today’s “Zero hour” (i.e. when the date & time turn at midnight) - useful in triggers & calculations.
- Returns the current local time and date in UNIX Time (in seconds) - useful when a time difference needs to be calculated.
-
BEWARE!!
1) {{ now() }} => 2024-07-02 11:24:17.517844-07:00 2) {{states.sun.sun.last_changed}} => 2024-07-02 12:50:42.842225+00:00 3) {{ as_datetime(states.sun.sun.last_changed) | as_local }} => 2024-07-02 05:50:42.842225-07:00 BUT: 4) {{ as_timestamp(as_datetime(states.sun.sun.last_changed) | as_local) }} => 1719944760.209791 5) {{ as_timestamp(states.sun.sun.last_changed) }} => 1719944760.209791
- Local datetime value as per first example
- But this datetime value is actually in UTC (perhaps not what you want, unless you are actually in UTC)
- Use this instead to get the correct local datetime of when a device last changed (in this example, 5:50 was sunrise so the timestamp makes sense now)
BUT: When doing time comparisons between two times, and a time difference needs to be determined, it is often easier to convert them to UNIX time and then subtract the two to find the difference. Here, 4 & 5 will convert to the same UNIX time as they both represent the SAME moment in time.
Date & Time Retrieval
Show month & day: {{ as_timestamp(states.sun.sun.last_changed) | timestamp_custom(' %B %d') | replace(" 0", "") }} => July 2
OR month & day with an Ordinal label {{now().strftime('%B %-d') | ordinal}} => July 2nd
Show time of day: {{ as_timestamp(states.sun.sun.last_changed) | timestamp_custom(' %I:%M %p') | replace(" 0", "") }} => 5:50 AM
Show days of week, month, and time {{ as_timestamp(states.sun.sun.last_changed) | timestamp_custom('%A %B %-d, %-I:%M %p') }} => Tuesday July 2, 5:50 AM
Show hour & minute ("00:00") format: {{ now().strftime("%-H:%M") }} => 9:54
Show year only: {{now().strftime('%Y')}} => 2024
Time Now: {{as_timestamp(now())|timestamp_custom('%X')}} => 12:22:26
Date Now: {{as_timestamp(now())|timestamp_custom('%x')}} => 07/02/24
Tomorrow's date: {{ as_timestamp(now() + timedelta(days=1)) | timestamp_custom('%a %-d %b') }} => Wed 3 Jul
Compare time as True/False: {{now() | as_datetime > today_at("07:30")}} => True
Good Resources:
- https://www.home-assistant.io/docs/configuration/templating/#time - worth scanning to see other options
- https://community.home-assistant.io/t/the-epic-time-conversion-and-manipulation-thread/85786/3 - long thread, lots of examples
- https://www.reddit.com/r/homeassistant/comments/1b7ntve/date_time_a_guide_i_made_for_formatting/
- https://brianhanifin.com/posts/home-assistant-date-time-template-macros/ - Oddly similar site theme to mine
- https://docs.python.org/3/library/datetime.html#datetime-objects - more info on datetime objects
More Code Snippets
Countdown to month end (remaining days):
{% set this = now() %}
{% set next = this.month + 1 if this.month + 1 <= 12 else 1 %}
{% set last = this.replace(month=next, day=1) %}
{{(last.date() - this.date()).days}}
Find the days in the month:
{% set start = now().replace(day=1) %}
{% set month = start.month + 1 if start.month < 12 else 1 %}
{% set year = start.year + 1 if start.month == 12 else start.year %}
{% set end = start.replace(year=year, month=month) %}
{{ (end-start).days }}
Create an sensor to calculate the hours of sunlight per day (note, this is in the legacy format):
- platform: template
sensors:
daylight_hours_today:
friendly_name: 'Daylight hours today'
value_template: >
{% set sunrise = as_timestamp(state_attr('sun.sun','next_rising')) %}
{% set sunset = as_timestamp(state_attr('sun.sun','next_setting')) %}
{% set midnight = as_timestamp(state_attr('sun.sun','next_midnight')) %}
{% if midnight < sunset %}
{% set sunrise = (sunrise - 86328.5) %}
{% set sunset = (sunset - 86471.5) %}
{% endif %}
{% if sunrise > sunset %}
{% set sunrise = (sunrise - 86328.5) %}
{% endif %}
{{ (sunset - sunrise)|timestamp_custom('%H:%M',false) }}
icon_template: mdi:weather-sunny
Example Dump (Unorganized)
- {{ as_timestamp(state_attr('automation.bwm_flur', 'last_triggered')) | timestamp_custom('%Y.%m.%dT%H:%M:%S') }}
- {{ state_attr('automation.bwm_flur', 'last_triggered') }}