Defining value of an custom mqtt automation

Hi all, I don’t know why I’m finding this so incredibly difficult. Frustrating because it seems like it should be very simple.

I have grafana/influx integrated into my HA setup. Grafana is able to plot out any entity_id value over time.

I have a pi on the network which has various python code publishing mqtt topics.

For example I publish home/stat/ which has a payload of

{
 "dba": 44
}

I’m simplifying the example but I’m sending dba sound levels as part of the json payload.

In HA, I’ve set up a very basic automation just so that an entity_id is created that Grafana can see. I don’t actually need to trigger or take any action right now – I just want to map the values from one of the json keys to be the “value” of this visible in Grafana over time.

After having this automation setup, Grafana is able to see this entity_id but it’s value is always either 0 or 1. If I trigger the topic publish, grafana graphs “1” but doesn’t log the actual value from the payload.

I’ve tried various things like:

platform: mqtt
topic: home/stat/
value_template: “{{ value_json.state.dba }}”

Am I on the right direction or do I need to set this up differently? Thanks

To help you create an mqtt sensor we need to see the actual data you are publishing to the mqtt topic, not a simplified version.

Right now that is the data:

{
"dba": 45
}

So it sounds like I don’t need an automation at all, but to create an mqtt sensor. Everything I could find says these are created in the configuration.yaml – curious, is there no other way to edit one through HA without using file editor. Sort of like how automations can be managed in settings without editing YAML directly

No It has to be done in YAML.

For your data the value template would be:

value_template: "{{ value_json.dba }}"

If other messages are sent to that topic that do not have the dba data key in them then you would have to use:

value_template: "{{ value_json.dba if value_json.dba is defined else this.state }}"

Ideally you would always send all data keys to the one topic, or use separate topics for separate messages that have different data.

1 Like

thank you @tom_l .

I did create a sensor this way and reloaded HA, but I don’t see it showing up as an entity_id in Grafana. I seem to only see the mqtt automations there but not mqtt sensors. Is there something else I have to do other than create it in the configuration yml?

Does the sensor show up correctly in Home Assistant?

You will have to restart home assistant after creating the sensor config. A config reload will not be sufficient for the first time you use any new integration.

How are you sending data to Grafana, are you using InfluxDB?

I did restart the server completely. I see the sensor showing up in HA under settings (/config/entities)

Status shows up as “read-only”.

Grafana is connected to influx yes. For example when I added the automation in automation.yml, it showed up in grafana automatically under entites.

I did see this message, not sure if this may be an issue:

This entity (‘sensor.spl2’) does not have a unique ID, therefore its settings cannot be managed from the UI. See the documentation for more detail.

I have no idea where you are seeing that.

Look in Developer Tools → States. Does your sensor appear there?

Does it have the correct state value?

It is not. It just means you can not edit it from the user interface.

yes I do see it in Developer Tools → States:

sensor.spl2
spl2
55	friendly_name: spl2

And it finally did show up in influxdb. Maybe it was because I needed to send the value again over mqtt. Thanks for your help :slight_smile:

Yes that is exactly why. The values are only sent to InfluxDB when they change.