Need help, first steps with MQTT on HA

Hello,

First al all, I want to introduce myself. My name is Eduard Hontele and I live in Antwerp, Belgium. Home assistant is new to me. After installing HA and the internal MQTT broker on a Raspberrypi 3B+, sending raw mqtt messages from a mqtt explorer on my desktop to the HA internal mqtt subscriber works fine.

I am doing these simple experiments to familiarise with HA and MQTT. It’s kind of the “Hello World” well known by every C programmer. The problem is starting alone. Nobody is there to lend me a hand in case of beginners worries. Now, I am loosing a lot of time seeking how to show a binary 0/1, received by the mqtt subscriber, in a card on the HA dashboard.

The ultimate goal of these basic experiments is to become the necessary skills for connecting home made, mqtt compatible devices, to HA. Is there anyone who has some free time to coach me on the road to become familiar with MQTT on HA.
Sincerely,

Eduard

Hi Eduard, welcome. General coaching is probably hard, but specific questions are often answered quickly. MQTT might not be the easiest way to get started though. What are you using to create the devices? ESP based? Maybe using ESPHome? that would be the path of choice. Using the HA api would be easier than mqtt.

but here we go with your first question.

As for binary values, the types of entities best suited are binary_sensor (just display, no direct means of changing it) or switch (that you can change), if you do not know what kind if device is switched.

Assuming binary_sensor, the values HA uses are on and off. They can be displayed in many ways, depending on the device class of the binary sensor.

This is how you define a binary sensor using mqtt. This is if you manually define them. HA does also allow autodiscovery if you send the right messge for it:

You’d need to define the payload for on and off as 1 and 0.

Once the entity is created, you can show it on the dashboard.

It is hard to start with generic concepts though, because if you start out using ESPHome, just experimenting with actual sensors may be way easier.

Hello Edwin,

Thanks for your very quick answer. I’ll soon take a look at: MQTT binary sensor - Home Assistant
Perhaps the details I’am struggling now are yet solved with your answer. I’ll keep you informed.

You are right saying ESPhome is easier. For sure I had no problem visualising the pushbutton on an ESP32 board in a panel on the HA dashboard. There exists an api to connect a Raspberry pico to HA isn’t it? I left ESPhome because I didn’t find the way to read binary data from another Rpi3 board.

Long before HA was created, there was some automation in our home. The system was build in 1993 and has been renewed several times. Now it is driven by the second Rpi3. If it is easier to read/write the digital data from this Rpi3, I’ll try that. The existing program is written by myself in standard K&R- C language. Because I didn’t find the ESPhome way to connect the old Rpi3 to the one carrying HA, I changed to MQTT. If a Rpi3 could be connected to ESPhome, I’ll surely try that.

Greetings,
Eduard

No, an RPI 3 is not a target system for ESPHome. Even though HA itself on that RPI is possible, GPI support is no longer part of core. You can only do that using custom integrations. I agree MQTT is a good fit for your use.

Hello Edwin,
Would you please have a look at the result of the things I recently changed. By my mind I am near to a living presentation on the dashboard.

  1. These lines were added to configuration.yaml:
mqtt:
  - binary_sensor:
      state_topic: "mqtt_binary_sensor_1 
  1. In services/mqtt a message with topic “mqtt_binary_sensor_1” is sent. The same thing can be done by mqtt-explorer. The internal mqtt subscriber in HA receives the 0/1 messages.

  2. This object has been added to the dashboard:
    Screenshot from 2024-08-05 10-25-52
    (Onbekend = Unknown)

The key problem stays; nothing changes in the dashboard when a “0” or a “1” payload is received.
The messag is not shown in the history of mqtt_binary_sensor_1.
Do you have an idea what I did wrong?

Sincerely,
Eduard

As per the documentation, the default payload should be “ON” or “OFF”. If you want to use 1 and 0 instead, you should indicate that in the definition by specifying payload_on and payload_off. You can also give it a nice friendly name:

mqtt:
  - binary_sensor:
      state_topic: "mqtt_binary_sensor_1"
      name: "My friendly name"
      payload_off: "0"
      payload_on: "1"

Hello Edwin,
A picture is worth thousands of words. I am pleased to show you the result:
Screenshot from 2024-08-05 13-37-37
Now the dashboard is showing the status after the “ON” payload has been transmitted.
The “bird” disappears after receiving the “OFF” message. The yellow background then becomes white.

I am wondering why there is a bold text “Onbekend” (= Unknown). Is it because the parameters for automatic retrieval are not set in the MQTT connection?

The reason why I lost so many time in this beginners exercise is doing it alone. I very happy with the help you have given. Now I am ready to go on with new exercises. One of the next will be putting several binary sensors on a dashboard card.

Thanks a lot,
Eduard

I’m Dutch too, so I’m not “Onbekend” with the Dutch translation. :wink:
What did you put in the entity card on the dashboard? Did you ask for a specific attribute?
If you put in an unknown attribute (instead of not asking for any attribute) you would get this result.


Hello Edwin,
Another problem has been solved. A little step for the world but a big one to me.

First, I’ll make a full copy of de Rpi SD card. After that I’ll go on making basic exercises with HA.
Greetings,

Eduard

Hello Edwin,

Changing state of mqtt_binary_sensor_1 works fine from a laptop connected to the local network.

Two users were defined in HA; “mqtt-user” and “mqtt-user2”. Both can be used with the same password.

Because this didn’t work with the old Rpi2, I borrowed an Rpi3B+. On this Rpi3B+, I installed the recent OS Bookworm Lite (without GUI) and mosquitto-clients.

Do you have any idea why the CONNECT from the Rpi3B+ is refused?

Sincerely,

Eduard

PS: This problem has been solved by elimination. First I replaced the OS Bookworm Lite by the full version to have a GUI. That didn’t help. Trying to install the MQTT-explorer client didn’t succeed on the Rpi. (no stable version for the ARM but a stable version exists for other CPU’s)
After changing the commandline to mosquitto_pub -d -u ‘mqtt-user’ -P ‘my_password’ -h 192.168.1.135 -t ‘mqtt_binary_sensor_1’ -m ‘ON’ everything works fine.

I have no idea why double quotes worked on a Dell laptop but not on a Rpi.
Maybe this information can be useful for others.