feeleep75
(Feeleep75)
June 17, 2024, 9:28am
1
Hi,
I am trying this for days and can’t get it work. Any help appreciated
I am trying to create MQTT discovery message. My JSON is following:
Topic:
scrappers/pmpm/status/total_energy
JSON:
{
"totalEnergy": 4489.648999999998,
"totalTime": "2310:05:30"
}
Now discovery message…
Topic:
homeassistant/sensor/pmpm_total_energy/config
JSON:
{
"name": "Total Energy",
"state_topic": "scrappers/pmpm/status/total_energy",
"json_attributes_topic": "scrappers/pmpm/status/total_energy",
"json_attributes_template":"{{ value_json.totalEnergy }}",
"unique_id": "pmcc_total_energy",
"template_value":"{{ value_json.totalEnergy }}",
"unit_of_measurement": "kWh",
"device_class":"energy",
"device": {
"name": "PMPM",
"identifiers": [
"pmpm"
],
"manufacturer":"PMPM"
}
}
tried with and without json_attributes_template
but it just show sensor state as On, nothing more.
Any ideas?
Hi,
Does the state change if you strip the code back to the basics in a test harness? e.g.
Remove the units and class to test getting a raw number from the JSON via the Jinja template?
Publish a test payload without totalTime
to simplify the JSON?
Have you double checked the payload in MQTT Explorer to avoid any confusion between total_energy
and totalEnergy
?
I’ve posted a few examples and dev cycle tips on HASS Discovery in the past, but suspect this is more about parsing the JSON:
Note to developers - Whilst you’re re-factoring your MQTT Discovery code, can I suggest adding additional values to improve how users can interact with your hardware?
Here’s an example from my own test code (with some light changes) which adds:
The example Device is configured with optional extra information such as manufacturer, version, and the ‘suggested_location’ which can be used to set the Area.
The example Entity adds optional extra values such as an icon, the device class and unit of …
You’ve got it in one - same HASS discovery as Tasmota (well, pre v-12 SetOption19 0 that is).
The hardware is based on a RPi Zero and a Pimoroni Grow hat running Python.
Note - due to the shortage of RPi, Pimoroni have produced a new version based on the new RPi PicoW uP. Not looked if the uP has microPython Paho MQTT support.
The code is a fork of Pimoroni’s example code with MQTT support added for both sensor publish and motor control using the Paho MQTT client. It should really be up in Gi…
Presumably, whilst the state entity was being added the devs worked on the entity naming code, updated the integration API and Entity Naming Standards and found the existing MQTT integration result gave “Device Name Device Name” as they code now concatenates Device Name and Entity Name which were often set to the same value by MQTT device developers.
These fields were only loosely documented in the HASS MQTT Discovery documentation, so now every developer using the spec needs to update their co…
If this helps, this post!
1 Like
feeleep75
(Feeleep75)
June 17, 2024, 1:42pm
3
Thanks for suggestion - I removed this line
"json_attributes_template":"{{ value_json.totalEnergy }}",
and it works now! I thought I tried all combinations…
Hi
Great news - my first auto-discovery in Python code felt like having to make all possible mistakes first, before finding one that worked by elimination. Simple is good!
It might help others find this post if you tick the Solution button.
TTFN,
James
feeleep75
(Feeleep75)
June 17, 2024, 2:28pm
5
So maybe one more question… Is it possible to set min and max values for Number platform from MQTT Json? When I try to do the same as value_template it seems to not working…
Looking at the docs shows plausible fields are included but I’ve not tried the real-world usage (or looked at the code )…
'max': 'max',
'min': 'min',
The issue is most of the values in the docs are basically a dump from a Python header file abbreviations.py
and don’t include much indication of real-world usage - hence my heavy use of mosquitto_pub
to create a test from the command line and quickly see what happens.
At least they now include some mosquitto_pub
examples after some gentle suggestion via GitHub which has made usage clearer.