MQTT / sensor help needed

Hi,

I’m trying to get something very simple done and I can’t figure it out.
I have a mqtt topic called : mytopic/test with the following json:

{
“state”: “A1 XXXXXXXXX”,
“attributes”: {
“time received”: “Sun Jul 25 11:43:16 2021”,
“group id”: “10.564”,
“receivers”: " (0044545460)",
“capcodes”: [
“0023230”
],
“priority”: 1,
“disciplines”: “Politie (0034560)”,
“raw message”: “Some RAW message”,
“region”: “Somewhere”,
“location”: “”,
“postcode”: “”,
“city”: “Groningen”,
“address”: “”,
“street”: “”,
“remarks”: “”
}
}

I try to get to make a sensor out of this. For this I tried:

sensor:

    • platform: mqtt*
  • name: “somesensorname”*
  • state_topic: “mytopic/test/state”*
  • value_template: ‘{{ value_json.state }}’*
  • json_attributes_topic: “mytopic/test”*

Now I do get the attributes, but no state name.
For the state name, I want ‘state’ but I cant figure out how to do that.

Can anyone help me out here please?

Please format your YAML and JSON properly – see point 11 here.

I don’t think this is correct. Try mytopic/test.

1 Like

@bastiaan The name associated with the state is the sensorname, in the yaml above “somesensorname”. If you want to display another name you have to specify this in LoveLace.

@FransO I think I don’t really understand you. :frowning:
Can you give me a little example please?

@ondras12345 has the solution.
Not really sure what Frans is talking about, either :wink:

Maybe an example serves best. I’ve got a soundmeter attached to a Tasmota device that sends the an MQTT message with the following format:

{    "FS": "Fast",
    "AC": "dB(A)",
    "Level": "30-130",
    "dB_act": 39.5,
    "dB_gem": 39.6,
    "UO": ""
}

The definition of this sensor in configuration.yaml is:

  - platform: mqtt
    name: SE322 Sensor
    availability_topic: "tele/tasmota-s5/LWT"
    payload_available: "Online"
    payload_not_available: "Offline"
    state_topic: tele/tasmota-s5/se322
    value_template: {{ value_json.dB_act | int }}
    unit_of_measurement: dB
    json_attributes_topic: tele/tasmota-s5/se322

A possible definition for the sensor on my dashboard, I use Lovelace, then looks like this:


As you can see, the state value gets the friendly name of the sensor: " SE322 Sensor". If I want to show the sensor as “Soundmeter”, I would have to add a “name: Soundmeter” line beneath line 3 in the definition of the card.

Fine, but the OP problem is to get the state value, not change the name.

I solved it by changing my JSON. I took out the ‘attributes’
Now it’s like this:

{
  "state": "SPOED AMBU BRUG WORKUM",
  "time received": "Mon Jul 26 10:24:48 2021",
  "group id": "06.035",
  "receivers": "000302271",
  "capcodes": [
    "000302271"
  ],
  "priority": 0,
  "disciplines": "",
  "raw message": "FLEX|2021-07-26 08:24:48|1600/2/K/A|06.035|000302271|ALN|SPOED AMBU BRUG WORKUM",
  "region": "",
  "location": "",
  "postcode": "",
  "city": "Workum",
  "address": "",
  "street": "",
  "remarks": ""
}

And my YAML now looks like this:

sensor:
  - platform: mqtt
    name: "p2000"
    state_topic: "p2000/data"
    value_template: "{{ value_json.state }}"
    json_attributes_topic: "p2000/data"