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 }}