Split configuration.yaml: binary sensor works, switches not!

I have some strange behavior with a configuration of relay over MQTT.
Struggled to get MQTT working, now it’s OK.

First strange thing: the MQTT integration doesn’t pick up the device even though it works.
Second odd thing with configuration.yaml:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#sensor: !include sensor.yaml
binary_sensor: !include binary_sensor.yaml
switch: !include switch.yaml

switch.yaml:

mqtt:
  switch:
    - name: 'relay16b-output1'
      unique_id: relay16b-output1
      state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
      command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
      payload_on: '{"relay1":{"on":1}}'
      payload_off: '{"relay1":{"on":0}}'
      value_template: '{{ value_json.relay1.on }}'
      state_on: 1
      state_off: 0
  
- platform: mqtt
  name: 'Output 2'
  unique_id: relay16B-2
  state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
  command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
  payload_on: '{"relay2":{"on":1}}'
  payload_off: '{"relay2":{"on":0}}'
  value_template: '{{ value_json.relay2.on }}'
  state_on: 1
  state_off: 0

binary_sensor.yaml:

- platform: mqtt
  name: 'relay16B-input1'
  unique_id: relay16B-input1
  state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
  value_template: '{{ value_json.input1.on }}'
  payload_on: 1
  payload_off: 0  

This way: the configuration checks OK, the input works but the output not.
If I add the switch to the configuration.yaml, the output works.

Out of curiosity: by adding a switch to configuration.yaml and still using the splitted configuration, will the switch.yaml be used by the system?

Your config is invalid. When you put a !include what actually happens is the contents of the file get shoved into that spot. So here’s your actual config after replacing the !include statements with the contents of the files:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#sensor: !include sensor.yaml
binary_sensor:
  - platform: mqtt
    name: 'relay16B-input1'
    unique_id: relay16B-input1
    state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
    value_template: '{{ value_json.input1.on }}'
    payload_on: 1
    payload_off: 0  
switch:
  mqtt:
    switch:
      - name: 'relay16b-output1'
        unique_id: relay16b-output1
        state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
        command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
        payload_on: '{"relay1":{"on":1}}'
        payload_off: '{"relay1":{"on":0}}'
        value_template: '{{ value_json.relay1.on }}'
        state_on: 1
        state_off: 0
  
  - platform: mqtt
    name: 'Output 2'
    unique_id: relay16B-2
    state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
    command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
    payload_on: '{"relay2":{"on":1}}'
    payload_off: '{"relay2":{"on":0}}'
    value_template: '{{ value_json.relay2.on }}'
    state_on: 1
    state_off: 0

See the problem? What’s under switch makes no sense. switch can’t be both a dictionary and an array.

mqtt is a top level key separate from switch. You have to re-arrange your config like this:

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
#sensor: !include sensor.yaml
binary_sensor:
  - platform: mqtt
    name: 'relay16B-input1'
    unique_id: relay16B-input1
    state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
    value_template: '{{ value_json.input1.on }}'
    payload_on: 1
    payload_off: 0  
switch: 
  - platform: mqtt
    name: 'Output 2'
    unique_id: relay16B-2
    state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
    command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
    payload_on: '{"relay2":{"on":1}}'
    payload_off: '{"relay2":{"on":0}}'
    value_template: '{{ value_json.relay2.on }}'
    state_on: 1
    state_off: 0
mqtt:
  switch:
    - name: 'relay16b-output1'
      unique_id: relay16b-output1
      state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
      command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
      payload_on: '{"relay1":{"on":1}}'
      payload_off: '{"relay1":{"on":0}}'
      value_template: '{{ value_json.relay1.on }}'
      state_on: 1
      state_off: 0

Feel free to replace any chunks of this with !include and move them out into other files. But you can’t really put new format mqtt entities and old format mqtt entities into the same file, that won’t work.

1 Like

Hi Mike, thank you for helping!
I don’t really get the problem… :blush:

Since it was not working, I tried the new format and saw that both ways are still valid so used them deliberately - but had no idea that they could not be mixed.

If I want to put these switches with the new format in a separate file, should it be mqtt.yaml and like this:

switch:
  - name: 'relay16b-output1'
    unique_id: relay16b-output1
    state_topic: 'relay16/2154d5e0aa46831378722ee8/state'
    command_topic: 'relay16/2154d5e0aa46831378722ee8/set'
    payload_on: '{"relay1":{"on":1}}'
    payload_off: '{"relay1":{"on":0}}'
    value_template: '{{ value_json.relay1.on }}'
    state_on: 1
    state_off: 0

