Mqtt sensor data

How can I configure a sensor in my .config to pull temp and humidity from this :


Message 10 received on tele/tasmota_377AE4/SENSOR at 7:05:
{
    "Time": "2021-04-29T07:05:31",
    "TuyaSNS": {
        "Temperature": 19.2,
        "Humidity": 4.7
    },
    "TempUnit": "C"
}
QoS: 0 - Retain: false

Try this:

sensor:
  - platform: mqtt
    name: temperature
    state_topic: "tele/tasmota_377AE4/SENSOR"
    value_template: "{{value_json.TuyaSNS.Temperature}}"
    device_class: temperature
    unit_of_measurement: '°C'
    icon: mdi:thermometer
  - platform: mqtt
    name: humidity
    state_topic: "tele/tasmota_377AE4/SENSOR"
    value_template: "{{value_json.TuyaSNS.Humidity}}"
    device_class: humidity
    unit_of_measurement: '%'
    icon: mdi:water-percent

I would note that 4.7% humidity seems very low, at least in my country!?!

1 Like

And if you’re interested in playing with the value_template:, you can use this in the template editor under developer tools:

{% set value_json=
  {
    "Time": "2021-04-29T07:05:31",
    "TuyaSNS": {
        "Temperature": 19.2,
        "Humidity": 4.7
    },
    "TempUnit": "C"
  }
  %}

{{value_json.TuyaSNS.Humidity * 10 }}

Put your mqtt package in and then you can test the value template beneath. Helped me a lot as I’m still very unsure about jinja2 and templating in general!

1 Like

Perfect thank you. The multiply was needed for humidity.

Temp shows correctly but will randomly show 0°c for several minutes, then update again correctly. Odd

I’m only just starting with mqtt and the json and don’t understand the syntax for it yet

Thanks for the help

It may do that if the payload published to tele/tasmota_377AE4/SENSOR doesn’t always contain a Temperature key.

1 Like

That’ll be it. It fires humidity only about 3 times then both together

Here’s a simple way to mitigate this:

  - platform: mqtt
    name: temperature
    state_topic: "tele/tasmota_377AE4/SENSOR"
    value_template: "{{ value_json.TuyaSNS.Temperature if value_json.TuyaSNS.Temperature is defined else states('sensor.temperature') }}"
    device_class: temperature
    unit_of_measurement: '°C'

If value_json.TuyaSNS.Temperature is defined then it reports the received value, if it’s not then it reports the existing value by using states('sensor.temperature').

2 Likes

@123 [Deleted a cross post] Sorry i think we posted at the same time. I’m going to try this on my case: CPU temp via telegraf-mqtt: templating MQTT sensors

I’ll report back… thank you!

edit: I was successful, such a straightforward answer which now seems to make total sense, so thanks again @123 !

legend, thank you! man I’ve got some learning to do

1 Like

Glad to hear it solves the original problem.

Please consider marking my post (above) with the Solution tag. Only you, the author of this topic, can do that. It will automatically place a checkmark next to the topic’s title which signals to other users that this topic is resolved. This help users find answers to similar questions.

1 Like

will do, it came up unavailable at first, I added a space after states
not sure if it needed it or it was coincidence but it showed after another restart

Not needed.

The sensor’s value will initially be unknown until the first payload is received. Afterwards it will be either the received payload or its last value.


NOTE

Whatever Tasmotized device you have, that is publishing humidity and temperature, ought to be configured to publish its payload as a retained message. If it doesn’t then the humidity sensor you have will report unknown after a restart (of either Home Assistant or the MQTT Broker).

From here:

SensorRetain
0 = disable use of sensor MQTT retain flag (default)
1 = enable MQTT retain flag on message tele/%topic%/SENSOR

1 Like