Hello
This is a question about the Homie convention and how to put all the data into a Javascript object which follows the Homie convention structure.
The question is further down, for those not interested in the background, just jump one page down.
Background:
With continually adding new devices to my node-red based home monitoring system, I found that my initially defined MQTT topic tree still works. So far so good. Nevertheless I had a close look at the Homie convention, and after some initial struggle started to like it.
Another problem of my initial setup is, that I split up the incoming messages from the sensors into single messages for each measurement type. For the DHT11 devices this means, that two messages are sent into influxdb for the temperature and for the humidity, and Colin on this board strongly advices against that in several threads. So I plan to take this into account too.
My current idea is to take the incoming messages via MQTT from the sensors, and create a Javascript object based on the Homie convention. I receive input messages from various devices in my home monitoring network. Mostly this is measurement or status data, changing over time. These measurement values are properties of a node of a device in Homie terminology. This is done in a function node per device as a first step, later on I might put all fixed data of the device like naming of the nodes and properties in a file or context and load it from there. Clever me wanted to put everything into a msg.homie.{} object, but I failed. This leads to the question below:
End of Background.
Start of Question:
I receive several measurement values from a sensor via MQTT like this:
msg.payload = {"Name":"sonoffth4","Temp":21.7,"Hum":47.1}`
I want to convert this into a Javascript object according the Homie convention like this
msg.homie.sonoffth4.{}
I use the MQTT topic hierarchy as structure of the Javascript object. Up to the node level everything went fine. But on the property level I got a problem. I paste the example of the convention, chapter 7.3.1, this will make it clear (hopefully):
homie/super-car/engine/temperature/$name → "Engine temperature"
homie/super-car/engine/temperature/$unit → "°C"
homie/super-car/engine/temperature/$datatype → "float"
homie/super-car/engine/temperature → "21.5"
Everything is a key-value pair, I know how to handle this. But on the very last line of the convention, the key does not exist, there is just a value posted. With MQTT that is no problem at all. But I was not able to wrap my head around how to code this.
msg.homie.super-car.engine.temperature = {};
msg.homie.super-car.engine.temperature.$name = "Engine temperature";
msg.homie.super-car.engine.temperature.$unit = "°C";
msg.homie.super-car.engine.temperature.$datatype = "float";
msg.homie.super-car.engine.temperature = "21.5";
The last line kills the entries before, as I found out the hard way.
I can fix it like this:
msg.homie.super-car.engine.temperature.$payload = "21.5";
But this breaks the Homie convention with a new attribute which I would like to avoid.
Is there a better way?