Splitting configuration file

I’m starting from the ground up with Home Assistant. My current version is out of date and the config is a total mess.

I was thinkign that this time around I’d split my configuration file out and use directories and seperate yaml files to keep things organized.

Now I’m only a few hours into getting things back into HA and I’m taking things slowly, making sure things work properly before moving on the next thing. Right now I’m working on setting up sensors for a weather platform.
Unfortunately I am getting the following error:

Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. (See /config/configuration.yaml, line 14).

The relevant area of my configuration file looks like this:

#Weather
weather: !include weather.yaml

sensor: !include_dir_named sensors
group: !include groups.yaml

In my sensor directory I have 1 file named environment_canada.yaml which contains:

#Environment Canada Sensors
platform: environment_canada

Appreciate any insight on this

Rob,

For code please use “Preformatted text” button:

intend

It’s very hard to tell if indentation is correct.
Maybe in your “environment_canada.yaml”, indentation is not correct.

No indentations, according to the docs there are no indetations or ‘-’ before platform when using !include_dir_named

Also I tried with - platform, no identations, and with indentations
I also tried with trailing ‘/’ after the directory name.
None of my attempts yielded any different results.

Does your sensorfiles beginn with ‚sensor‘? For example:


sensor:
  - platform: template
    sensors:
#
      date: 

It is. Read https://www.home-assistant.io/docs/configuration/splitting_configuration/

" This small example illustrates how the “split” files work. In this case, we start with two (2) device tracker entries (owntracks and nmap). These files follow “style 1” that is to say a fully left aligned leading entry (- platform: owntracks) followed by the parameter entries indented two (2) spaces."

Example:
configuration.yaml

sensor: !include_dir_merge_list sensor/

One of the sensor file:

#Home IP
  - platform: rest
    resource: https://api.ipify.org?format=json
    value_template: '{{ value_json.ip }}'
    name: Home IP

You missing " - " before platform:

Yes, but when I put a file named environment_canada in my packages directory with platform: environment_canada , I get the following error:

expected dict for dictionary value @ data['packages']['environment_canada']['platform']

When I use

sensor:
  - platform: environment_canada

the config is fine.

Before you mention it’s sensor and you like to include it using

sensor: !include_dir_named sensors

If it’s sensor use:

sensor: !include_dir_merge_list sensor/

and remove sensor: from code

  - platform: environment_canada

No. My packages directory includes a folder sensoren including several sensor files.

sensors aren’t “named”. They are “lists”.

lists start with “-”. like sensors, automations, binary sensors, etc.

named things don’t start with “-” - like scripts.

and you have to use a “/” after the “sensor” to denote that it’s a directory.

So I should be using !include_dir_merge_list instead of !include_dir_named?

Thanks, is there a clear definition of what each should be used for? I don’t find the docs overly clear on this.

Rob,

“!include_dir_merge_list” will merge/combine all .yaml files in directory.
“!include_dir_named” will map filenames to file content

Advanced Usage

I don’t understand exactly what that means. I’m a very visual learner, I love examples.
I assume that dir_merge_list is the most common usage and works something like this?

configuration.yaml

Yaml before !include_fir_merge_list

#contents of file 1
lines
  of 
  yaml
  code

#contents of file 2
lines
  of
  yaml
  code

#contents of file 3
lines
  of
  yaml
  code

Yaml after !include_fir_merge_list

In my configuration.yaml, I have:

sensor: !include_dir_merge_list sensors

I then have a folder called sensors with a bunch of YAML files in. Each one of these files is formatted thus:

- platform: rest
  resource: http://192.168.1.9/DevMgmt/ProductUsageDyn.xml
  name: "Printer total page count"
  value_template: "{{ value_json['pudyn:ProductUsageDyn']['pudyn:PrinterSubunit']['dd:TotalImpressions']['#text'] }}"
  unit_of_measurement: "pages"

- platform: rest
  resource: http://192.168.1.9/DevMgmt/ProductUsageDyn.xml
  name: "Printer colour page count"
  value_template: "{{ value_json['pudyn:ProductUsageDyn']['pudyn:PrinterSubunit']['dd:ColorImpressions'] }}"
  unit_of_measurement: "pages"

All works fine, and I can add more YAML files into that folder, or even subfolders of it, and they all get included on reload.

@rlongfield — yes, the files are included as you have presented it.

“!include_dir_merge_list” will merge/combine all .yaml files in directory.
Example:
if you like to combine all sensors .yaml files, HA will already know it’s a sensors and you don’t have to add to all files

sensor:

just name of the platform and etc…

configuration.yaml

sensor: !include_dir_merge_list sensor/

/config/sensor/ directory will have number of .yaml files

home_ip.yaml

