Hello everyone, after a long search, I would like to ask the more experienced for advice:
HA on ODROID, I receives JSON data from an external MQTT broker. Data to this broker are sended from multiple sensors. All sensors send data to ONE common topic. The JSON structure in MQTT is the same for all sensors and includes the string ADDR, which lets me know which sensor sent the data. Up to this is Ok, on the dashboard I can in real time see the numbers {ADDR:xxx, VAL1:yyy, VAL2:zzz, VAL3:qqq} extracted from JSON.
Now I need to put data into influxDB and then display it with Grafana. But display separately for each ADDR.
I do not known, how to save it to InfluxDB so that ADDR is a tag for each VALx and I could filter/group those VALx according to ADDR. It’s somehow saved to InfluxDB now, but the values don’t make sense (VALx is from a different sensor at every moment, mixed together).
Instead of having just one sensor in HA, create template sensors for each separate one that each extract their own information form the main one. Then influxDB will also have the data separated. Having all sensors separated in HA is useful for automations in HA too, so you should do that anyway.
Yea as edwin suggested, you really want a one to one match between physical sensors and HA sensors. If all your physical sensors are dumping into one HA sensor you’re going to struggle all over. Especially with the model you’re describing since it sounds like you can’t even predict which field has the data for which sensor, you have to first find the correct ADDR
field then find the matching VAL
field.
You’re going to need very complicated templates everywhere in HA to do anything with that data. And it’s going to be really rough on your DB since you have all these constantly changing attributes, something HA does not really handle well (both in InfluxDB and Recorder).
Do you have control over the source of that MQTT data? If so could it be changed to publish more like this:
topic: {common base topic}/{ADDR}
payload: {VAL for device with ADDR}
Then you could simply create an MQTT sensor per each device. This will work well with both InfluxDB and HA in general.
If you can’t do that then do what Edwin suggested above. Make template sensors where each looks something like this:
name: Device {ADDR}
state: >-
{% set addr = '{ADDR}' %}
{% set index = states.sensor.raw_mqtt_data.attributes | dictsort
| selectattr('1', 'eq', addr) | map(attribute='0')
| first | replace('ADDR', '') %}
{{ state_attr('sensor.raw_mqtt_data', 'VAL%s' % index) }}
This then gives you a sensor per physical device. Then you can use these sensors throughout HA and tell InfluxDB and Recorder to record their history. You should then exclude sensor.raw_mqtt_data
from both recorder and influxdb to ensure it is not recording history of the raw sensor as that will chew up a ton of disk space.