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.