Updated MQTT code for Home Assistant - Examples?

Home Assistant has changed the way MQTT Cover works… I had my garadgets working through MQTT, and I updated my configuration.yaml code to the new format… I seem to have lost how to create the cover for my garage doors, and now I can’t access them through my UI.

Does anyone have updated configuration.yaml example code that they are using with garadget and Home Assistant that they could share?

Thanks.

Update: I was able to sort my issue… I was confusing the Home Assistant YAML parser… I use the code below in my configuration.yaml file, and it is working great. I have my garadgets set up for MQTT communication. I have the cover, “door since” and “door brightness” entities on my lovelace dashboard/UI for each of the garage doors.

mqtt:
cover:
- name: “Large Garage Door”
device_class: ‘garage’
command_topic: “garadget/lgarage/command”
state_topic: “garadget/lgarage/status”
value_template: >-
{% if value_json.status == ‘closed’ %}
closed
{% else %}
open
{% endif %}
payload_open: “open”
payload_close: “close”
payload_stop: “stop”

- name: "Small Garage Door"
  device_class: 'garage'
  command_topic: "garadget/sgarage/command"
  state_topic: "garadget/sgarage/status"
  value_template: >-
    {% if value_json.status == 'closed' %}
      closed
    {% else %}
      open
    {% endif %}
  payload_open: "open"
  payload_close: "close"
  payload_stop: "stop"   

sensor:
- name: “Large Garage Door Since”
state_topic: “garadget/lgarage/status”
value_template: ‘{{ value_json.time }}’

- name: "Large Garage Door Brightness"
  state_topic: "garadget/lgarage/status"
  unit_of_measurement: '%'
  value_template: '{{ value_json.bright }}'      

- name: "Small Garage Door Since"
  state_topic: "garadget/sgarage/status"
  value_template: '{{ value_json.time }}'

- name: "Small Garage Door Brightness"
  state_topic: "garadget/sgarage/status"
  unit_of_measurement: '%'
  value_template: '{{ value_json.bright }}'
2 Likes

Hello,

I can’t get this example to work with MQTT and HA.
Can some one post his working configuration? Maybe, well formated? This would be very helpful for me.

Thank you in advance.

Regards,

Michael

Here is the code I put into my HA configuration.yaml.

mqtt:
  cover:
    - device_class: 'garage'
      unique_id: "298u9823983249hd928"
      name: "<friendly_name>"
      command_topic: "garadget/<name_created_during_setup>/command"
      state_topic: "garadget/<name_created_during_setup>/status"
      value_template: >-
        {% if value_json.status == 'closed' %}
          closed
        {% else %}
          open
        {% endif %}
      payload_open: "open"
      payload_close: "close"
      payload_stop: "stop"
  sensor:
    - name: "In state for"
      state_topic: "garadget/<name_created_during_setup>/status"
      value_template: '{{ value_json.time }}'
    - name: "Brightness"
      state_topic: "garadget/<name_created_during_setup>/status"
      unit_of_measurement: '%'
      value_template: '{{ value_json.bright }}'
    - name: "Reflection percent"
      state_topic: "garadget/<name_created_during_setup>/status"
      unit_of_measurement: '%'
      value_template: '{{ value_json.sensor }}'
    - name: "WiFi power"
      state_topic: "garadget/<name_created_during_setup>/status"
      unit_of_measurement: 'dB'
      value_template: '{{ value_json.signal }}'
    - name: "WiFi SSID"
      state_topic: "garadget/<name_created_during_setup>/config"
      value_template: '{{ value_json.ssid }}'

I’m no Home Assistant expert and the whole process took me quite a lot of digging and googling and piecing things together. To that end, I’ll try and write it all up here, and hopefully anyone else trying to get Garadget running with MQTT and HA will have an easier time.

First things first, you need to set up a local user login in the Mosquitto Broker add-on. Settings->Add-ons->Mosquitto broker->Configuration (tab at the top). You’ll need to add a user under logins. You can add a username and password as plain text:

