Adding a new mqtt switch, does not how up as entity

Hi!

I have this

configuration.yaml

...
mqtt: !include yaml/mqtt/mqtt.yaml
...

Then the MQTT yaml file

switch:
  # mFi mPower
  - name: Gaming Headsets Charger
    unique_id: "mpower_gaming_headsets_charger"
    state_topic: "mFi/mPower/port1/relay"
    command_topic: "mFi/mPower/port1/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 2
    retain: true
  - name: Empty mPower
    unique_id: "mpower_empty_mpower"
    state_topic: "mFi/mPower/port2/relay"
    command_topic: "mFi/mPower/port2/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 2
    retain: true

I need to add another mqtt switch, I added, the file looks like this

switch:
  # mFi mPower
  - name: Gaming Headsets Charger
    unique_id: "mpower_gaming_headsets_charger"
    state_topic: "mFi/mPower/port1/relay"
    command_topic: "mFi/mPower/port1/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 2
    retain: true
  - name: Empty mPower
    unique_id: "mpower_empty_mpower"
    state_topic: "mFi/mPower/port2/relay"
    command_topic: "mFi/mPower/port2/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 2
    retain: true
  - name: Google Home Computer
    unique_id: "mpower_google_home_computer"
    state_topic: "mFi/mPower/port3/relay"
    command_topic: "mFi/mPower/port3/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 2
    retain: true

I have restarted home assistant, the new entity does not show up…
I am missing something?

Clear cache or using another browser, does not solve the issue

Thanks!

Hello PickOne,

Troubleshooting MQTT? MQTT-Explorer can help you be successful!
If you have Add-ons available, try adding this Add-on repository to your Add-on list. GitHub - GollumDom/addon-repository.
With this you can see that the broker sees and likely find the bug.
HINT: I have found using this, if you look at the JSON string and it is in color, the JSON is valid. If it is monochrome, the JSON is invalid and it will not work.

I don’t see anything wrong with the switch

Off topic, but

retains the command, not the state, which might have unintended effects.

The thing is, these switches are not discoverable, I can only add them through YAML. However, when I do that, the entity ID doesn’t show up after restarting Home Assistant. I’ve tried using MQTT Explorer, but that didn’t help either.

So… wait, wait, wait… All this time I thought that retain is the option to keep the state known, after restarting… It is not this? If I add retain, it will trigger a command after restart, putting the switch in the same state as before restarting? I am trying to understand it 100%

This. Might or might not be wanted. To retain the state, the device needs to set the retain flag.

As long as the state is only ever set from HA, it makes no difference. But if you can set the state on the device/other means, it does. E;g.hardwired relays running Tasmota that have a switch or button to toggle/switch on/off the relay. If the last command send with retain flag was to switch the relay off, but the physical switch turned the relay on afterwards, the state is off, but on a HA restart HA will send the last command (because retained) which may or may not be desired.

This explain my other issue I had before, not knowing why two switches were turning back on, after I removed retain, all good…

And, what about the mqtt issue, do you know sometihng? Maybe it is a bug in this new HA update? :frowning:

I tried it out



Did have to shift everything 2 spaces to the right though (but could be because of my mqtt.yaml)

But when I add two spaces, the editor delete them :frowning:

Let me guess, vscode ?

Studio Code Editor, yes

Soo… I need those two spaces? But only to mqtt yaml? On other yamls I don’t have this issue…

I don’t know how you configured your includes, but if your first 2 switches work the 2 spaces won’t be the problem.
Are you sure the 3rd switch can’t be found ?

This is what I used

mqtt: !include yaml/mqtt/mqtt.yaml

100% not showing.

The thing is that in the past, I had like this

switch:
  # mFi mPower
  - name: Gaming Headsets Charger
    unique_id: "mpower_gaming_headsets_charger"
    state_topic: "mFi/mPower/port1/relay"
    command_topic: "mFi/mPower/port1/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true
  - name: Power Light Charger
    unique_id: "power_light_charger"
    state_topic: "mFi/mPower/port2/relay"
    command_topic: "mFi/mPower/port2/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true

Then I changed “Power Light Charger” to “Empty mPower” (the last switch)

switch:
  # mFi mPower
  - name: Gaming Headsets Charger
    unique_id: "mpower_gaming_headsets_charger"
    state_topic: "mFi/mPower/port1/relay"
    command_topic: "mFi/mPower/port1/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true
  - name: Empty mPower
    unique_id: "mpower_empty_mpower"
    state_topic: "mFi/mPower/port2/relay"
    command_topic: "mFi/mPower/port2/relay/set"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true

I have deleted the old entity, and the new one does not appear…

I will do a test now, writing directly to configuration.yaml…

Guess, what? :smiley: directly to configuration.yaml, works

My question now is:

how can I add all the switches into one file and all the sensors to another files, what I need to use for include, to include all the yaml files of mqtt, from one single folder? No matter if is switch of sensor, having them in separate files

I don’t understand how is the split configuration for mqtt…

Ok, I did it, finally. It was because of the split configuration, of course…

Thanks!