Adding trigger based template to a templates.yaml file?

I’m trying to add a trigger based template to an included templates.yaml file. In my configuration it looks like:

template: !include templates.yaml

and in templates.yaml I have:

binary_sensor:                                                           
  - name: 'Both Home'                                                                   
    unique_id: both_home            
...
sensor:
  - name: 'House Power Demand'
    unique_id: house_power_demand
    icon: mdi:transmission-tower
...

But when I add:

trigger:
  - trigger:
    - platform: mqtt
      topic: "birdnet"
    - platform: time
      at: "00:00:00"
      id: reset
    sensor: 
      - unique_id: new-sequence-12345678

I get an error:

Invalid config for 'template' at templates.yaml, line 217: required key 'platform' not provided

(line 217 is the -trigger line)

I’ve tried other various formations (including starting with -trigger), but no joy. I’m not sure where to go from here - how can I add trigger based templates to an included templates.yaml file?

Thanks for any clues.

trigger:   <----- remove this line
  - trigger: <----- keep this line
    - platform: mqtt
      topic: "birdnet"
    - platform: time
      at: "00:00:00"
      id: reset
    sensor: 
      - unique_id: new-sequence-12345678

From the documentation:

That doesn’t work, It seems like the documentation doesn’t translate for a separated templates file. I get the following:

2024-04-16 12:22:02.075 ERROR (MainThread) [homeassistant.config] Invalid config for 'template' at templates.yaml, line 216: 'trigger' is an invalid option for 'template', check: sensor->40->trigger
Invalid config for 'template' at templates.yaml, line 216: required key 'state' not provided
Invalid config for 'template' at templates.yaml, line 222: 'sensor' is an invalid option for 'template', check: sensor->40->sensor

the indenting looks like this:

binary_sensor:                                                           
  - name: 'Both Home'                                                                   
    unique_id: both_home            

sensor:
  - name: 'House Power Demand'
    unique_id: house_power_demand
    icon: mdi:transmission-tower

  - trigger:
    - platform: mqtt
      topic: "birdnet"
    - platform: time
      at: "00:00:00"
      id: reset
    sensor: 
...

if I adjust the indenting to the left, so that -trigger is “top level” in the file, I get:

2024-04-16 12:25:56.892 ERROR (MainThread) [homeassistant.components.template] while parsing a block mapping
  in "templates.yaml", line 1, column 1
expected <block end>, but found '-'
  in "templates.yaml", line 216, column 1

If someone has a working chunk of yaml for trigger based templates, in a separate file, I would love to see it. Thanks.

OK, I figured it out. Although this is acceptable, in isolation:

binary_sensor:                                                           
  - name: 'Both Home'                                                                   
    unique_id: both_home            

sensor:
  - name: 'House Power Demand'
    unique_id: house_power_demand
    icon: mdi:transmission-tower

I needed to reformat to:

- binary_sensor:                                                           
  - name: 'Both Home'                                                                   
    unique_id: both_home            

- sensor:
  - name: 'House Power Demand'
    unique_id: house_power_demand
    icon: mdi:transmission-tower

to allow adding:

- trigger:
  - platform: mqtt
    topic: "birdnet"
  - platform: time
    at: "00:00:00"
    id: reset
  sensor: 
...

Well that’s plainly wrong. Compare it to the screenshot I posted.

That’s exactly what I had suggested; once again, see the screenshot (above) sourced from the documentation. It’s the Solution.

Yes, for some reason I had it in my mind, that once moving to a separate file, you dropped the array ‘-’ part - I struggled with that when splitting my configuration files. Anyway - I’ve learned something new, and it’s all for the better.

Thanks for your help.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag, given that it demonstrated what was needed to correct the error you had reported.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

I’m trying to do similar. Although the code validates and saves OK, it doesn’t seem to work. The sensor section appears to work in Developer tools, it’s the trigger that doesn’t seem to evaluate the entity. Their states remain as ‘unknown’.

   - trigger:
     - platform: time
       at: "23:59:50"
     sensor:
       - name: "Greenhouse Heater Energy"
         unit_of_measurement: "kWh"
         device_class: energy
         state: > 
            {% set greenhouse_daily_energy = states('sensor.shellyheater_daily_energy') %}
       - name: "Daily Gas Consumption"
         unit_of_measurement: "m³"
         device_class: gas
         state: >
            {% set gas_daily_consumption = states('sensor.current_accumulative_consumption_m3') %}

Your templates aren’t outputting anything.

Jinja uses various delimiters in the template strings.

{% %} - statements, these do not output anything.

{{ }} - expressions to print to the template output.

{# #} - comments which are not included in the template output, they also do not execute statements.

Adding a - after a starting delimiter will remove previous whitespace. Adding a - before an ending delimiter will remove trailing white space.

e.g.

{%- removes leading white space before a statement

-}} removes trailing white space after an expression that prints to the template output

Thanks for replying. Sorry I can’t format the code correctly (Edited to correct).
I was expecting the end of day values of the two sensors to be saved as two entities. I can add the curly brakets to output their values in Developer tools OK.

To format code, use 3 backticks before and after the code block.

` is a backtick.

On american keyboards, it shares the same button with ~ (tilda)

e.g.

```

```