MQTT Switch integration error

Hello community members,

I’m facing a configuration issue with setting up an MQTT switch in Home Assistant and I’m hoping to get some assistance from you all.

I have meticulously followed the official documentation to configure the MQTT switch, but I’m encountering an error that I’ve been unable to resolve. I’ve checked and rechecked my configuration multiple times, and it appears to align with the requirements provided in the documentation.

Here’s the relevant section from my configuration.yaml:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

influxdb:
  host: 192.168.178.44
  port: 8086
  database: openhab_db
  username: grafana
  password: xxx

mqtt:
  switch:
    - name: "Virtual Bathroom Switch"
      unique_id: virtual_switch_bathroom
      state_topic: "tele/shelly_badetimmer_decke_D4FB9F/SENSOR"
      availability_topic: "tele/shelly_badetimmer_decke_D4FB9F/LWT"
      state_on: "ON"
      state_off: "OFF"
      qos: 0
      retain: true
      value_template: "{{ value_json.Switch1 }}"

However, despite my efforts, I’m consistently encountering the following errors:

Logger: homeassistant.components.homeassistant
The system cannot restart because the configuration is not valid: Error loading /config/configuration.yaml: mapping values are not allowed here in "/config/configuration.yaml", line 21, column 9
Logger: homeassistant.components.websocket_api.http.connection
[2789329968] The system cannot restart because the configuration is not valid: Error loading /config/configuration.yaml: mapping values are not allowed here in "/config/configuration.yaml", line 21, column 9

I’m puzzled by this situation as I believe I’ve adhered to the provided documentation. Could someone kindly review my configuration and offer any insights or suggestions on what might be causing these errors?

I sincerely appreciate your time and help. Thank you in advance!

When I get in a situation like that I try commenting / removing stuff until the error goes away - to identify exactly what line it is unhappy with.

mqtt:
  - switch:
      name: etc...

Thanks @tom_l

But still errors :frowning:

mqtt:
  - switch:
    name: "Virtueller Schalter Badezimmer"
    unique_id: virtual_switch_Badezimmer
    state_topic: "tele/shelly_badetimmer_decke_D4FB9F/SENSOR"
    availability_topic: "tele/shelly_badetimmer_decke_D4FB9F/LWT"
    state_on: "ON"
    state_off: "OFF"
    qos: 0
    retain: true
    value_template: "{{ value_json.Switch1 }}"

Logger: homeassistant.components.homeassistant
Source: components/homeassistant/__init__.py:170
Integration: Home Assistant Core Integration (documentation, issues)
First occurred: 21:40:38 (3 occurrences)
Last logged: 21:51:11

The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': {'name': 'bathroom', 'state_topic': 'home/bathroom/gpio/13', 'command_topic': 'home/bathroom/gpio/13', 'payload_on': '1', 'payload_off': '0'}}]. (See /config/configuration.yaml, line 32).
The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': {'name': 'bathroom', 'state_topic': 'home/bathroom/gpio/13', 'command_topic': 'home/bathroom/gpio/13', 'payload_on': '1', 'payload_off': '0'}}]. (See /config/configuration.yaml, line 41).
The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': None, 'name': 'Virtueller Schalter Badezimmer', 'unique_id': 'virtual_switch_Badezimmer', 'state_topic': 'tele/shelly_badetimmer_decke_D4FB9F/SENSOR', 'availability_topic': 'tele/shelly_badetimmer_decke_D4FB9F/LWT', 'state_on': 'ON', 'state_off': 'OFF', 'qos': 0, 'retain': True, 'value_template': '{{ value_json.Switch1 }}'}]. (See /config/configuration.yaml, line 18).

You still have not indented it correctly. Look at my example again. Name is not inline with switch.

Hello everyone,

Firstly, thank you to all, especially @tom_l, for the support and advice. I’ve implemented the changes you’ve suggested and correctly indented the code:


mqtt:
  - switch:
      name: "Virtueller Schalter Badezimmer"
      unique_id: virtual_switch_Badezimmer
      state_topic: "tele/shelly_badetimmer_decke_D4FB9F/SENSOR"
      availability_topic: "tele/shelly_badetimmer_decke_D4FB9F/LWT"
      state_on: "ON"
      state_off: "OFF"
      qos: 0
      retain: true
      value_template: "{{ value_json.Switch1 }}"

However, the issue persists and I continue to receive error messages indicating an invalid format in my MQTT configuration.

Logger: homeassistant.components.websocket_api.http.connection
Source: components/homeassistant/__init__.py:181
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 16:04:14 (1 occurrences)
Last logged: 16:04:14

