Esp32 mqtt Button card not change status

Hi Dear members,

I’m struggle since days.
The setup:
esp32 with PubSubClient publishing mqtt msg
HomeAssistant

I have created an mqtt switch in the configuration.yaml.
I added a button on the dashboard, but it say as status “unknown” and not sending other then “off” mqtt msg.
but with mqtt explorer it is visible the esp32 send properly the feedback on the right topic.

This is my configuration.yaml:


mqtt:
  - switch:
      unique_id: switch.esp32_test_switch
      name: "esp32_test"
      state_topic: "eps32/garage/switch/status"
      command_topic: "esp32/garage/switch/set"
      availability:
        topic: "esp32/garage/switch/available"
        payload_available: "online"
        payload_not_available: "offline"
      payload_on: "1"
      payload_off: "0"
      state_on: "Open"
      state_off: "Closed"
      optimistic: false
      qos: 1
      retain: false

So I added a button and a entity switch like on the picture.
with this Optimistic: false setup the BUTTON only sends OFF payload. If I change the Optimistic to true, the BUTTON toggles between ON and OFF, but it is never show the real status(below the “Garage” title it is always unknown) what is sent by the esp32

And maybe I missed to mention an important thing. What I want to achive :slight_smile:
I would like to make a toggle switch of this like the picture above the “Garage relay”
and in case of I make a BUTTON of it I would see my custom state what is broadcasted from the esp32 like under the Heating Button “Off” but I would have have here instead of “Off” Closed or Opening or anything custom text.

That’s not a toggle switch. That’s specifically a switch which doesn’t have a state, so that you have to click on the “on” or “off” button.

What is the actual content of the eps32/garage/switch/status topic?
The way you used state_on / state_off, it must contain “Open” or “Closed” (case-sensitive)

Thank you koying for the reply.
the content of the esp32/garage/switch/status is “Open” or “Closed”, this is what I send through mqtt from the esp32. like above in my code. so it is identical
image

I have checked it with Mqtt explorer as well and it gives back Open or Closed.
in my configuration.yaml I also tried it with quotation marks and without them, but nothing changed

Is the topic retained?
Bottom-line is that HA cannot determine the actual state of your switch, for whatever reason.

in configuration.yaml I’ve tried with retain: true and also with false, but non of them made any change

The retain flag is set when you publish, so on the esp32 side for the status.

I see, let me try then change it in the esp32 code.
I’m very glad you are trying to help me!

right now I have to leave, but later today I will try it out and coming back with the result.

I’m struggle. I 've tried now different things, but still somehow the retain true cannot be achieved.
My code on the esp32:

client.publish(“esp32/garage/switch/status”, “Closed”, true);

on the receiver side I receive the message properly but somehow without the retain flage true :frowning:

and one additional ingredient to the story, I have created a sensor in the configuration.yaml:


mqtt:
  - switch:
      unique_id: switch.esp32_test_switch
      name: "mqtt_switch"
      state_topic: "eps32/garage/switch/status"
      command_topic: "esp32/garage/switch/set"
      availability:
        topic: "esp32/garage/switch/available"
        payload_available: "online"
        payload_not_available: "offline"
      payload_on: "1"
      payload_off: "0"
      #state_on: "Open"
      #state_off: "Closed"
      optimistic: false
      qos: 2
      retain: true

  - sensor:
      unique_id: sensor.esp32_test_signal
      name: "mqtt wifi strength"
      state_topic: "esp32/garage/switch/signal"

and it works as expected, from esp32 each 10 sec it send the WiFi signal strength. So my understandig the information from esp32 come through as it should be, but somehow on broker side I don’t handle it right in with the switch configuration.

Thank you koying for your support and you tried to help me, meanwhile I found out the issue. It is so unbelivable, I spent how many hours to investigate what is the issue and then the issue was A TYPO :smiley:
instead of “esp” I wrote “eps” in the configuration.yaml. in mqtt explorer and in mqtt listener I typed in write so I seen the communication is proper. It was so dumb error.


t

1 Like