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 / Function | Input | Output | Example |
|---|---|---|---|
| as_timestamp() | datetime or string | Unix epoch (float) | as_timestamp(now()) |
| as_datetime() | epoch or ISO string | datetime object | as_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 datetime | now().hour |
| utcnow() | — | current UTC datetime | utcnow().isoformat() |
| today_at() | time string | today at that time | today_at('14:30') |
| strptime() | string + format | datetime object | strptime('2026-03-26', '%Y-%m-%d') |
| timedelta() | keyword args | timedelta object | timedelta(hours=2, minutes=30) |
| .strftime() | datetime method | formatted string | now().strftime('%H:%M') |
| .timestamp() | datetime method | epoch float | now().timestamp() |
| .date() | datetime method | date only | now().date() |
| .total_seconds() | timedelta method | float seconds | delta.total_seconds() |
timestamp_custom Format Codes
Same as Python's strftime — use with timestamp_custom() or .strftime().
| Code | Meaning | Example |
|---|---|---|
| %Y | 4-digit year | 2026 |
| %m | Month (01–12) | 03 |
| %d | Day (01–31) | 26 |
| %H | Hour, 24h (00–23) | 14 |
| %I | Hour, 12h (01–12) | 02 |
| %M | Minute (00–59) | 30 |
| %S | Second (00–59) | 45 |
| %p | AM / PM | PM |
| %A | Full weekday | Thursday |
| %a | Short weekday | Thu |
| %B | Full month name | March |
| %b | Short month name | Mar |
Entity State Attributes
| Attribute | Type | Description |
|---|---|---|
| state | string | Current state value ("on", "off", "23.5", etc.) |
| last_changed | datetime | When the state last changed value |
| last_updated | datetime | When the state was last written (even if same value) |
| attributes | dict | Entity-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.