Cannot get MQTT Discovery working

Hi!

I am trying a bit to get MQTT Discovery working, I downloaded a python script (GitHub - greenMikeEU/SmartMeterEVNKaifaMA309: Dieses Projekt ermöglicht es den Smartmeter der EVN (Netz Niederösterreich) über die Kundenschnittstelle auszulesen.) that reads parameters from my smartmeter and publishes them to mosquitto.

I wanted to adapt the script to support discovery, I currently publish following discovery message to homeassistant/device/smartmeter/config

{
  "dev": {
    "ids": "smartmeter",
    "name": "Smartmeter",
    "mf": "Shenzhen Kaifa Technology",
    "mdl": "Kaifa MA309M H4LAT1",
    "sw": "1.0",
    "sn": "",
    "hw": ""
  },
  "o": {
    "name": "Smartmeter",
    "sw": "1.0",
    "url": "https://github.com/greenMikeEU/SmartMeterEVNKaifaMA309"
  },
  "cmps": {
    "smartmeterTotalEnergyExport": {
      "p": "sensor",
      "device_class": "measurement",
      "unit_of_measurement": "Wh",
      "value_template": "{{ value }}",
      "unique_id": "TotalEnergyExport",
      "state_topic": "homeassistant/sensor/smartmeter/TotalEnergyExport"
    },
    "smartmeterTotalEnergyImport": {
      "p": "sensor",
      "device_class": "measurement",
      "unit_of_measurement": "Wh",
      "value_template": "{{ value }}",
      "unique_id": "TotalEnergyImport",
      "state_topic": "homeassistant/sensor/smartmeter/TotalEnergyImport"
    }
  },
  "qos": 0
}

In MQTT Explorer it currently looks like this:

I am trying to have the smartmeter as 1 device with multiple components (currently 2, TotalEnergyExport and TotalEnergyImport for testing, but there are more values i would like to get).

Can someone point me in the right direction or does have an example?

I find the documentation at MQTT - Home Assistant a bit confusing, I am not sure what i need to structure differently.

Thanks in advance :slight_smile:

Cheers,
zuwizara

I think that device discovery only applies if a single topic defines the states of multiple components.

In you case, you probably need single component discovery (with the dev and o section identical for all)

I actually thought about that too and tried the following config sent to topic homeassistant/device/smartmeter/config

{
  "dev": {
    "ids": [
      "smartmeter181"
    ]
  },
  "o": {
    "name": "smartmeter"
  },
  "cmps": {
    "TotalEnergyExport": {
      "p": "sensor",
      "value_template": "{{ value_json.TotalEnergyExport }}"
    },
    "TotalEnergyImport": {
      "p": "sensor",
      "value_template": "{{ value_json.TotalEnergyImport }}"
    }
  },
  "state_topic": "smartmeter/state",
  "schema": "json",
  "qos": 0
}

And a single topic, smartmeter/state defines the state for both components.

This is as close to the example in the docs MQTT - Home Assistant as I can get, however still no luck.

Are there any logfiles which could help me figure out whats wrong here maye? :thinking:

Hi zuwizara,

You said auto-discover…
For auto discover to work, you have to put things in specific topics where HA is looking. It doesn’t look everywhere, just certain places. MQTT - Home Assistant.

Since you do not appear to have control of the paths the software uses, you will need to set the entities up manually.

One entry per entity.

Hi @Sir_Goodenough!

Thanks for the reply!

I have complete control of where i put things - I just dont understand where I need to put them.

My auto-discover prefix is “homeassistant”, so i put the discover message there.

It also says that i need to put it under “device” in the docs - so i put it under homeassistant/device/smartmeter/config.

The State is under smartmeter/state - since I am referring to this topic in my config.

Did I understand something wrong from the docs?

This is a blueprint, but in it I create a number of auto discovery HA entities.

Perhaps you can look at it for inspiration.

HA_Blueprints/Automations/Octoprint_Additional_Buttons_Helper.yaml at d1c15d75a7b5fa3042d60f7eee2d7633ea8a27a3 · SirGoodenough/HA_Blueprints · GitHub.

Hmm i thought maybe somebody has the device discovery already implemented working and could share an example JSON-config along with the topic where it gets published to and the layout of the topic(s) containing the state(s) :slight_smile: