How do I create MQTT Sensor

hi! Similar issue here. Please Help!

Sensor value : unknown.

in Mosquitto addon values are visible.


What is your yaml setup for the mqtt sensor.

Everything as you see in the first picture. nothing in config.yaml

mqtt.yaml

# MQTT merilec porabe

sensor:
   - name: "Skupna poraba" 
     state_topic: "merilec/IH000140/counters"
     force_update: 'true'
     value_template: "{{value_json.measurements/counter1 | float | multiply(100)}}"
     device_class: "energy"
     state_class: "measurement"
     unique_id: "skupna poraba"

I think that should be

value_template: "{{value_json.measurements.counter1 ...

Still unknown value.

Perhaps try without the float and multiply, to reduce chances of error.

The topic contains units. Try this:

value_template: "{{ (value_json['measurements']['counter1']).split()[0] }}"
unit_of_measurement: "kWh"

My test (yeah, I know I copied the number wrong):

Not sure why you’re trying to multiply by 100?

Just found some idea to tryout…

It works!

Thank you!!!

Alex

2 Likes

How did you know what topic to listen to?

Hello!.

I have the same error. Anyone can help me??. I success send data from python script to home assistant using mqtt but from 3 sensors, i only recieve 1 correct data.
image

I have the same data send it with the same script.

Why only Odomecer get the correct data and the temperature and fuel tank get unkown state??

Because you’re using dot notation for keys with - characters in.

Use:

value_json['foo-bar']

not

value_json.foo-bar

The dot notation is being interpreted as value_json.foo minus bar. Always better to use bracket notation.

1 Like

Hello and thanks for all the examples you shared. I tried my best to adapt them to my problem, but sadly I cant figure out what I’m doing wrong.

grafik

mqtt:

  • sensor:
    • name: “HauszählerNeu”
      unique_id: power_leistung_ein
      state_topic: “tele/tasmota_88CE90/SENSOR”
      value_template: “{{ (value_json[‘Hausheld’]).E_out }}”
      unit_of_measurement: “kWh”
      device_class: “energy”

Can somebody please help me? Thanks a lot!
Fred

Well for a start it has to be yaml. Secondly, tell us what symptoms you have. And post your sensor in text, not a picture. Copy/paste, been around for decades.

I’m sorry. I see how my post is confusing.

The mentioned code is copied from the configuration.yaml.

The error I got is status of the sensor “unknown”.

This is a copy from my mqtt explorer:

▼tele

▼tasmota_88CE90

LWT = Online

STATE = {“Time”:“2024-10-07T08:02:58”,“Uptime”:“1T17:30:15”,“UptimeSec”:149415,“Heap”:125,“SleepMode”:“Dynamic”,“Sleep”:50,“LoadAvg”:19,“MqttCount”:1,“Berry”:{“HeapUsed”:40,“Objects”:653},“Wifi”:{“AP”:1,“SSId”:“AnneKa”,“BSSId”:“2C:91:AB:FD:C1:4B”,“Channel”:6,“Mode”:“HT20”,“RSSI”:74,“Signal”:-63,“LinkCount”:1,“Downtime”:“0T00:00:04”}}

SENSOR = {“Time”:“2024-10-07T08:02:58”,“Hausheld”:{“E_in”:60.000,“E_out”:190.000,“Power”:0,“Wert”:“0a0148484500000467be”}}

Thanks for helping!!

Better, but your " and " is wrong. Managed to get it into https://jsonpathfinder.com/

Is the problem with the strange char at the end of Hausheld. It seems to give problems in jsonpathfinder. notepad++ shows it as EOT, which as far as I can woirk out is old ascii code for “end of transmission”.

I am no expert in character encoding or i18n.

1 Like

It does look odd.

Try:

{{ value_json[(value_json|select('match','Haus'))|first]['E_out'] }}

And @fredkometplease format your code properly with the </> button. I wasted a lot of time fixing smart quotes. Here’s my test template from Developer Tools / Templates, returns 190:

{% set value_json = {"Time":"2024-10-07T08:02:58","Hausheld":{"E_out":190.000}} %}
{{ value_json[(value_json|select('match','Haus'))|first]['E_out'] }}
1 Like

Thank you for helping! I found the issue. It was indeed a problem with the the sign after „hausheld“. When I got rid off it by fixing the mqtt message, the problem was gone and I can now see the sensors.

2 Likes