Splitting config for template:

I have just spent 8 hours moving all my template binary sensors and sensors into the new format.

I have this in my configuration.yaml file:

template: !include templates.yaml

And that file contains this structure:

#################################################################
## Binary Sensors
#################################################################
- binary_sensor:

# Day / Night sensor

  - name: "Dark Outside"
    icon: "mdi:weather-night"
    state: "{{ states('sensor.calculated_light_brightness')|int >= 24 }}"

# Dishwasher Running

  - name: "Dishwasher Running"
    icon: "mdi:dishwasher"
    state: "{{ states('sensor.dishwasher_power')|float(default=0) > 10 }}"
    device_class: moving
    delay_off:
      minutes: 5
    delay_on:
      minutes: 2

etc...

#################################################################
## Sensors
#################################################################

- sensor:

# Amplifier sensors, CINEMA

  - name: "Cinema Dialogue Lift"
    state: "{{ state_attr('sensor.cinema_amp', 'dialogue_lift')|float / 2 }}"

  - name: "Cinema Dialogue Level"
    state: "{{ state_attr('sensor.cinema_amp', 'dialogue_level')|int }}"

etc...

Yet I am getting this error:

Invalid config for [sensor.template]: expected dictionary for dictionary value @ data['sensors']. Got None. (See ?, line ?). 

I’ve been over the docs tons of times but can’t see what I am doing wrong.

That message is vaguely familiar from long ago. I have a hazy recollection of having to do something goofy like putting at least one Trigger-based Template Sensor at the top so that Template Sensors and Template Binary Sensors don’t appear first.

Paste this freebie at the top of your templates.yaml file, reload and see if it makes any difference.

- trigger:
  - platform: time
    at: '00:00:00'
  binary_sensor:
  - name: 'Festive Season'
    state: >
      {% set m = now().month %}
      {% set d = now().day %}
      {{ m in [1,10,12] and
         ( (m == 12 and 1 <= d <= 31) or
           (m == 1 and 1 <= d <= 10) or
           (m == 10 and d == 31) ) }}

2 Likes

Thanks Taras, no joy unfortunately. Same error.

As an experiment I commented out the !include and pasted the entire file contents under template: > Still the same error. So now I’m really confused. Maybe it does not like the indentation?

I just cut the entire - sensor: portion out and only left the - binary_sensor: key. Same damn error.

Before the possibility to move the new format into packages I played with template.yaml, too, but I used template: !include_dir_merge_list template

I have a flat file structure. I don’t like hunting through directories.

Now I cut everything out of the file except Taras’s triggered template. Same error.

I’m still on 2021.7.x

Is this only supported in 2021.8.x?

Your template configuration is nearly identical to mine (except I have a few Trigger-based ones at the beginning of the file). I don’t get that error message.

The message refers to sensors (plural) but that’s not the option’s name (it’s sensor). Is it possible you have tradional sensors defined elsewhere?

Ah. I bet that’s it.

I commented them out as I went along, I’m guessing I missed one.

EDIT: Yes it was. Thanks Taras.

# - platform: template
#   sensors:
#     4g_internet_out_mbps:
#       value_template:  "{{ (states('sensor.erx_4g_wan_out_derivative')|float*8/1000000)|round(2) }}"
#       unit_of_measurement: 'Mbps'
#       friendly_name: '4G WAN Out'
#     erx_4g_wan_out_mb:
#       value_template:  "{{ (states('sensor.erx_4g_wan_out')|float/1048576)|round(2) }}"
#       unit_of_measurement: 'MB'
#       friendly_name: '4G Uploaded'

- platform: template
  sensors:
    # used_gb:
    #   friendly_name: "NBN Used"
    #   value_template: "{{ ( states('sensor.nbn_downloaded')|float + states('sensor.nbn_uploaded')|float )|round(2) }}"
    #   unit_of_measurement: "GB"
    # used_gb_pct:
    #   friendly_name: "Data Used"
    #   value_template: "{{ (100 * states('sensor.used_gb')|float / 500 )|round(1) }}"
    #   unit_of_measurement: "%"

Now lets restart and see what I’ve stuffed up somewhere in 8 hours work…

EDIT2: I don’t believe it. That was my only mistake!

…though I would like three files in /config.

template_triggered_sensors.yaml
template_binary_sensors.yaml
template_sensors.yaml