[2541822224] The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': {'name': 'Virtueller Schalter Badezimmer', 'unique_id': 'virtual_switch_Badezimmer', 'state_topic': 'tele/shelly_badetimmer_decke_D4FB9F/SENSOR', 'availability_topic': 'tele/shelly_badetimmer_decke_D4FB9F/LWT', 'state_on': 'ON', 'state_off': 'OFF', 'qos': 0, 'retain': True, 'value_template': '{{ value_json.Switch1 }}'}}]. (See /config/configuration.yaml, line 18).
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 205, in handle_call_service
    await hass.services.async_call(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1910, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1950, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 887, in admin_handler
    await result
  File "/usr/src/homeassistant/homeassistant/components/homeassistant/__init__.py", line 181, in async_handle_core_service
    raise HomeAssistantError(
homeassistant.exceptions.HomeAssistantError: The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': {'name': 'Virtueller Schalter Badezimmer', 'unique_id': 'virtual_switch_Badezimmer', 'state_topic': 'tele/shelly_badetimmer_decke_D4FB9F/SENSOR', 'availability_topic': 'tele/shelly_badetimmer_decke_D4FB9F/LWT', 'state_on': 'ON', 'state_off': 'OFF', 'qos': 0, 'retain': True, 'value_template': '{{ value_json.Switch1 }}'}}]. (See /config/configuration.yaml, line 18).
Logger: homeassistant.components.homeassistant
Source: components/homeassistant/__init__.py:170
Integration: Home Assistant Core Integration (documentation, issues)
First occurred: 16:04:14 (1 occurrences)
Last logged: 16:04:14

The system cannot restart because the configuration is not valid: Invalid config for [mqtt]: expected dictionary for dictionary value @ data['mqtt']. Got [{'switch': {'name': 'Virtueller Schalter Badezimmer', 'unique_id': 'virtual_switch_Badezimmer', 'state_topic': 'tele/shelly_badetimmer_decke_D4FB9F/SENSOR', 'availability_topic': 'tele/shelly_badetimmer_decke_D4FB9F/LWT', 'state_on': 'ON', 'state_off': 'OFF', 'qos': 0, 'retain': True, 'value_template': '{{ value_json.Switch1 }}'}}]. (See /config/configuration.yaml, line 18).

Could someone perhaps take another look and provide further insights? It would be greatly appreciated. I’m grateful for any assistance.

Thank you in advance!

Which version of home assistant are you running?

Hi @tom_l,

Thank you for your quick response. I’m running Home Assistant in a Docker container with the version 2023.6.1 and the Frontend version is 20230608.0 - latest.

Best regards,

Ok that’s the problem. The new mqtt format was released in a later version.

You need to update.

EDIT: hmm. No the changes were actually released in 2022.12.

Hi @tom_l,

Oh, that’s intriguing! I’ve come across similar error messages in older forum threads, which is why the idea of not being on the latest version didn’t cross my mind. I’ll go ahead and install the latest version and circle back with an update.

Thank you for pointing that out!

Yeah, no I was wrong. I just edited my post. Updating to the latest version couldn’t hurt though.

Hi @tom_l,

I appreciate your clarification. I’ve gone ahead and updated to Home Assistant version 2023.8.4 (Frontend 20230802.1 - latest), but unfortunately, the issue persists.

Additionally, I tried listening to the topic via the web interface, and that seems to be working fine (as per the attached screenshot).

I should also mention that I added the broker through the MQTT integration under Settings > Devices & Services. However, I presume that shouldn’t be a cause for this issue, right?
Given that this wasn’t resolved by the update and that the topic can be accessed through the web interface, I’m even more puzzled about what might be causing this. Any other ideas or insights?

Hello Community,

Thank you all for your suggestions and assistance. I’ve managed to find a solution to the issue myself. It turns out that the problem was the missing command_topic. Initially, I hadn’t provided a command_topic because my intention was only to read the state of the Shelly switch without wanting to control the relay. It seems like I stumbled upon a rather edge case scenario.

Although the documentation states that the command_topic is optional, it seems that in practice, its absence caused the issue. I’m not sure if I misunderstood this aspect of the documentation or if the documentation does not accurately reflect the program’s behavior in this instance.

For future reference or if anyone else encounters a similar situation, here’s the updated configuration:

mqtt:
  - switch:
      name: "Virtueller Schalter Badezimmer"
      unique_id: virtual_switch_Badezimmer
      value_template: "{{ value_json.Switch1 }}"
      state_topic: "tele/shelly_badetimmer_decke_D4FB9F/SENSOR"
      state_on: "ON"
      state_off: "OFF"
      command_topic: "home/demo/demo/set" # This was the missing part
      availability_topic: "tele/shelly_badetimmer_decke_D4FB9F/LWT"
      payload_available: "Online"
      payload_not_available: "Offline"
      qos: 0
      retain: true

Again, thank you for your support. I hope this can help others in the future.

1 Like

Looking at the code, the command topic is required for a MQTT switch so yes, the documentation is wrong.

# Switch type platforms publish to MQTT and may subscribe
MQTT_RW_SCHEMA = MQTT_BASE_SCHEMA.extend(
    {
        vol.Required(CONF_COMMAND_TOPIC): valid_publish_topic,
        vol.Optional(CONF_OPTIMISTIC, default=DEFAULT_OPTIMISTIC): cv.boolean,
        vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
        vol.Optional(CONF_STATE_TOPIC): valid_subscribe_topic,
    }
)

I have submitted a correction.

2 Likes