Home Assistant Date Templates

Generate Jinja2 templates for dates and times in Home Assistant. Pick an operation — get copy-ready templates with YAML context.

 

How to Use This Tool

Select the context where you'll use the template — a template sensor (in configuration.yaml), an automation condition/action, or a Lovelace card / script. Then pick an operation. The tool generates a Jinja2 template with the correct syntax for that context, plus the YAML wrapper showing where to paste it.

Home Assistant's template engine is powered by Jinja2 with custom extensions. Templates go inside double curly braces {{ ... }} for values or {% ... %} for logic. All Python strftime format codes work in timestamp_custom and .strftime().

Jinja Filter Reference

Filter / FunctionInputOutputExample
as_timestamp()datetime or stringUnix epoch (float)as_timestamp(now())
as_datetime()epoch or ISO stringdatetime objectas_datetime(1774533600)
timestamp_custom()epoch (via pipe)formatted string... | timestamp_custom('%H:%M')
relative_time()datetime"5 minutes" / "2 hours"relative_time(state.last_changed)
now()current local datetimenow().hour
utcnow()current UTC datetimeutcnow().isoformat()
today_at()time stringtoday at that timetoday_at('14:30')
strptime()string + formatdatetime objectstrptime('2026-03-26', '%Y-%m-%d')
timedelta()keyword argstimedelta objecttimedelta(hours=2, minutes=30)
.strftime()datetime methodformatted stringnow().strftime('%H:%M')
.timestamp()datetime methodepoch floatnow().timestamp()
.date()datetime methoddate onlynow().date()
.total_seconds()timedelta methodfloat secondsdelta.total_seconds()

timestamp_custom Format Codes

Same as Python's strftime — use with timestamp_custom() or .strftime().

CodeMeaningExample
%Y4-digit year2026
%mMonth (01–12)03
%dDay (01–31)26
%HHour, 24h (00–23)14
%IHour, 12h (01–12)02
%MMinute (00–59)30
%SSecond (00–59)45
%pAM / PMPM
%AFull weekdayThursday
%aShort weekdayThu
%BFull month nameMarch
%bShort month nameMar

Entity State Attributes

AttributeTypeDescription
statestringCurrent state value ("on", "off", "23.5", etc.)
last_changeddatetimeWhen the state last changed value
last_updateddatetimeWhen the state was last written (even if same value)
attributesdictEntity-specific attributes (friendly_name, etc.)

Access via states.domain.entity or states('domain.entity'). The .last_changed and .last_updated attributes are UTC datetime objects — convert to local with as_local() if needed.

Common Automation Patterns

Time-based automation triggers

Time-based conditions

Notification with time info

Frequently Asked Questions

How do I get the current time in Home Assistant templates?

{{ now() }} for the full local datetime. {{ now().strftime('%H:%M') }} for formatted time. {{ now().hour }} for just the hour as an integer. {{ utcnow() }} for UTC. {{ today_at('14:30') }} for today at a specific time.

How do I show time since last motion?

{{ relative_time(states.binary_sensor.motion.last_changed) }} gives "5 minutes" or "2 hours". For minutes as a number: {{ ((now() - states.binary_sensor.motion.last_changed).total_seconds() / 60) | round(0) }}.

How do I create a countdown to an event?

Template sensor: {{ (strptime('2026-12-25', '%Y-%m-%d').date() - now().date()).days }}. This gives the number of days until Christmas. Wrap it in a template sensor in configuration.yaml for a dashboard display.

How do I trigger at sunset plus 30 minutes?

Sun trigger with offset: trigger: - platform: sun, event: sunset, offset: '+00:30:00'. Offset format is HH:MM:SS. Use negative for before: '-01:00:00' triggers 1 hour before sunset.

What is the difference between as_timestamp and as_datetime?

as_timestamp() converts a datetime to an epoch float. as_datetime() converts an epoch or string to a datetime object. They're inverses. Use as_timestamp for math, as_datetime when you need .strftime() or .date().

Why do I need as_local() for last_changed?

last_changed and last_updated are stored in UTC. If your timezone is not UTC, wrap with as_local() before formatting: {{ as_local(states.sensor.temp.last_changed).strftime('%H:%M') }}. Without as_local(), the time shown may be hours off.

Related Tools