Long setup time for MQTT integration

The following script (script.create_sensors) contains five service calls. Each service call is an mqtt.publish creating a sensor via MQTT Discovery. You can easily see all of the sensor information you provided is included in the payload of each mqtt.publish call.

The payloads are published as retained messages which means they will be stored on the MQTT broker; their configuration information will be instantly available to Home Assistant on startup. You don’t have to run the script more than once (unless you want to change the configuration of one or more of the five sensors).

In addition to creating the five sensors, it groups them together within a single device called ESP9. This is only possible with MQTT Discovery and cannot be done with manual MQTT configuration. I’ve included this in the script as a convenience so when you go to Configuration > Devices > ESP9, you will see all the sensors related to it.

IMPORTANT

Do not execute this script before you first remove your existing five MQTT sensors from your system. If you don’t remove them and run this script, you will create duplicated sensors.

I suggest you delete, or comment out, the existing 5 sensor configurations then execute Configuration > Server Controls > Reload Manually Configured MQTT Entities to purge them from Home Assistant. Then run the script and they should all re-appear but this time they will be created via MQTT Discovery.

For more information about MQTT Discovery, refer to its documentation.

Show script
script:
  create_sensors:
    alias: "Create sensors via MQTT Discovery"
    sequence:
      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/temperatura_dnevna/config
          retain: true
          payload: >
            {
              "name": "Temperatura Dnevna",
              "unique_id": "esp9_temperature_dneva",
              "device_class": "temperature",
              "unit_of_measurement": "°C",
              "state_topic": "home/esp9/dnevna",
              "value_template": "{{ value_json.temperatura }}",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }

      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/vlaznost_dneva/config
          retain: true
          payload: >
            {
              "name": "Vlažnost Dnevna",
              "unique_id": "esp9_vlaznost_dneva",
              "unit_of_measurement": "%",
              "state_topic": "home/esp9/dnevna",
              "value_template": "{{ value_json.vlaznost }}",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }

      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/datum_dneva/config
          retain: true
          payload: >
            {
              "name": "Datum Dnevna",
              "unique_id": "esp9_datum_dneva",
              "state_topic": "home/esp9/dnevna",
              "value_template": "{{ value_json.datum }}",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }

      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/cas_dneva/config
          retain: true
          payload: >
            {
              "name": "Čas Dnevna",
              "unique_id": "esp9_cas_dneva",
              "state_topic": "home/esp9/dnevna",
              "value_template": "{{ value_json.cas }}",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }

      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/senzor_dneva/config
          retain: true
          payload: >
            {
              "name": "Senzor Dnevna",
              "unique_id": "esp9_senzor_dneva",
              "state_topic": "home/esp9/pinstatus",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }

      - service: mqtt.publish
        data:
          topic: homeassistant/sensor/status_dneva/config
          retain: true
          payload: >
            {
              "name": "Status Dneva",
              "unique_id": "esp9_status_dneva",
              "state_topic": "home/esp9/dummy",
              "device": {
                  "identifiers": ["esp9"],
                  "name": "ESP9",
                  "model": "ESP",
                  "manufacturer": "Espressif",
                  "sw_version": "1.XX"
              }
            }
2 Likes

Great, thx a lot…
I’ve tried it already with publishing a message to my broker (directly in MQTT Explorer) in this way:

Published to topic:
homeassistant/sensor/esp9/temperatura/config

json payload:

{
  "availability": [
    {
      "topic": "zigbee2mqtt/bridge/state"
    }
  ],
  "device": {
    "identifiers": [
      "esp9"
    ],
    "manufacturer": "Espresiff",
    "model": "ESP8266",
    "name": "Dnevna",
    "sw_version": "1.0"
  },
  "enabled_by_default": true,
  "icon": "mdi:gesture-double-tap",
  "json_attributes_topic": "home/esp9/dnevna",
  "name": "Temperatura Dnevna",
  "state_topic": "home/esp9/dnevna",
  "unique_id": "esp9_temperatura_dnevna",
  "value_template": "{{ value_json.temperatura }}"
}

I based it on config topics of other devices, which have discovery enabled (IKEA and Aqara). But nothing happened… I could see the topic briefly appear in MQTT, but disappeared a few milliseconds later - no error returned. Obviously something was wrong, just no idea what.

