Showing value of MQTT Sensor on Lovelace Dashboard

Hi! I have been having issues with just being able to show my humidity and temperature values on lovelace. I have this custom sensor that sends me the temp. & hum. values in millivolts. I am able to make them show in lovelace trought creating the entities like in my configuration.yaml. With this code:

sensor 1:
  platform: mqtt
  state_topic: '/devices/192.168.1.125/ai/1'
  name: 'Temperature'
  unit_of_measurement: '°C'
 
sensor 2: 
  platform: mqtt
  state_topic: '/devices/192.168.1.125/ai/2'
  name: 'Humidity'
  unit_of_measurement: '%'

Screenshot 2020-11-18 104040

NOW… what i need is to show the real temp & hum values, like 24 celcius and 35%… so i went on reading about TEMPLATING to make mathematical calculation on the millivolt values and bring them to readable values. VALUE_TEMPLATE. I have been trying all kinds of different lines of codes that i could find on the forum with no success.

The value_template line is the one i need help with…i show here what i would need it to do…

sensor 1:
  platform: mqtt
  state_topic: '/devices/192.168.1.125/ai/1'
  name: 'Temperature'
  unit_of_measurement: '°C'
  value_template: '{{ (value / 100) -20 }}'
  
sensor 2: 
  platform: mqtt
  state_topic: '/devices/192.168.1.125/ai/2'
  name: 'Humidity'
  unit_of_measurement: '%'
  value_template: '{{ value /100 }}'

THANK YOU!

Can you show what mqtt message you actually receive ?

the same as in the picture of the description.

here a picture of my mqtt explorer when i listen on topic:/devices/192.168.1.125/ai/1’
Screenshot 2020-11-18 151502
Same value as i am able to show in lovelace.

What is missing is the calculation for formatting the values.

Thank you

I really need help on that to continue, please!

I found it ! I am setup for that part ! Lets move on ! :slight_smile:
this is how i wrote it
value_template: ‘{{ (((value_json * 80)/10000) - 17) | round(2) }}’
It is just value_json the value and then i can make mathematicals…!

1 Like

Sorry, I know that this is an old thread…

But I just wanted to do the same and I´m having problems to get the information in my entities. Let me explain the setup:

RaspberryPi with DHT22 running a python script to send via mqtt the temperature & humidity values to my Home Assistant
Home Assistant running in my QNAP NAS as a Virtual Machine with the addon mqtt running.

If I go to Configuration → Add-on, Backups & Supervisor → Mosquitto broker:
I see Mosquito broker started with this configuration:

certfile: fullchain.pem
customize:
  active: false
  folder: mosquitto
keyfile: privkey.pem
logins: []
require_certificate: false

Ports are the current ones that came by default configured. I´m using 1883/tcp (Normal MQTT)

If I go to Configuration → Devices & Services → Integrations → Mosquito broker → Configure
Listen to a topic → # → Start Listening : I can see the values sent by my script on it

Great, so seems to be OK… Now comes the problems:

I´ve configured the sensors in HA according with this documentation. I´ve configured in the sensor.yaml file because I´ve already created for other issues and in configuration.yaml I get errors… So, this is sensor.yaml config:

# Sensor R.Pi
  - platform: mqtt
    name: "Temperatura Hab. Azul"
    state_topic: "nicma/sensors/blueroom/temperature"
    unit_of_measurement: "°C"
    availability_topic: "nicma/sensors/blueroom/temperature/status"
    payload_available: "online"
    payload_not_available: "offline"
  - platform: mqtt
    name: "Humedad Hab. Azul"
    state_topic: "nicma/sensors/blueroom/humidity"
    availability_topic: "nicma/sensors/blueroom/humidity/status"
    unit_of_measurement: "%"
    payload_available: "online"
    payload_not_available: "offline"

With this configuration I can see the two entities in Configuration → Devices & Services → Integrations → Entities:

