How do I setup template trigger sensor in a Splitting config

I would like to add weather forecast sensor. my config look like this:

light: !include light.yaml
binary_sensor: !include aVR/binary_sensor/binary_sensor.yaml 
switch: !include switch.yaml
sensor: !include_dir_merge_list aVR/sensor/ 
template:
  - binary_sensor: !include_dir_merge_list aVR/template/binary_sensor/ 
  - sensor: !include_dir_merge_list aVR/template/sensor/
  #- trigger: !include aVR/trigger.yaml
  - !include aVR/trigger.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

and this is what I did in my trigger.yaml

  - trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.openweathermap'].forecast[0].temperature }}"
        unit_of_measurement: °C

What am I doing wrong?

This is not a valid include.

You must have a key for an include, e.g.

This is valid

something: !include something.yaml

This is not:

something: 
  - !include something.yaml

There’s a whole topic on the best way to do this for the template integration with many methods if you search the forum.

EDIT: here it is: Splitting config for template: There are a lot of false leads in that topic too.

Personally I ended up doing this:

configuration.yaml

template: !include templates.yaml

and in templates.yaml

- binary_sensor: !include template_binary_sensors.yaml

- image: !include images.yaml

- sensor: !include template_sensors.yaml

#################################################################
# All my triggered template sensors here
#################################################################

- trigger: 
    - platform: etc...

It’s a lot easier if you use directories. I like a flat file structure in /config though.

1 Like

My setup is very much like @tom_l described. I actually modeled mine off of @frenck where all integrations are loaded from a folder that then points to an entity folder with subfolders for each type of entity and even sub-sub folders for templates.

So for example, regarding your question:

configuration.yaml

homeassistant:
  customize: !include customize.yaml
  packages: !include_dir_named integrations

And in the ‘integrations’ folder I have all my YAML files for the various integrations I do. One is for templates:

template.yaml

template: !include_dir_list ../entities/templates/sensors

Which I create folders of YAML files that I organize categorically (i.e., a folder called Weather for my weather template sensors).

This is where I differ a bit from others, I throw all my templates into the “sensors” folder (which was probably an oversight when I first set it up) because it was when there was a transition to the newer way of doing templates, so I wanted to break out the new versus the old. You are probably better off doing what Tom described and putting each sensor type into its own folder. Mine works the same but reads in a confusing way.

Once there, the templates themselves follow this format inside their own YAML:

sensor:
  - name: "Downstairs CO2 Health"
    unique_id: 13fe52a0-15a1-4d5b-956c-00114943d642
    state: >-
      {% set co2 = float(states('sensor.basement_air_quality_carbon_dioxide'), 0) %}
      {% set level = "Normal" %}

      {% if co2 >= 800 and co2 < 1000 %}
        {% set level = "Moderate" %}
      {% elif co2 >= 1000 %}
        {% set level = "High" %}
      {% endif %}

      {{ level }}

    icon: mdi:brain

The thing that often throws people is that they want to copy-paste the documented examples or examples posted here, but once you nest everything into packages you don’t start your template the same.

You start at the domain and work down:

sensor:
  - name: "Downstairs CO2 Health"

Instead of:

template:
  - sensor:
      - name:
    ...

It can get confusing to wrap your head around at first, but when you do packages then you’ve already created the dictionary so you don’t have to indent or prepend a dash in most cases (depending on how you include the file(s)).

It was a bit confusing at first but now that I’m used to it I love it this way rather than having to dig through hundreds or thousands of lines in a single YAML, plus it makes it really simple to add or remove integrations or entities on the fly.

I changed my config to: template:

!include aVR/templates.yaml

when I add the following lines to my templates.yaml file. All my templates stop working.

  - binary_sensor: !include_dir_merge_list aVR/template/binary_sensor/ 
  - sensor: !include_dir_merge_list aVR/template/sensor/

how do I add a trigger template? I would like to add the following sensor:

- trigger:
      - platform: time_pattern
        hours: /1
    action:
      - service: weather.get_forecasts
        data:
          type: hourly
        target:
          entity_id: weather.openweathermap
        response_variable: hourly
    sensor:
      - name: Temperature forecast next hour
        unique_id: temperature_forecast_next_hour
        state: "{{ hourly['weather.openweathermap'].forecast[0].temperature }}"
        unit_of_measurement: °C

Assuming you are using packages and this sensor is in a YAML file of its own:

trigger:
  - platform: time_pattern
    hours: /1
action:
  - service: weather.get_forecasts
    data:
      type: hourly
    target:
      entity_id: weather.openweathermap
    response_variable: hourly
sensor:
  - name: Temperature forecast next hour
    unique_id: temperature_forecast_next_hour
    state: "{{ hourly['weather.openweathermap'].forecast[0].temperature }}"
    unit_of_measurement: °C

I added yaml in my template sensor folder, with only the info you gave me. There is no erros, but the sensor was not created.