New MQTT Sensor Configuration and Template

Hello,

Manually configured MQTT sensor(s) found under platform key ‘sensor’, please move to the mqtt integration key, see MQTT Sensor - Home Assistant

As the configuration of MQTT Sensors will change I was wondering how I should split my configuration.

Until now all MQTT Sensor YAML Files and Template YAML Files were in the sensor Folder (see below at: My current configuration structure).

My Question is:

  • Do I need to split the MQTT Sensor File and the Template File in two different Folders?
  • Is there a way to have the two Files in Folder mqtt/sensor?
  • What do I need to change in File 2 to archive this?

New configuration structure:
configuration.yaml

mqtt: 
  sensor: !include_dir_merge_list mqtt/sensor/ 

sensor: !include_dir_merge_list sensor/

File 1 in Folder mqtt/sensor/

- name: outdoor_node_1_status
  state_topic: "home/outdoor_node_1/status"
  value_template: '{{ value_json.node_status }}' 

File 2 in Folder sensor/

- platform: template
  sensors:
    template_outdoor_node_1_status:
      ##friendly_name: "XXX"
      value_template: "{{ states.sensor.outdoor_node_1_status.state }}"
      icon_template: >-
        {% if is_state('sensor.outdoor_node_1_status', 'online') %}
          mdi:wifi
        {% else %}
          mdi:wifi-off
        {% endif %}


My current working configuration structure:
configuration.yaml

sensor: !include_dir_merge_list sensor/ 

File 1 in Follder sensor/

- platform: mqtt
  name: outdoor_node_1_status
  state_topic: "home/outdoor_node_1/status"
  value_template: '{{ value_json.node_status }}' 

File 2 in Follder sensor/

- platform: template
  sensors:
    template_outdoor_node_1_status:
      value_template: "{{ states.sensor.outdoor_node_1_status.state }}"
      icon_template: >-
        {% if is_state('sensor.outdoor_node_1_status', 'online') %}
          mdi:wifi
        {% else %}
          mdi:wifi-off
        {% endif %}

For what it is worth here is what I did to simplify my configuration.

  • Added this to configuration.yaml
mqtt: 
  #certficate: auto
  sensor: !include_dir_merge_list _mqtt/sensor/
  binary_sensor: !include_dir_merge_list _mqtt/b_sensor/
  • Example mqtt binary sensor file located in homeassistant/_mqtt/b_sensor directory
- name: "Garage Door"
  state_topic: 'home/sensor7'
  device_class: door
  qos: 1
  • Follow this pattern for each of the MQTT Sensor types
2 Likes

When I add the sensor line to my configuration.yaml I get an error on validating the config

# MQTT
mqtt:
  broker: !secret mqtt_host
  port: !secret mqtt_port
  username: !secret mqtt_username
  password: !secret mqtt_password
  discovery: true
  sensor: !include_dir_merge_named includes/mqtt/sensors/
Invalid config for [mqtt]: required key not provided @ data['mqtt']['sensor'][0]['state_topic']. Got None. (See /config/configuration.yaml, line 33). 

Line 33 is the mqtt declaration

Removing the sensor line removes the error

The error occurs with or without a file in the includes directory. Removing the other settings under mqtt makes no difference

Any idea what I’m doing wrong here?

To answer my own question, I needed !include_dir_merge_list instead of !include_dir_merge_named