This page is a cheat sheet for working with datetime objects in Home Assistant.
Copy & paste the code blocks below into the template editor window in Home Assistant. This is located at: your_HA_URL:8123/developer-tools/template
to see how the jinja syntax is evaluated in Home Assistant. Reminder, this syntax is everything inclusive of and in between the mustache braces (see code block below).
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
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
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
High Value Resources:
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
- {{ as_timestamp(state_attr('automation.bwm_flur', 'last_triggered')) | timestamp_custom('%Y.%m.%dT%H:%M:%S') }}
- {{ state_attr('automation.bwm_flur', 'last_triggered') }}