Trouble in getting mqtt device/entity

Hi dudes,

I’m a newbie, giving frist steps in HA’s matters. At this point I’m struglling to setup up my shelly devices throw MQTT protocol. I’ve intalled mosquito brocker and mqtt’s integration and configured out one of my shellies (in this instance i’m using a Shelly plus 1pm gen2). I 've checked thow MQTT explorer that my shelly device is communication to my brocker, sending at every minute info. At this point I’ve got 2 issues: 1st one MQTT auto discovery is not working as it’s not found my shelly (using the Shelly integration, the relay is detected with no problom), the 2nd one when trying create the yaml file to provide the entity into my HA, i’m facing some problem which are beyound my understading…

  • I’ve created a yaml proper file named mqtt.yaml with this code:
- switch:
    unique_id: ILdeck
    name: "Luz Jardim - deck"
    state_topic: "ILdeck/status/switch:0"
    command topic: "ILdeck/command/switch:0"
    payload_on: "ON"
    payload_off: "OFF"
    state_on: "ON"
    state_off: "OFF"
    QoS: 1
    retain: false 
  • in configuration.yaml file I’ve included this code line:
    mqtt: !include mqtt.yaml

After this I’ve got the following error:

Home Assistant detected an invalid config for a manually configured item.

Platform domain: switch
Configuration file: /config/mqtt.yaml
Near line: 2
Configuration found:

unique_id: ILdeck
name: Luz Jardim - deck
state_topic: ILdeck/status/switch:0
command topic: ILdeck/command/switch:0
payload_on: 'ON'
payload_off: 'OFF'
state_on: 'ON'
state_off: 'OFF'
QoS: 1
retain: false

Error: extra keys not allowed @ data[‘command topic’].

I really appreciate any kind of support in order move forward.

thanks in advance.

Add an underscore between the words command and topic.

command_topic: "ILdeck/command/switch:0"

Reference

MQTT Switch command_topic


In addition, change QoS to qos. Options are in lower case. Similarly, the value of unique_id should be a slug (which means lower case letters and numbers and underscores only). Refer to the examples in the MQTT Switch documentation.

Hey @123

thanks for your prompt reply. Never thought in a syntax issue. I’ve just got another issue: just realise that my shellies is publishing in json format, my question here is how specify that in my yaml file.

Blockquote

Ldeck

▼events

▼status

switch:0 = {“id”:0, “source”:“WS_in”, “output”:true, “apower”:146.6, “voltage”:243.0, “current”:0.642, “aenergy”:{“total”:36774.973,“by_minute”:[2432.608,2436.372,2441.390],“minute_ts”:1761436860},“temperature”:{“tC”:52.3, “tF”:126.1}}

input:0 = {“id”:0,“state”:false}

I identify the highlighted vars as being the ones i need control…

This should work for receiving the device’s current state.

state_topic: ILdeck/status/switch:0
value_template: "{{ 'ON' if value_json.output is true else 'OFF' }}"

However, for sending a command to the device, I first need to know the command’s format. What does the device expect to receive via the topic ILdeck/command/switch:0?

command_topic: "ILdeck/command/switch:0"
command_template: <to be determined>

Hi mate, huge thanks! the status is working perfectly. Regarding the command. I gave a command using the shelly official integration and what shelly pusblish into the broker is the changing of output var from false to true.

At input:0 one I’ve connect a lighting sensor. don’t need integration with HA propely.

@123 any thoughts mate?

If you’re saying that is the command to turn on the device:

{"output":true}

Then try this:

command_topic: "ILdeck/command/switch:0"
command_template: "{{ {'output': value|bool} | to_json }}"

hey,

I’ve tried the solution you suggest me however the entired string is replaced by the boolean when I shoot the commad. Is there any way of replacing only the the output var? I tested a json string like the one you provided me for the status but the only thing I got it was a parsing template error.

btw. My conclusion concerning the command topic seems to be the same that the status one, once it’s the only var where i can see changings when turn on/off the command (using the official shelly integration for that). Make any sense?

Thanks a lot once again for you time.

According to Shelly’s MQTT documentation, the command’s format is simply a lowercase string:
on, off, toggle.

command topic: ILdeck/command/switch:0
payload_on: 'on'
payload_off: 'off'

.

1 Like

That’s it mate. Thanks a lot. Just a note for whoever can be following the topic: it’s needed change the on/off at state template to lowercase otherwise it’s not possible getting the state from Shelly.