Splitting out sensors from configuration.yaml

i have the following in my configuration.yaml:

template:
  - sensor:
      - name: "Average temperature"
        unit_of_measurement: "°C"
        state: >
          {% set stue = states('sensor.netatmo_home_living_room_temperature') | float %}
          {% set kontor = states('sensor.netatmo_home_living_room_gaestevaerelse_temperature') | float %}
          {{ ((stue + kontor) / 2) | round(1, default=0) }}
      - name: "Average illum" # test
        unit_of_measurement: "lx"
        state: >
          {% set stue = states('sensor.motion_livingroom_1_illuminance') | float %}
          {% set kontor = states('sensor.motion_office_illuminance') | float %}
          {{ ((stue + kontor) / 2) | round(1, default=0) }}
      - name: "how_bright_office"
        state: >
          {% set brightness = states('sensor.motion_office_illuminance') | int %}
          {% if brightness > 100 %}
            bright
          {% else %}
            not so bright
          {% endif %}
          

And this works as intended. I would like to split this out in a separate sensors.yaml file using

sensor: !include sensors.yaml

But cant make it work. Any hint on how to do this would be much appreciated

You can’t have a flat single file structure but you can include a directory.

This is as close as I came to a flat structure https://community.home-assistant.io/t/splitting-config-for-template/328636

thanks a lot; that helped me figure out that i of course need …

template: !include templates.yaml

… instead, which works just fine

Nope. That won’t work. Read the topic I linked.

If you don’t use triggered sensors, you can do this https://community.home-assistant.io/t/splitting-config-for-template/328636/27?u=tom_l

If you do use them then you have to do this: https://community.home-assistant.io/t/splitting-config-for-template/328636/28?u=tom_l

I finally got it to work like this:

template:
  - binary_sensor: !include template_binary_sensors.yaml
  - sensor: !include template_sensors.yaml

I tried putting them both in one file, but couldn’t get it working. One of those links above suggested using “dummy” templates which aren’t needed, but somehow satisfy the limitations of the include process. That seemed to work but the solution I settled on seems more elegant.

I also struggled with the spacing in the include files. Some examples seemed to suggest that the included lines could or should be shifted left a couple of spaces, but I didn’t find that. In my sensors include file I have things like this:

      - name: "guest_rm_temperature"
        unique_id: "guest_rm_temp"
        unit_of_measurement: "°F"
        state: "{{ state_attr('climate.guest_rm', 'current_temperature') }}"

I’m very happy with the results. This project gave me the opportunity to change all my templates over to the “new” method, and organize them better. I was sorry to lose the “friendly name” option, but actually having one less place to have to maintain entity names is probably a good thing.

1 Like

thanks for your input, for me it works having sensors, binary sensors and triggered sensors all in one file using

template: !include templates.yaml

Can you show the structure inside your templates.yaml file?

And that there aren’t actually any errors in your log related to this?

Because it simply isn’t possible.

- sensor:
    - name: "Average temperature"
      unit_of_measurement: "°C"
      state: >
        {% set stue = states('sensor.netatmo_home_living_room_temperature') | float %}
        {% set kontor = states('sensor.netatmo_home_living_room_gaestevaerelse_temperature') | float %}
        {{ ((stue + kontor) / 2) | round(1, default=0) }}
    - name: "Average illum" # test
      unit_of_measurement: "lx"
      state: >
        {% set stue = states('sensor.motion_livingroom_1_illuminance') | float %}
        {% set kontor = states('sensor.motion_office_illuminance') | float %}
        {{ ((stue + kontor) / 2) | round(1, default=0) }}
    - name: "how_bright_office"
      state: >
        {% set brightness = states('sensor.motion_office_illuminance') | int %}
        {% if brightness > 100 %}
          bright
        {% else %}
          not so bright
        {% endif %}
    - name: "how_bright_livingroom"
      state: >
        {% set brightness = states('sensor.motion_livingroom_1_illuminance') | int %}
        {% if brightness > 200 %}
          extremely bright
        {% elif brightness > 100 %}
          bright
        {% elif brightness > 10 %}
          not so bright
        {% else %}
          quite dark
        {% endif %}
- binary_sensor:  
    - name: "all_lights"
      state: >
        {% if is_state('light.net_light', 'off') %}
          off
        {% else %}
          on
        {% endif %}
- trigger:
    - platform: state
      entity_id: light.net_light
      to: "on"
  sensor:
    - name: "illum_office"
      state: "{{ states('sensor.motion_office_illuminance') | int }}"

Oh right. Yeah that will work.

I was thinking binary_sensors and sensors in different files, which was my aim originally.

But if you are happy with them all in the one file that will work.