Example from my config:

mqtt:
  sensor: !include_dir_merge_list cfg/_component/mqtt/sensor/
  binary_sensor: !include_dir_merge_list cfg/_component/mqtt/binary_sensor/
  switch: !include_dir_merge_list cfg/_component/mqtt/switch/
sensor: !include_dir_merge_list cfg/_component/sensor/
binary_sensor: !include_dir_merge_list cfg/_component/binary_sensor/

cfg/_component/mqtt/sensor/boiler_temperature.yaml


  - name: "Boiler Return"
    state_topic: "building/dhjm/eobs/99031647c54fff28"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['obs']['temperature']|float }}"
    device_class: temperature
    unique_id: onewire-cb6a6c2d-b7e1-4369-9088-dd559fd6e9e9

  - name: "Boiler Water Out"
    state_topic: "building/dhjm/eobs/99031647c54fff28"
    value_template: "{{ value_json['obs']['temperature']|float }}"
    unit_of_measurement: '°C'
    device_class: temperature
    unique_id: onewire-fa42e7c4-1e87-4622-900f-75f517061933

cfg/_component/mqtt/sensor/ruuvi.yaml


  - name: "Patio Temperature"
    state_topic: "dhjm/building/eobs/ruuvi/d5ac310fd9d4"
    unit_of_measurement: '°C'
    value_template: "{{ value_json['temperature'] | float }}"
    unique_id: ruuvi-5935f262-65ae-479c-a16d-5b499a5b341e
    device_class: temperature

  - name: "Patio Humidity"
    state_topic: "dhjm/building/eobs/ruuvi/d5ac310fd9d4"
    unit_of_measurement: '%'
    value_template: "{{ value_json['humidity'] | float }}"
    unique_id: ruuvi-ab8ad083-5efa-48a8-9158-ae918aaf35d5
    device_class: humidity
    
  - name: "Patio Air Pressure"
    state_topic: "dhjm/building/eobs/ruuvi/d5ac310fd9d4"
    unit_of_measurement: 'hPa'
    value_template: "{{ value_json['pressure'] | float }}"
    unique_id: ruuvi-134288e9-af9f-464f-addf-2f89a0b4018a
    device_class: pressure

I hope that gives you a bit more of an idea of how to split the files up.
I’ll add a switch example too:

cfg/_component/mqtt/switch/fr_switches.yaml


  - command_topic: dhjm/rfxgateway/command
    name: "Event Lights"
    optimistic: true
    payload_off: >
      {"device":"light","address":"AC/0x0002A95/1","data":"Off"}
    payload_on: >
      {"device":"light","address":"AC/0x0002A95/1","data":"On"}
    state_off: "Off"
    state_on: "On"
    state_topic: dhjm/rfxgateway/AC/0x0002A95/1
    unique_id: rf-4676eecb-fe7f-4919-a04f-f56be91ad1ee

  - command_topic: dhjm/rfxgateway/command
    name: "Garden Audio"
    optimistic: true
    payload_off: >
      {"device":"light","address":"AC/0x00030CE/4","data":"Off"}
    payload_on: >
      {"device":"light","address":"AC/0x00030CE/4","data":"On"}
    state_off: "Off"
    state_on: "On"
    state_topic: dhjm/rfxgateway/AC/0x00030CE/4
    unique_id: rf-ec4e648a-2927-4e2a-9d78-c7efc41f8cd2

  - command_topic: dhjm/rfxgateway/command
    name: "Keyring Alarm"
    optimistic: true
    payload_off: >
      {"device":"light","address":"AC/0x00006224/2","data":"Off"}
    payload_on: >
      {"device":"light","address":"AC/0x00006224/2","data":"On"}
    state_off: "Off"
    state_on: "On"
    state_topic: dhjm/rfxgateway/AC/0x00006224/2
    unique_id: rf-b8f166d7-c6ed-44fe-8a37-646ba11305ab

(Use https://www.uuidgenerator.net/ to generate your unique IDs - so that you can customise your entities in the UI)

1 Like

In the same file. You can have some switches in the new format and some in the old format just fine. Just can’t shove them into exactly the same file.

Assuming your configuration.yaml now includes this:

mqtt: !include mqtt.yaml

Then yes, that’s fine.

1 Like