Separate sensor files not including?

I’m trying to organize my files but for some reason the include directory isn’t working for sensors.

I’ve already done this with automations, and it works great:
!include_dir_merge_named automations_yaml

Now I want to do it for sensors.

If I try and import sensor files using a directory, this doesn’t work (but passes checks no errors):
!include_dir_merge_named sensors_yaml

But if I maually specify exactly one file, it works perfectly
!include sensors_yaml/rtl433_mqtt_acurite_tower_sensors.yaml

What is going on here?

Here’s the contents of the file sensors_yaml/rtl433_mqtt_acurite_tower_sensors.yaml:

# Outside Front Sensor
- platform: mqtt
  name: "Outside Front Temperature"
  state_topic: "homeassistant/sensor/rtl433/Acurite_tower_sensor/14084"
  unit_of_measurement: '°C'
  value_template: "{{ value_json.temperature_C }}"
- platform: mqtt
  name: "Outside Front Humidity"
  state_topic: "homeassistant/sensor/rtl433/Acurite_tower_sensor/14084"
  unit_of_measurement: '%'
  value_template: "{{ value_json.humidity }}"

# Outside Rear Sensor
- platform: mqtt
  name: "Outside Rear Temperature"
  state_topic: "homeassistant/sensor/rtl433/Acurite_tower_sensor/9972"
  unit_of_measurement: '°C'
  value_template: "{{ value_json.temperature_C }}"
- platform: mqtt
  name: "Outside Rear Humidity"
  state_topic: "homeassistant/sensor/rtl433/Acurite_tower_sensor/9972"
  unit_of_measurement: '%'
  value_template: "{{ value_json.humidity }}"

!include_dir_merged_named is for things that aren’t lists. For exapmle, scripts.

script:
  script_name_1:
     ...
  script_name_2:
     ...

include_dir_merged_list is for things that are a list (starts with a ‘-’ in YAML)

  - platform: mqtt
   ...
  - platform: mqtt
  ...

This is a list of things. So you should do !include_dir_merge_list to merge a list of things into a single list.

Also, I’m not sure how your automatons are working in that configuration as automations are also a list of things…

Here is how I have some of mine laid out…

automation: !include_dir_merge_list automations/
scene: !include_dir_merge_list scenes/
script: !include_dir_merge_named scripts/
sensor: !include_dir_merge_list sensors/

Use packages and you can do whatever you want in one file.

homeassistant:
  packages: !include_dir_named packages

packages/my_motion_lights.yaml

sensor:
  ...

switch:
  ...

automation:
  ...

script:
  ...
1 Like

Interesting, I changed all 3 to list and now automations, sensors, and binary-sensors are all working.

I will have to look at the packages, that sounds like it may be an even better way to group related sensors and devices in the same file (e.g. my Acurite 433MHz thermometers have temp, humidity, and binary low-battery among other things they send)

I don’t know how _named was working for you with automations. It’s a list, not a ordered dictionary. It should have never worked in the first place.

Hi petro. I’m confused! I’m reading the documentation “Splitting up configurations” over and over again. It does not show the result.

At the end !include-statments feed-in files and combine it to a larger stream. Right? Under a list i understand a list of files, not the content. Like the DIR DOS-command. And LIST command lists the content if files.

Could one say that !include using _list results in a list of files? Like the CLI command DIR does?

the _list or _named has nothing to do with the files. It has to do with the collection/data used in the configuration, where you are using the include.

List

In yaml, this is a list:

field:
- name: a
  attribute: x
- name: b
  attribute: y
- name: c
  attribute: z

The - signifies the start of each listed item in yaml.

if you wanted to separate a, b, and c into separate files, you’d use !include_dir_list or !include_dir_merge_list.

If you use !include_dir_list, you have to separate each listed item into a file. a, b, and c need to be separate files.

In the files, you must omit the - because the - is implied with the separate files.

field: !include_dir_list my_stuff

my_stuff/my_file1.yaml

  name: a
  attribute: x

my_stuff/my_file2.yaml

  name: b
  attribute: y

my_stuff/my_file3.yaml

  name: c
  attribute: z

If you use !include_dir_merge_list, you can add any number of items into a file. You can have a and b in the same file while c is separate.

You must include the - because you can have more than 1 listed item in the files.

field: !include_dir_merge_list my_stuff

my_stuff/my_file1.yaml

- name: a
  attribute: x
- name: b
  attribute: y

my_stuff/my_file3.yaml

- name: c
  attribute: z

Dictionary

In yaml, this is a dictionary:

field:
  a:
    attribute: x
  b:
    attribute: y
  c:
    attribute: z

Notice the lack of -? Also notice that each item has a name and that name defines the item.

if you wanted to separate a, b, and c into separate files, you’d use !include_dir_list or !include_dir_merge_list.

If you use !include_dir_named, you have to separate each named item into a file. a, b, and c need to be separate files.

Keep in mind that you MUST name each file in relation to the name you used inside field. So a: will become a.yaml as the filename, etc.

field: !include_dir_named my_stuff

my_stuff/a.yaml

attribute: x

my_stuff/b.yaml

attribute: y

my_stuff/c.yaml

attribute: z

If you use !include_dir_merge_named, you can name these items in the file and the filename has no impact on the internal data.

field: !include_dir_merge_named my_stuff

my_stuff/my_file1.yaml

  a:
    attribute: x
  b:
    attribute: y

my_stuff/my_file2.yaml

  c:
    attribute: z
1 Like

Hola Petro.

Thank you for this comprehensive explanation.

What i tried to do is cut out a section from configuration.yaml out into a separate file.

Lines to separate into “eWelink.yaml”

    switch:
      - platform: template
        switches:
          ewelink_virtual_switch:
            turn_on:
              service: switch.turn_on
            turn_off:
              service: switch.turn_off
    
    cover:
      - platform: template
        covers:
    
    fan:
      - platform: template
        fans:

and replace it with a !include-statement.
From you explanations i came to the following solution:

switch: !include_dir_merge_named ewelink.yaml

cover: !include_dir_merge_named ewelink.yaml

fan: !include_dir_merge_named ewelink.yaml

The config checker is happy with this. But is it correct? One include for every label? Although all labels are in the same file?

Had i designed the include - logic it would be straight forward: Cut-out lines into a separate file and put instead a include-statement. Without any limiting rules. Just to make the main file (program, definition, yni) more redable. Why isn’t it like that?

If you’re just including single files then you just use !include. But it’s odd that they all have the same name. They should not as they should be different.

This was my first intent: !include ewelink.yaml. But the include-statement requires a label like:

integration: !include file.yaml

As ewelink is not an integration (i’ts an Add-on) this attempt resulted in errors:

  • With label ewelink: Integration error: ewelink - Integration 'ewelink' not found

  • with label switch: Invalid config for [switch]: required key not provided @ data['platform']. Got None. (See /config/configuration.yaml, line 108).