But, as you have seen both appears as disabled

And also, when I click on them I see this message:
This entity (‘sensor.temperatura_hab_azul’) does not have a unique ID, therefore its settings cannot be managed from the UI. See the documentation for more detail.

I´ve been reading the documentation and the customizations part… But, I don´t know how to go on with these two issues.

Any help will be very wellcome.

I think there are several things missing
Keyword sensor at the beginning of your code. unique_id and state_topic are missing as well. So you should have:

sensor:
  - platform: mqtt
    name: "nameOfYourSensor"
    unique_id: "a unique id"
    state_topic : "depends on your hardware"

You should use MQTT explorer to see how the message are constructed. Maybe you need to add a value_template in order to extract the value you are looking for.

Hi,

Hi, thanks for your help.

Regarding your code… Is this code to be configured in configuration.yaml file?
Becuase I was using sensor.yaml instead of configuration.yaml because I´ve got errors when I configure it in configuration.yaml file

You say that state_topic is missing… But, it´s included in my code also! Have you seen it?

Regarding MQTT Explorer… Yes, I´ve used it to check that everything was sent correctly from my python script and AFAIK the messages are recieved in my broker I guess…

Here a picture of MQTT Explorer recieving the two topics sent from the script:

So, now… looking to your code:

sensor:
  - platform: mqtt
    name: "nameOfYourSensor"
    unique_id: "a unique id"
    state_topic : "depends on your hardware"

I have the next doubts about it:

1.- In which file should I configure this? or it´s still valid my config from sensor.yaml
2.- name: “nameOfYourSensor”: Which name should I use? DHT22? Or I could keep my previous name?
3.- unique_id: “a unique id”: How can I configure the unique_id? I´ve been reading regarding that but I don´t know how to proceed

Yes sorry I did not see state_topic. Yes the code is in configuration.yaml. Could be in another file, provided this file is included in configuration.yaml. To start with, make it simple in order not to cumulate problems.
unique_id has to be defined by you and shall be … unique. For instance since I’m developing bidirectional communication between HA and Domoticz, I use the Domoticz device IDX which is by contruction unique. It is really up to you.
name of your sensor is up to you as well. It will be the name displayed in the frontend interface. So yes you can keep the one you already set.
Don’t forget to restart the core after any change you make
As an example, see my configuration herebelow

sensor:
  - platform: mqtt
    name: "Température extérieure"
    unique_id: "415_1"
    object_id: "415_1"
    state_topic: "domoticz/out/415"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.svalue1 | round(1) }}"
    retain: true
  - platform: mqtt
    name: "Humidité"
    unique_id: "415_2"
    object_id: "415_2"
    state_topic: "domoticz/out/415"
    unit_of_measurement: "%"
    value_template: "{{ value_json.svalue2 }}"
  - platform: mqtt
    name: "Pression"
    unique_id: "415_3"
    object_id: "415_3"
    state_topic: "domoticz/out/415"
    unit_of_measurement: "hPA"
    value_template: "{{ value_json.svalue4 }}"

OK, I had to configure it in sensor.yaml since in configuration.yaml I had the reference:

sensor: !include sensor.yaml

Having this configuration in sensor.yaml:

  - platform: mqtt
    name: "Temperatura Hab. Azul"
    unique_id: "temp.hab.azul"
    object_id: "temp.hab.azul"
    state_topic: "nicma/sensors/blueroom/temperature"
    unit_of_measurement: "°C"
    payload_available: "online"
    payload_not_available: "offline"
  - platform: mqtt
    name: "Humedad Hab. Azul"
    unique_id: "hum.hab.azul"
    object_id: "hum.hab.azul"
    state_topic: "nicma/sensors/blueroom/humidity"
    unit_of_measurement: "%"
    payload_available: "online"
    payload_not_available: "offline"

It works very well. Thanks @kolia for your help!!

Glad you sorted it out.