So, I’ll try with your scripts and I’ll take it really slow, step by step and device by device, to make sure, everything works OK :). Will report results.

I want to caution you again about duplication. Your latest attempt to create a sensor via MQTT Discovery used the same name as one of your existing sensors. Unless you have already removed a sensor that’s manually configured, don’t attempt to create a duplicate of it via MQTT Discovery.

Although you can use MQTT Explorer to publish a payload to a topic, you can also use Developer Tools > Services:

Here’s a uniquely named sensor you can try to create via MQTT Discovery. Just paste the following YAML into Developer Tools > Services and click Call Service. Then check Configuration > Entities, or Developer Tools > States, to confirm it was created.

service: mqtt.publish
data:
  topic: homeassistant/sensor/temperatura_dnevna_test/config
  retain: true
  payload: >
    {
      "name": "Temperatura Dnevna Test",
      "unique_id": "esp9_temperatura_dnevna_test",
      "state_topic": "home/esp9/dnevna",
      "value_template": "{{ value_json.temperatura }}",
      "device_class": "temperature",
      "unit_of_measurement": "°C",
      "icon": "mdi:gesture-double-tap",
      "device": {
          "identifiers": ["esp9"],
          "name": "ESP9",
          "model": "ESP8266",
          "manufacturer": "Espressif",
          "sw_version": "1.0"
      }
    }
1 Like

Well, this one doesn’t work for me…

When trying to publish, I get an error, that “value_json” is undefined. I guess the service understands, that it should pass the template {{ value_json.temperatura }} as a part of payload, which of course has no value in this context, so it returns an error.

So I’ll have to do it with scripts or in MQTT explorer…

Sorry about that; my mistake. Home Assistant’s Jinja2 interpreter attempts to evaluate this template

{{ value_json.temperatura }}

before the payload is published. We don’t want the jinja2 interpreter to evaluate it so we wrap the template in raw/endraw tags as shown below:

service: mqtt.publish
data:
  topic: homeassistant/sensor/temperatura_dnevna_test/config
  retain: false
  payload: |
    {
      "name": "Temperatura Dnevna Test",
      "unique_id": "esp9_temperatura_dnevna_test",
      "state_topic": "home/esp9/dnevna",
      "value_template": {% raw %}"{{ value_json.temperatura }}"{% endraw %},
      "device_class": "temperature",
      "unit_of_measurement": "°C",
      "icon": "mdi:gesture-double-tap",
      "device": {
          "identifiers": ["esp9"],
          "name": "ESP9",
          "model": "ESP8266",
          "manufacturer": "Espressif",
          "sw_version": "1.0"
      }
    }

Here’s the result after I click Call Service and publish a JSON payload, containing the temperature, to home/esp9/dnevna.

The sensor’s appearance in the UI:
Screenshot from 2021-11-12 16-43-42

3 Likes

Yay, it works. Had to publish 130 pre-configured payloads (a couple of hours clicking, typing and double-checking, that the entity names will be the same, as they were before :slight_smile: ), but now I have them all covered. Loading time for MQTT reduced from 68 to 5.9 seconds :).

Now, the new issue I have is, that some entities are missing as I somehow managed to delete two branches of existing config topics from Aqara and Ikea in MQTT Explorer while comparing existing and new entities and will most likely have to re-pair those devices (“only” 17 of them :slight_smile: ) to have the entities discovered again. I’m not sure, but maybe they even got deleted by themselves when I enabled and again disabled discovery on the MQTT integration while testing (and additionally deleted all integration related devices and entities).

But apart from that, happy happy :).

Thx a lot once again @123 for your help.

1 Like

This is great. Thought that all this is only possible from “outside” but not already with mqtt.publish. With that it’s even more curios, that the device creations/assignments are not possible via yaml, neither old nor new way. Personally I like(d) the yaml way, but this is more or less the same, but with devices.

Is there anything, which I would not be able to do via the discovery way?

And secondly, for what are the identifiers of the device?

Very useful post! Thanks

1 Like

I’m curious how everyone is finding integration load times now in HA 2024.2.1?
My system takes about 90 seconds from restart back to ready.

image

image