- username: your_username
  password: your_insecure_visible_password

However, I didn’t like the idea of a plain text password sitting there visible and there are instructions that say “You can also optionally set a password value using the hashed password obtained from the pw command (which is present inside the Mosquitto container). If doing so, you must also specify password_pre_hashed: true alongside the username and password values.” Using the command below:
pw -p "<password>"
(yes, leave the quotes)
This will generate a hashed password that you can copy into the password field. Seemed simple enough, but I didn’t know how to run this command in the “Mosquitto container.”

In order to be able to run the command, you need to have the add-on “Advanced SSH & Web Terminal” installed and the “Protection mode” toggle on the “Info” tab turned off, then the add-on restarted. I got a nice, red warning about protection mode being turned off at the top of the add-on. Launch Terminal and then run the command:

docker ps

Example output:

CONTAINER ID   IMAGE                                                          COMMAND                  CREATED         STATUS                  PORTS                                                                                                                          NAMES
6e7ae15c6cfc   ghcr.io/home-assistant/generic-x86-64-homeassistant:2024.5.4   "/init"                  3 hours ago     Up 3 hours                                                                                                                                             homeassistant
ac2752f3f0ab   homeassistant/amd64-addon-mosquitto:6.4.0                      "/init"                  5 hours ago     Up 5 hours              0.0.0.0:1883-1884->1883-1884/tcp, :::1883-1884->1883-1884/tcp, 0.0.0.0:8883-8884->8883-8884/tcp, :::8883-8884->8883-8884/tcp   addon_core_mosquitto
a0a77ecfa6d8   ghcr.io/hassio-addons/ssh/amd64:17.3.0                         "/init"                  5 hours ago     Up 5 hours                                                                                                                                             addon_a0d7b954_ssh

Find the container ID of the image containing amd64-addon-mosquitto. Now you can run that pw command as such:

docker exec <container_id> pw -p "<password>"   

Example output:

docker exec ac2752f3f0ab pw -p "test"
PBKDF2$sha512$100000$KDMIksYvx0Yv/tFmr3pR8Q==$grB05mGMFZumWBiBubfK+iNKV0u+ou7xc4GS9EC7r0ghvzRJTq8uffyIPoKbQv2FBydR1+Zr3f2HXTB1tDS+UA==

Copy that hashed string into the Mosquitto broker Configuration login field for the password.

Example:

- username: your_username
  password: PBKDF2$sha512$100000$KDMIksYvx0Yv/tFmr3pR8Q==$grB05mGMFZumWBiBubfK+iNKV0u+ou7xc4GS9EC7r0ghvzRJTq8uffyIPoKbQv2FBydR1+Zr3f2HXTB1tDS+UA==
  password_pre_hashed: true

Hit save on this. I noticed that it modified the password line to put the entire password on the next line.

At this point, you can finally connect your Garadget device to MQTT [Original instructions]. Hold the M button until it slow flashes dark blue, connect to the Garadget WiFi (PHOTON-XXXX) and open 192.168.0.1 in your browser.

Select your WiFi network, give it the WiFi password. I opted to use the cloud and MQTT. Give it the IP of the MQTT broker (your Home Assistant IP) and port 1883 (assuming you’ve made no changes to the default). Give it the username and password that you set up in the Mosquitto broker Configuration. Save that and hopefully Garadget connects to WiFi and the light turns solid cyan. On a few occasions during my testing I had to repeat all these steps a second time before I got connected.

You can paste the code from the top into the configuration.yaml file, save it, reload the YAML configuration under developer tools, and you’ll have a garage door that you can open, close, and get some stats on.

I had initially tried setting up Garadget’s MQTT connection using a Home Assistant username and password I had set up in HA’s People because a lot of documentation and forum posts seemed to indicate that you could log into Mosquitto broker with a Home Assistant username and password. When I checked the Mosquitto broker logs, I would see that my Garadget was trying to connect but it was unauthorized. Going through all the steps above finally got Garadget visible and operable in Home Assistant.

I hope this can help the next person trying to work this all out.