Template Collection (CookBook)

Hi,
When I started my HomeAssistant Journey the word “template(s)” was interresting and mysterious for me and only later I found out the power of it.

I know that there is plenty of documentation about templates in the Official HomeAssistant Docs, but I wanted to share user-created templates for anybody else to use which are “easy to copy”.

I would like to help anybody who struggles to solve an automation problem with this “collection” of Templates used by you, the users of HomeAssistant. Maybe someone also gets different ideas for new automations when looking through this topic. I understand the frustration of newbies (I was also one) where they searched something what could be solved easily by templates.

So, please feel free to share your various templates for others!

I will start with a few of mine: (I will for sure add more later!)

Random Spotify Playlist:
I have an automation which starts playing music randomly in the morning through spotcast, for this I wanted to randomly select a playlist. Here is the (shortened) template I use in SpotCast automation:

{{ ( ( "lofi playlist", "good morning playlist", "positive vibes playlist" ) | random ) }}

Random Delay: (useful in Scripts/Automations)

delay:
 seconds: '{{ range(8, 11)|random|int }}'

True/False if entity is in a state: (the following example returns “true” if door is open)

{{ is_state("sensor.living_room_door", "on") }}

Naming of Files: (hass-version, date/time; useful for backup automations, or camera snapshots)
the following code returns something like: “backup_7.6.2022_ver_2022.6.2”

backup_{{now().strftime('%d.%m.%y')}}_ver_{{states('sensor.installed_version')}}

the following code returns: HH:MM (12:47)

{{ as_timestamp (now()) | timestamp_custom('%H:%M') }}

Replace/Remove values from Sensor: (useful to create a new sensor with different readings)
The following code removes the “€” sign from the sensor result and replaces “,” (comma) with “.” (dot)

{{ states.sensor.petrol_price.state |replace("€", "")|replace(",", ".") | float }}

Entity exists check: (returns true/false if an entity exists or not (put “is not defined” to turn around the true/false))

{{ states.image_processing.qrcode_cam.state is defined }}
2 Likes

Limit a template value to a range with |sort: Limit a value to fall within a range

It’s less convoluted than using both |min and |max.

I have something new I want to add:
I wanted to check if all doors and locks are closed prior arming the alarm, also with a TTS message if any doors are open.

I have a group of the doors+locks I want to check, called group.door_lock

{{ expand('group.door_lock') | selectattr('state', 'eq', 'on')| map(attribute='name') | list | join(' and ')  }}

The above templates returns the Friendly Names of all entities in the state “on” from the group door_lock, if there are more, they are joined with “and” which can be used directly in TTS messages.

If you want to count, for whatever reason your entities with a specific state in a group, it is that easy:

{{ expand('group.door_lock') | selectattr('state', 'eq', 'on') | list | count }}

In spoken English, it’s less common to say this:

The front and rear and garage and patio doors are open.

It’s more common to say:

The front, rear, garage and patio doors are open.

The following topic explains how to do that:

Thanks for the Input! Even better, in my language it is actually the same, it “sounds” better, but… let´s say it differently, I was too lazy to check other use-cases, and the “and” was enough for me (for now)…
I will use your input, thanks again!