#Home IP
  - platform: rest
    resource: https://api.ipify.org?format=json
    value_template: '{{ value_json.ip }}'
    name: Home IP

mta_status.yaml

# MTA service status sensor
  - platform: command_line
    name: MTA service status
    command: "python3 /config/python_scripts/service_status.py"
    scan_interval: 600

“!include_dir_named” will map filenames to file content

if you using in configuration.yaml

sensor: !include_dir_named sensors

/config/sensor/ directory will have number of .yaml files

you have to add to each file

sensor:

home_ip.yaml

#Home IP
sensor:
  - platform: rest
    resource: https://api.ipify.org?format=json
    value_template: '{{ value_json.ip }}'
    name: Home IP

mta_status.yaml

# MTA service status sensor
sensor:
  - platform: command_line
    name: MTA service status
    command: "python3 /config/python_scripts/service_status.py"
    scan_interval: 600

I hope it will help.

@Troon @Vlad Thanks for the clarification.

I wish there was a way to see the ‘working’ configuration.yaml file.
In my case if I could have seen the configuration.yaml file that was the result of the !includes I might have been able to catch the mistake earlier, especially when the generated error message is referencing a blank line.

This is my working configuration.yaml

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  # Location required to calculate the time the sun rises and sets
  latitude: !secret loc_latitude
  longitude: !secret loc_longitude
  # Impacts weather/sunrise data (altitude above sea level in meters) Elevation: 18m / 59feet of Brooklyn, NY, USA
  elevation: !secret elev
  # metric for Metric, imperial for Imperial
  unit_system: metric
  external_url: https://xxxxxxxxxx.xxxxxxxx.org
  internal_url: http://192.168.1.244:8123
  # Pick yours from here: http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  time_zone: !secret time_zone
    ##Authentication
  auth_providers:
   - type: homeassistant
   - type: legacy_api_password
     api_password: !secret http_password
   - type: trusted_networks
     trusted_networks:
      - 127.0.0.1
      - 192.168.1.0/24
  customize: !include include/customize.yaml
  packages: !include_dir_named packages/
#  whitelist_external_dirs:
  allowlist_external_dirs:
     - /config/data/
# Show links to resources in log and frontend
##custom_updater:
##introduction:
# Enables configuration UI
config:
system_health:
# Enables the frontend
frontend: !include include/frontend.yaml
lovelace: !include include/lovelace.yaml
##  mode: yaml
http: !include include/http.yaml
customizer: !include include/customizer.yaml
##customize_glob: !include include/customize_glob.yaml
##updater: !include include/updater.yaml
nest: !include include/nest.yaml
# Discover some devices automatically
discovery: !include include/discovery.yaml
# Allows you to issue voice commands from the frontend in enabled browsers
conversation: !include include/conversation.yaml
map:
mqtt: !include include/mqtt.yaml
# Enables support for logger
logger: !include include/logger.yaml
# Enables support for tracking state changes over time.
history: !include include/history.yaml
# View all events in a logbook
logbook: !include include/logbook.yaml
#DB purge_days
recorder: !include include/recorder.yaml
# Track the sun
sun:
python_script:
notify: !include include/notify.yaml
##panel_iframe: !include include/panel_iframe.yaml
##panel_custom: !include include/panel_custom.yaml
####OWNTRACK START####
owntracks: !include include/owntracks.yaml
####OWNTRACK END####
alexa_media: !include include/alexa_media.yaml
jewish_calendar: !include include/jewish_calendar.yaml
sensor: !include_dir_merge_list sensor/
switch: !include_dir_merge_list switches/
light: !include_dir_merge_list lights/
tts: !include_dir_merge_list tts/
group: !include include/groups.yaml
automation: !include include/automations.yaml
binary_sensor: !include include/binary_sensors.yaml
script: !include_dir_named scripts/
media_player: !include include/media_player.yaml
device_tracker: !include include/device_tracker.yaml
##shell_command: !include_dir_named shell/
input_boolean: !include include/input_boolean.yaml
input_select: !include include/input_select.yaml
climate: !include include/climate.yaml
#speedtestdotnet: !include include/speedtestdotnet.yaml
camera: !include include/camera.yaml
#android_ip_webcam: !include include/ip_camera.yaml
vesync: !include include/vesync.yaml
smartir: !include include/smartir.yaml
#vacuum: !include include/vacuum.yaml
#stream:
shopping_list:
wake_on_lan: !include include/wake_on_lan.yaml
webostv: !include include/webostv.yaml
calendar: !include include/calendar.yaml
##websocket_api: !include include/websocket_api.yaml
input_number: !include include/input_number.yaml
timer: !include include/timer.yaml
zone: !include include/zone.yaml
sense: !include include/sense.yaml