Problem with OpenMQTTGateway / MQTT Sensor and Xiaomi MIJIA

Hey Guys,

i’m using OpenMQTTGateway to read the temperature and humidity from Xiaomi Mijia hygrothermometer and reading it in homeassistant as mqtt sensor.
Configuration:

   - platform: mqtt
     name: "Temperatur Baby"
     state_topic: "home/OMG_Baby/BTtoMQTT/582D3431C7BF"
     unit_of_measurement: '°C'
     value_template: "{{ value_json.tem }}"
 
   - platform: mqtt
     name: "Luftfeuchtigkeit Baby"
     state_topic: "home/OMG_Baby/BTtoMQTT/582D3431C7BF"
     unit_of_measurement: '%'
     value_template: "{{ value_json.hum }}"

It works almost fine. The problem is, that sometimes the gateway sends both, the temp and humidity in one publish and sometimes only one of both values, which leads to an empty value of the “not sent” item.

Examples:

home/OMG_Baby/BTtoMQTT/582D3431C7BF {“hum”:53.5}
or

home/OMG_Baby/BTtoMQTT/582D3431C7BF {“hum”:53.5,“tem”:24.5}
or
home/OMG_Baby/BTtoMQTT/582D3431C7BF {“tem”:24.5}

Is there a way to check if “none” was sent in the last message and keep the last sent value?

Thanks in advance!

Hello,

I think you have to follow this example of value_template :
{{ value_json.tem | is_defined }}

Note also that if you uncomment Zmqttdiscovery, OMG should generate the sensors in HA with the good definition.

Has anyone else had an issue with core-2022.2.0 that now the entity state changes to the entire json string whenever a message is sent without the specified value_json value.

I haven’t updated yet, let me try

ouch, I confirm the bug, screenshots at different time:
image

image

I have posted a bug report

There is also another thread in which is discuss is_defined that may be of some use.

Any idea of an alternative template workaround in the meantime?

1 Like