OpenEnergyMonitor Home Assistant MQTT Discovery (WIP)

I’ve been using Home Assistant (HA) in my own home for years as a kitchen dashboard display. I started using HA when configuration via .yaml files was the only way to configure it, hence most of my config is still done this way. However, HA has advanced a lot over the years, and it’s now possible to configure everything via the UI and auto-detection of devices plays a big role in this.

To get data from OpenEnergyMonitor hardware into HA there are currently two options:

  1. Emoncms: Use the Emoncms HA integration to get feeds from Emoncms into HA

  2. MQTT: Get the data locally from the MQTT server running on the emonPi / emonBase

I imagine most people currently use option 1, but option 2 has a number of advantages it also provides the most real-time data via lightweight MQTT and gives the option to use OpenEnergyMonitor hardware with HA without having to configure Emoncms.

2. MQTT

All emonPi and emonBase units run an MQTT server on port 1882, data is published to MQTT topics with the following format:

basetopic/node/keyname

e.g

emon/emonth6/temperature

The manual way to configure to subscribe to an MQTT topic in HA was to configure it manually in configuration.yaml e.g

mqtt:
  sensor:
    - name: "emonth6-temperature"
       state_topic: "emon/emonth6/temperature"
       device_class: "Temperature"
       unit_of_measurement: "°C"

However, wouldn’t it be great if all the OpenEnergyMonitor sensor data would just appear automagically in HA!


This will be possible in the future once we implement HA MQTT auto-detection. The purpose of the remainder of this post is to explain how this works, the user would not normally need to be aware of this.

Autodetection works by using HA MQTT Discovery, we tell HA in advance what each MQTT topic contains by posting a JSON string to the HA MQTT Discovery topic, for a sensor by default this is:

homeassistant/sensor/<vendor-name>/<device-name>/config

For our emonth6 temperature example we would post to the topic

homeassistant/sensor/openenergymonitor/emonth6_temperature/config

The following json string;

{
  "stat_t": "emon/emonth6/temperature",
  "device": {
    "identifiers": "emonth6",
    "name": "emonth6",
    "manufacturer": "OpenEnergyMonitor"
  },
  "uniq_id": "emonth6_temperature",
  "name": "emonth6 Temperature",
  "unit_of_meas": "°C",
  "device_class": "Temperature"
}

and then do the same for the humidity reading also coming from emonth6

homeassistant/sensor/openenergymonitor/emonth6_humidity/config

{
  "stat_t": "emon/emonth6/humidity",
  "device": {
    "identifiers": "emonth6",
    "name": "emonth6",
    "manufacturer": "OpenEnergyMonitor"
  },
  "uniq_id": "emonth6_humidity",
  "name": "emonth6 Humidity",
  "unit_of_meas": "%",
  "device_class": "Humidity"
}

and finally the same for the battery voltage from emonth6

homeassistant/sensor/openenergymonitor/emonth6_battery/config

{
  "stat_t": "emon/emonth6/battery",
  "device": {
    "identifiers": "emonth6",
    "name": "emonth6",
    "manufacturer": "OpenEnergyMonitor"
  },
  "uniq_id": "emonth6_battery",
  "name": "emonth6 Battery",
  "unit_of_meas": "V",
  "device_class": "Voltage"
}

Note: the config should be published with persistence flag and persistence should be enabled in the MQTT broker for reliable device config.

Now if look under Settings > Devices & Services > MQTT in HA we see the following device

Clicking on the devices shows the sensors associated with this emonth6 devices:

These sensors are now being logged to HA can now be easily added to any HA dashboard or used in automations

2 Likes

Hopefully this will not be automatic and forced on users, but selectable. I don’t want several hundred Feeds suddenly appearing in HA.

Certainly don’t use the emon basetopic - you are just loading up the incoming emoncms_mqtt process with even more work (it is pretty inefficient as it is).

I think the effort would be better spent on getting the integration working with UI configuration.

Supporting @bwduncan to do this is a much better solution.

I feel that HA are trying to discourage MQTT Auto Discovery for proper integrations, preferably using the API.

[edit]
I also note you have not included the state_class in the discovery message. This is likely throwing errors in the log at startup.

This discovery will need to be optional as the user will have to identify the state_class for the entity as that cannot be assumed.

1 Like

Do you know why? It seems uncooperative, if so.

No, just seems to be the way. It might be that using the API is less resource intensive than MQTT (which isn’t very efficient).

Devs generally implement what they want in the way they want it rather than what the users want, I find.

1 Like

Oh yes, this will be optional. There could be a flag to enable HA integration in emonHub, possibly on a per device basis.

I didn’t realise that MQTT discovery was being discouraged

They certainly seem to be in favour of the API. Just pick it up from the change notes etc. probably not actively discouraging, but certainly not encouraging that direction for new work.