MQTT - Receive a value on a custom topic

Hello everyone,

Following my previous post (Ores - Compteur électrique intelligent - #10 par pierre-gilles), I realized it was necessary to go through Node-RED to transform information received by an MQTT sensor to Gladys.

To put it simply, my sensor sends with a topic (ex: « dsmr/reading/phase_currently_delivered_l3 ») and Gladys only allows me to listen on an automatically generated and non-editable topic (ex: « gladys/master/device/mqtt:consommation_instantanee/feature/mqtt:consommation_instantanee/state »).

In short, to do something ‹ simple ›, I have to go through Node-RED to transfer the information from one topic to another.

Would it therefore be possible to create a mapper to fill this gap? Or to make the topic created by Gladys editable? :slight_smile:

Thanks in advance

Thank you for the request, I edited the title to make it clearer.

For me 2 possibilities with their pros and cons:

  • Do this via scenes, via a trigger « When an MQTT message is received », which takes as parameters an MQTT topic, and an example of a received message. Then, in the scene, a variable is injected that is usable everywhere (send a Telegram message, set the state of a sensor, etc..). The advantage is that it’s infinitely customizable; the disadvantage is that you have to make one scene per sensor, which can be cumbersome.
  • Do this in the MQTT integration directly, via a « custom » topic that will be customizable. The advantage is that it’s simple to use; the disadvantage is that it’s not customizable at all (if the MQTT message has a specific format, how do we handle that?)
1 Like

It is true, as I already pointed out, that this is a constraint when dealing with fixed or limited publish/subscribe on certain devices; the question remains whether the ability to assign a device’s behavior (switch, on/off bulb, etc.) to a customizable subscribe or publish is feasible in Gladys rather than the somewhat heavy current syntax (the « mqtt: » is unnecessary in the name of the subscribe or publish). It would be a plus, because currently it’s an illogical limitation given how MQTT works.

2 Likes

Partially developed by @Lokkye, right?

Following this discussion I also think that allowing the listening topic to be configured would be interesting.

I’ve started development on this topic :slight_smile:

Following our discussion with @Alyafride, I went for an interface that looks like this:

Nothing definitive, I just started 2 minutes ago :stuck_out_tongue:

I’ll keep you posted as soon as I have a test image to show you!

4 Likes

Great, I think this addition can help to easily integrate different sensors.

In terms of UI I would have imagined a switch at the device level to define whether the payload is of type JSON or « sub-topic » and optionally to set the parent topic. Then at the feature level define the JSON path or the sub-topic (not sure that combining both simultaneously is necessary)

First successful test !

I created a device that listens on this topic with a JSON body :

I send this JSON body :

Available in the UI :

There is still work to do, because currently the MQTT integration does not support multiple listeners on the same topic, which would prevent using the same topic across multiple features, which is inconvenient in the case of this feature

I’m not sure, that might make each feature longer (and it’s already long)

Ah? In my mind that slightly complicates the creation of the device but simplifies that of the feature

Ah, I didn’t realize you were talking at the device level!

I don’t know, as long as it’s as simple as possible, nothing to activate, just fill it in if the user wants to use it

I see pros and cons to both options actually :thinking:

On one hand, since the MQTT topic for devices often contains a unique ID (the device’s serial number or its MAC address), it seemed simpler to define it only once at the device level, but that has a heavier impact on the interface.
On the other hand, it offers more flexibility to specify the full topic each time and has less impact on the interface. However, it implies a « JSON path » field which can be confusing if the metrics are emitted « flat ».

For me it should be at the feature level — we don’t know how each brand works ^^

I imagine some will want a thread per feature

That works, I’ll be able to run tests with what I have at home on MQTT.

I’ve finished the feature, and I’m currently adding a « Debug MQTT » tab which will make it easier to understand what’s happening on the instance:

That way the user isn’t left in the dark if it doesn’t work :slight_smile:

4 Likes

I’ve made good progress and have a test version to submit to you :slight_smile:

The build is currently being built on the Docker image:

gladysassistant/gladys:mqtt-custom-topic

Edit: the image is ready!

Help: Tutoriel: Lancer une image Docker de test

Looking forward to your feedback!

I just tested, it works for a device that posts with a topic/metric. However, for a device that posts different metrics within a JSON, only the first « feature » works; the second, configured on the same topic but with a different JSON path, does not retrieve any data. Ex of data:

{"CO2": 588.235, "CO2|unit": "ppm", "Humidity": 43.0, "Humidity|unit": "%", "Temperature": 25.8, "Temperature|unit": "\u00b0C", "Day/Night": "Day", "Battery Autonomy": "62.5-50%", "_timestamp": 1714758545, "_rssi": -61, "_rorg": 210}

Finally I hadn’t remembered this when defining the expected behavior, but it’s clear that filling in the external ID is quite tedious; it would be nice to have a generated default value if the field is left to MQTT:

Hi @Alyafride !

I think there’s a UI bug when filling in these values during the creation of the MQTT device, however when editing it works perfectly… Thanks for the feedback, I’ll work on it and get back to you :slight_smile:

However, I tested the case of having multiple metrics within the same JSON and it works well:

The configuration:

[quote="Alyafride

1 Like

Yes, why not, personally I quite like NetBox’s logic for defining slugs. Basically, if the field isn’t filled in it sets it based on the name and makes it URL-safe.

2 Likes

@Alyafride I figured out what was wrong, I made a fix, can you test with the new image? :slight_smile:

\r\ngladysassistant/gladys:mqtt-custom-topic

Remember to do docker pull, stop, rm, and recreate the container :slight_smile:

1 Like

That seems good to me now :slight_smile: I tested it in JSON and the metric « flat ».