Can't figure out !include_dir_merged_list for my mqtt sensors

When I put the configuration directly into the configuration.yaml file the sensor works. I can see the data in the states. However, when I try to move it out and into a more organized list, it stops working. I’ve tried several different configurations and can’t seem to figure out the magic combination. Even the MQTT documentation only shows it in the configuration.yaml file but no examples on how to move it to its own file.

configuration.yaml (works)

mqtt:
  sensor:
    - name: "R2D2"
      state_topic: "r2d2/battery"
      value_template: "{{ value_json.BatteryState }}"

configuration.yaml (doesn’t work)

mqtt: !include_dir_merge_list mqtt/

mqtt/ directory with 1 file in it (r2d2.yaml)

sensor:
  - name: "R2D2"
    state_topic: "r2d2/battery"
    value_template: "{{ value_json.BatteryState }}"

If I try to move that into the sensors directory I already have set up, it yells at me for bad syntax, so I adjust it like so:

- platform: mqtt # <- error Missing property "state_topic".

Okay, so I add a state_topic

- platform: mqtt
  state_topic: "r2d2/battery" # <- error String does not match the pattern of "DEPRECATED^"

What am I doing wrong?

It’s not a list.

This is a list:

mqtt:
  - sensor:
  - binary_sensor:
  - cover:

This is a dictionary/map:

mqtt:
  sensor:
  binary_sensor:
  cover:

Use !include_dir_merge_named.

Thanks for the reply, but what?

Why in some places do I need platform and others I don’t? When do I use a list instead of a dictionary?

I have read and reread the docs on this but it’s still not clear. I even have a separate sensors directory with multiple files in it, another directory for rest stuff and they all are in the config as include_dir_mege_list. This mqtt one acts differently.

When the integration documentation tells you to.

Okay. But the MQTT docs do not say which one to use. That’s why I opened this topic.

There are clear examples:

Yep. I understand that part if it’s directly in the main file, but if you now move that to a separate file in a different directory it’s broken and the docs don’t help with reconfiguring it.

You can’t put mqtt sensors in your sensor directory. Or you will be trying to merge all the sensors (even the ones that aren’t using the mqtt platform) under mqtt:

Create a new directory, mqtt_sensors, or something like that.

Thanks again for your replies, as a troubleshooting tactic I tried the sensors directory because that’s what it ultimately is so I thought I would try and posted my issues along with my original attempt to show effort. Sorry I was wrong but now I know. Moving on.

I stated in my original post I have an mqtt directory. But I tried to put that markup in there (along with different syntax attempts) but the sensor doesn’t appear. I used the same syntax to merge the mqtt files as I did sensors, and rest commands. The sensor only seems to show up if the markup is directly in the configuration.yaml file.

I apologize if my issue is unclear.

This topic looks like it might have answers. I’ll read through it tomorrow. Thanks again.

Did you manage to get this to work, Im strugelig with the same issue

The instructions don’t change no matter how many files are involved.

  • !include = “take whatever is in this YAML file and shove it in this spot exactly as is”
  • !include_dir_list = “Add a list here. For every YAML file in this directory, shove its contents into an item in this new list”
  • !include_dir_named = “Add a dictionary here. For every YAML file in this directory, add a new item to the dictionary where the key is the file name and the value is the contents of the file”
  • !include_dir_merge_list = “Add a list here. Every YAML file in this directory contains a list, merge all those lists into this new list.”
  • !include_dir_merge_named = “Add a dictionary here. Every YAML file in this directory contains a dictionary, merge all those dictionaries into this new dictionary.”

So it doesn’t matter what you put in what file. If you want to figure out why its telling you your schema is invalid start from configuration.yaml. Pretend the !include directives don’t exist by mentally merging in the files as a I described. Or physically merging them by copying and pasting stuff from one to another if you’re having trouble envisioning it. If that no longer matches what the documentation is showing for configuration.yaml then you have your answer.

1 Like

In the past I followed the instruction from https://sequr.be/blog/2020/10/home-automation-getting-started-with-mqtt-and-home-assistant-and-shelly/ to get my shelly h&t and shelly plugs ready. I liked the flavour to have good file/folder structure.

With that change of the HA mqtt function few weeks ago, that sensors did also not work anymore. I had the same issue: The MQTT YAML code work in the configuration.yaml, but not in a seperated file mqtt.yaml with the include command.

Today I was able to get them running again. For that, I moved all the shelly YAML files into a subfolder /mqtt and named them like “mqtt-sensor-shelly.yaml” or “mqtt-switch-shelly.yaml”.

The configuration.yaml looks like this (Thx for that important hint about the “_named”, @tom_l):

mqtt: !include_dir_merge_named mqtt/

The file “mqtt/mqtt-sensor-shelly.yaml” looks like this (first lines).

> # /mqtt/mqtt-sensor-shelly
> # ----------------------------------------------------------------------------------------
> # shellyht01
> # Temperatur
> sensor:
>   - name: "Shellyht01 Arbeitszimmer Temperatur"
>     unique_id: "shellyht01_temperature"
>     unit_of_measurement: "°C"
>     expire_after: 86400
>     device_class: temperature
>     icon: mdi:home-thermometer-outline
>     qos: 0
>     state_topic: "shellies/shellyht-XXXXXX/sensor/temperature"
>     json_attributes_topic: "shellies/shellyht-XXXXXX/announce" # Nur bei einer Entitaet pro shelly Sensor notwendig
>   # Luftfeuchte
>   - name: "Shellyht01 Arbeitszimmer Luftfeuchte"
>     unique_id: "shellyht01_humidity"
>     unit_of_measurement: "%"
>     expire_after: 86400
>     device_class: humidity
>     icon: mdi:water-percent
>     qos: 0
>     state_topic: "shellies/shellyht-XXXXXX/sensor/humidity"
> # ---------------------------------------------------------------------------------------

The file “mqtt-switch-shelly.yaml” looks like this (first lines).

# /mqtt/mqtt-switch-shelly.yaml

switch:
  # ----------------------------------------------------------------------------------------
  ## shellyplugs01
  - unique_id: shellyplugs01_mobil_steckdose
    name: "ShellyplugS01 Mobil Steckdose"
    payload_on: "on"
    payload_off: "off"
    qos: 1
    state_topic: "shellies/shellyplug-s-XXXXXX/relay/0"
    command_topic: "shellies/shellyplug-s-XXXXXX/relay/0/command"
# ----------------------------------------------------------------------------------------

The file/folderstructure:

configuration.yaml
/mqtt/mqtt-sensor-shelly.yaml
/mqtt/mqtt-switch-shelly.yaml

With that, HA gets the sensor values again. Yeah! Done.

Thx for the initial post, @fischgeek .

As I understand this way I can have only one “switch”/“sensor”/… file in the mgtt folder. This is very inconvenient as earlier I was able to use different “sensor” files for logical grouping elements based on e.g. different rooms.
The other solution to put every mqtt element to a separate file in separate directories and use “!include_dir_named” results high number of files what less convenient than a rational logical grouping.
So it seems to me, this new mqtt setup solution is a step backwards in configuration file structure flexibility.

1 Like