brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: today_at() makes time-of-day checks in Home Assistant templates easy

Yaml
~/tips/tip-home-assistant-template-today-at

cat ~/tips/tip-home-assistant-template-today-at.md

today_at("18:00") returns today's date at that time, so you can compare it with now().

Comparing the clock with a fixed time of day in a template invites string tricks on now(). today_at gives you a real datetime for “today, at this hour”, and datetimes compare without any ceremony.

It takes an optional string in HH:MM or HH:MM:SS format and defaults to 00:00. Here are the examples from the docs:

{{ today_at("08:00") }}
{{ now() > today_at("18:00") }}
{{ today_at("18:00") <= now() < today_at("23:00") }}
{{ ((today_at("17:00") - now()).total_seconds() / 60) | int }}

The first prints something like 2024-03-15 08:00:00+01:00, a datetime with the time zone included. The second is a plain true or false. The third is an evening window in one line, which works well as a template condition. The last one counts the minutes until five o’clock, because subtracting two datetimes gives a timedelta.

The caveat is the word today. The result is always today’s date, so a window like “22:00 to 06:00” does not work as one chained comparison. It would need two parts joined with or. For a plain time window in an automation, the ready-made time condition is the simpler choice. Home Assistant 2021.12 is the version the docs name for this function.

Further reading: today_at in the Home Assistant template functions

More Yaml tips yamllint -f parsable config.yaml yamllint -d relaxed docker-compose.yml iif(condition, if_true, if_false, if_none) Sensor is available actions: All Yaml tips (10) All topics

Link