Will this work (can I nest includes)?

configuration.yaml:

template: !include template_triggered_sensors.yaml

template_triggered_sensors.yaml

#################################################################
## Triggered Binary Sensors and Sensors
#################################################################

########### Binary Sensors

- trigger: # Dummy triggered sensor for compatibility
  - platform: time
    at: '00:00:00'
  binary_sensor:
  - name: 'Festive Season'
    state: >
      {% set m = now().month %}
      {% set d = now().day %}
      {{ m in [1,10,12] and
        ( (m == 12 and 1 <= d <= 31) or
          (m == 1 and 1 <= d <= 10) or
          (m == 10 and d == 31) ) }}

########### Sensors

- trigger: ...etc

- binary_sensor: !include template_binary_sensors.yaml

- sensor: !include: template_sensors.yaml

For my setup, I use-

  1. Inside /config/configuration.yaml-
homeassistant:
  packages: !include_dir_named integrations
  1. Inside /config/integrations/template.yaml-
template:
  - sensor: !include_dir_list ../entities/templates/sensors
  - binary_sensor: !include_dir_list ../entities/templates/binary_sensors

For now, all sensors are showing up fine.


Just a suggestion (although different with what you intended), you can make it-

template:
  - sensor: !include template_sensors.yaml
  - binary_sensor: !include template_binary_sensors.yaml
  - template_trigger: !include template_triggered_sensors.yaml
3 Likes

Ok, sounds promising. I’ll give it a go.

Sorry, just realized that you want to store all 3 template.yaml inside /config, not /config/template. Probably you’ll need to change it to just sensor: !include template_sensors.yaml.

Furthermore, from the documentation, it is written-
Nesting !includes (having an !include within a file that is itself !included) isn’t going to work.

1 Like

Yeah I was way ahead of you :slight_smile:

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

I’m a little concerned about that last one though. It does not seem to follow the documentation for template triggers. https://www.home-assistant.io/integrations/template/#trigger-based-template-sensors

it needs to be -trigger: not template_trigger: and each triggered sensor/binary sensor has to start with -trigger:

EDIT: ayup,

Logger: homeassistant.config
Source: config.py:445
First occurred: 01:13:43 (1 occurrences)
Last logged: 01:13:43

Invalid config for [template]: [template_trigger] is an invalid option for [template]. Check: template->template_trigger. (See /config/configuration.yaml, line 162).

I don’t see any way to do the template triggered sensors.

My mistake :grimacing:
However if inside configuration.yaml-

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

Then, inside template_triggered_sensors.yaml, you write-

- trigger:
    - platform: time_pattern
      hours: 0
      minutes: 0
  sensor:
    - name: Not smoking
      state: ""
      unit_of_measurement: "Days"

Wouldn’t at the end it would be like-

template:
  - trigger:
      - trigger:
          - platform: xx
            ...

Yep. That’s the issue I can’t see a way around.

What if you add 2 lines of template:

template: !include template_triggered_sensors.yaml

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

No error from Check Configuration but I’m afraid one of them will be ignored.

Pretty sure that will generate a duplicate template: key error.

I don’t actually use triggered sensors as I find automations that publish retained mqtt messages and mqtt sensors initialise better after a restart. So I might just skip them.

In the off chance that I ever do need one I can bung it in configuration.yaml.

Tested on mine.

  1. Inside /config/integrations/template.yaml -
template:
  - sensor: !include_dir_list ../entities/templates/sensors
  - binary_sensor: !include_dir_list ../entities/templates/binary_sensors
  1. Inside /config/integrations/test.yaml -
template: !include_dir_list ../entities/templates/trigger_template
  1. Inside /config/entities/templates/trigger_template/justatest.yaml-
trigger:
  - platform: time_pattern
    # This will update every night
    hours: 0
    minutes: 0
sensor:
  # Keep track how many days have past since a date
  - name: Test
    state: 'unavailable'
    unit_of_measurement: "Days"

All sensors shows up fine including the trigger template.

1 Like

That’s not a flat directory structure though. Which was the aim. All in /config.

Oh! I think this will work:

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

I could even have:

template:
  - sensor: !include template_sensors.yaml
  - binary_sensor: !include template_binary_sensors.yaml
  - !include template_triggered_sensors.yaml
  - !include template_triggered_binary_sensors.yaml
2 Likes