Example of the new template feature + plus question support of conditions

I’m trying to understand the new template feature.
It seems very powerfull (as the read complexity of the documentation also implies).
For those looking for an example on the new template feature, this is a sensor that shows the last item played by one of the listed media players.

As you can see the new templates are close to what automations provide (templates can have actions as well).
What a template doesn’t seem to support are conditions.
In this example a condition might be the type of source selected and depending on that type decide whether or not to update the sensor values.
Alternatively, I read something about self referencing support (i.e. a variable this referencing to the current state of the sensor).
I guess that could also be a way to preserve the old state when conditions aren’t met?

#configuration.yaml	
template: !include templates.yaml
#templates.yaml
- trigger:
  - platform: state
	entity_id: 
	  - media_player.woonkamer
	  - media_player.badkamer
	  - media_player.slaapkamer
	  - media_player.werkkamer
	  - media_player.opbouw
	to: "playing"
  unique_id: bc528955-ebb4-42d4-a9d3-6fdfdd147807
  sensor:
    - name: "DJ MiCasa"
	  unique_id: "dj_micasa"
	  state: >
	    {{ trigger.to_state.state }}
	  attributes:
	    media_channel: "{{ trigger.to_state.attributes.media_channel }}"
	    media_content_id: "{{ trigger.to_state.attributes.media_content_id }}"
	    media_content_type: "{{ trigger.to_state.attributes.media_content_type }}"
	    entity_picture:  "{{ trigger.to_state.attributes.entity_picture }}"

What is “new” about the “new template feature” you are demonstrating?

Your example is a Trigger-based Template Sensor which is a feature created nearly 3 years ago (2021.4.0).

@123 I guess I misinterpreted the many blogs and posts stating the new way was introduced around 2023.
I had some trouble understanding without a complete example and thus figured my learnings would help others.
No pun intended!

Can you share links to a few of these “many blogs and posts” from 2023? I’m curious to see what they say exactly.

I’ve been al over the internet for this :sweat_smile:
What I could condensate from my browser history:

https://www.reddit.com/r/homeassistant/comments/pysnxa/how_do_template_triggers_work/?rdt=36584
https://www.reddit.com/r/homeassistant/comments/pysnxa/how_do_template_triggers_work/

  • Your very first link leads to a topic created in 2021.

  • The second link leads to a topic created this year but the user is still using the legacy method of defining a Template Sensor which was deprecated years ago.

  • The third link leads to one of your own posts created this year where you report your inability to understand the documentation for creating a Template Sensor.

  • The fourth link leads to a topic created nearly 2 years ago.

  • The links to YouTube tutorials explain features that have existed in Home Assistant well before the tutorials were created.

I stopped reviewing the remaining links because it’s clear that nothing posted recently can legitimately claim a Trigger-based Template Sensor is a new feature. It was introduced years ago, shortly after a new configuration style was adopted for Template entities.

Perhaps the only thing new about it is that you recently discovered it and so it’s new for you.

1 Like

@123 you’re right I’m new and was struggling.
I’m happy I figured it out and going to stay happy (not addressing hospitality) closing this thread.