Skip to content

mqtt-hassio.cfg: two bugs when using --mqttjson with HA MQTT discovery #1735

@SanwebGit

Description

@SanwebGit

Environment

  • ebusd version: 26.1
  • Using --mqttjson --mqttint=mqtt-hassio.cfg
  • Home Assistant MQTT integration (auto-discovery)

Bug 1 — value_template uses .value attribute access which fails in HA Jinja2

File: contrib/etc/ebusd/mqtt-hassio.cfg, line 267

"value_template":"{{value_json[\"%field\"].value}}",

When --mqttjson is active, ebusd publishes nested JSON payloads like:

{"temp": {"value": 14.31}, "tempmirror": {"value": 65318}, "sensor": {"value": "ok"}}

value_json["%field"] therefore returns the inner dict {"value": 14.31}. In Home Assistant's Jinja2 environment, .value does not perform dict key access — it returns the dict object itself serialized as a string, producing the following HA error for every polled sensor:

ERROR homeassistant.components.mqtt.models: Value error while updating state of sensor.ebusd_bai_aitemp_temp:
Sensor has device class 'temperature' […] however, it has the non-numeric value: '{'value': 14.31}'

Fix:

-  "value_template":"{{value_json[\"%field\"].value}}",
+  "value_template":"{{value_json[\"%field\"][\"value\"]}}",

Bug 2 — Name-based matching in type_switch-number assigns incorrect device_class when no matching unit is present

File: contrib/etc/ebusd/mqtt-hassio.cfg, lines 243–251

type_switch-number =
    ...
    sensor,temperature,measurement = temp|,°C$
    sensor,temperature,measurement = temp|,K$
    sensor,power,measurement       = power|,kW$|,W$
    ...
    sensor,energy,total_increasing = energy|,Wh$

The | operator makes these rules match if either the field/message name contains the keyword or the unit matches. This causes fields that contain "temp", "energy", or "power" as a substring — but carry no matching unit — to receive an incorrect device_class. Examples from a Vaillant BAI circuit:

Entity Matched substring Assigned device_class Actual unit
DeactivationsTemplimiter "temp" in "Templimiter" temperature (none)
PrEnergyCountHc1 / Hc2 / Hc3 "energy" energy (none)
PrEnergySumHc1 / Hwc1 etc. "energy" energy (none)
WPPWMPowerDia "power" power (none)

HA emits a warning for every such entity on every state update:

WARNING homeassistant.components.sensor: Entity sensor.ebusd_bai_deactivationstemplimiter
is using native unit of measurement 'None' which is not a valid unit for the device class
('temperature') it is using; expected one of ['°C', '°F', 'K']

Fix — match only on unit, not on name:

-    sensor,temperature,measurement = temp|,°C$
-    sensor,temperature,measurement = temp|,K$
-    sensor,power,measurement       = power|,kW$|,W$
-    sensor,energy,total_increasing = energy|,Wh$
+    sensor,temperature,measurement = ,°C$
+    sensor,temperature,measurement = ,K$
+    sensor,power,measurement       = ,kW$|,W$
+    sensor,energy,total_increasing = ,Wh$

This ensures device classes are only assigned when the unit confirms the physical quantity, not when the field name happens to contain the substring. Sensors with units of °C, K, kW, W, or Wh are unaffected.


Happy to provide a pull request if preferred.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions