Mqtt sensors and influx:

Hi There,

I’m using several mqtt sensors and influxdb with grafana.

This worked ok for the first sensor: I was able to find it in grafana.

Since that sensore reports temperature, humidities and rotation of a fan, I added more entries, so all of the values are available in lovelace and in grafana.

I added the unit_of_measurement entry to the config, which brings me -literally- the unit of measurement to grafana. I rather expected the name of the sensor to be selectable, not the “g/m^3”.

grafana

I read the docs but was unable to find s.th. reasonable. I tried to specify unique_id, but this didn’t help either.

Did anybody have a similar problem?

Here’s the relevant parts of my config:

sensor:
  - platform: mqtt
    name: "Luefter 1 - Temperature"
    state_topic: "/mhiot/luefter1/status/dht_temp"
    unit_of_measurement: '°C'
    device_class: temperature
    unique_id: 'sensor.luefter1.temperature'
  - platform: mqtt
    name: "Luefter 1 - abs-humidity"
    state_topic: "/mhiot/luefter1/status/dht_habs"
    unit_of_measurement: 'g/m^3'
    device_class: humidity

  - platform: mqtt
    name: "Luefter 2 - Temperature"
    state_topic: "/mhiot/luefter2/status/dht_temp"
    unit_of_measurement: '°C'
    device_class: temperature
    unique_id: 'sensor.luefter2.temperature'
  - platform: mqtt
    name: "Luefter 2 - abs-humidity"
    state_topic: "/mhiot/luefter2/status/dht_habs"
    unit_of_measurement: 'g/m^3'
    device_class: humidity
influxdb:
  host: 192.168.0.1
  database: home_assistant
  username: homeassistant
  password: 9wi40htqh5.
  ssl: false
  verify_ssl: false
  max_retries: 3
  # default_measurement: state
  tags:
    instance: prod
    sourceinfluxdb:
  host: 192.168.0.1
  tags:
    instance: prod
    source: hass
  tags_attributes: friendly_name
# also tried:
#  tags_attributes: 
#        - friendly_name
#        - unique_id
# without success

@marcvs

Have you find solution to this issue? I have the same problem :frowning:

Thx

Sort of, I’ve written a small tool that listens on specific mqtt topics and pushed them into influx. Since some processing may be required, it’s pluggable, so that one can adapt the processing by providing one’s own python code.

It’s independent of home-assistant, as it solely interfaces with mqtt and influx.

I’ve developed it against my private git repo, but I can upload it to github, if you’re interested.

Try “Toggling Edit Mode” and then write your own query…I find it works much better and is more precise. Use the grafana docs to get other ideas for constructing your own queries.

I use this query for my sensors (including MQTT) hasn’t failed yet for me this query:

SELECT mean("value") FROM "V" WHERE ("entity_id" = '240_v') AND $timeFilter GROUP BY time($__interval) fill(null)

NOTE "V" = YOUR_unit_of_measurement, WHILE '240_v' = your_entity_ID

Try that and see if it works for you

Alright, I’ve placed it on github. Documentation may be improvable. Feedback / PRs appreciated.

What you want to achieve is actually independent of MQTT. You may use the influxdb integration and map the entity_ids to units as below.

In Grafana you can just then use these units to select all entity_ids that output these units. Then delineate them by using GROUP_BY with tag(entity_id) (literally as written here). As Alias you may want to use $tag_entity_id (literally).

It is really that easy.

influxdb:
  host: hassio
  port: 8086
  database: homeassistant
  username: hassio
  password: !secret influxdb_hassio
  max_retries: 3
  default_measurement: state
#  exclude:
#    entities:
#       - entity.id1
#       - entity.id2
#    domains:
#       - automation
#  include:
#    entities:
#       - entity.id3
#       - entity.id4
#  component_config:
#    counter.hotw:
#      override_measurement: lt
  component_config_glob:
    sensor.*link*quality*:
      override_measurement: linkquality
    sensor.*status:
      override_measurement: linkquality
    sensor.*humidity*:
      override_measurement: humidity
    sensor.*temperature*:
      override_measurement: temperature
    sensor.*illuminance*:
      override_measurement: illuminance
    sensor.*air*quality*:
      override_measurement: airquality
    sensor.*air*pressure*:
      override_measurement: airpressure
    sensor.*_power*:
      override_measurement: power
  tags:
#    instance: prod
    source: hassio
1 Like

Hi,
I had the same problem and I thought I’d share the solution: Home Assistant uses the unit of measurement as the default for measurement_attr and falls back to ‘entity_id’ if no unit is specified. To always use the entity ID just add this to the influxdb config section:

measurement_attr: entity_id
1 Like