How to declare Xiaomi aquara temperatur sensor with zigbee2mqtt?

I have configured zigbee2mqtt and paired a Xiaomi aquara temperatur sensor. The discovery shows 5 new sensors:


How would I configure those sensors (in the HA configuration.yaml?) to have nicer names and i.e. round some displayed values (20,7 instead of 20,68).

When I declare i.e. the temperature value like shown below, I see the sensor two times:

sensor:
  - platform: mqtt
    name: "Xiaomi_temperature"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: '°C'
    value_template: "{{ value_json.temperature }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "temperature"

I know this to change the displayed name:
image

Maybe someone can help or share a configuration?

You don’t need to configure anything as it shows up already, automagically.

Yes, but I would like to have other names and round or manipulate some values.

And the values reported are:

temperature 20.44
pressure 1024
humidity 37.52
voltage 3005
battery 100
linkquality 70

But there is no voltage sensor discovered

Sorry I misread your message. I’ll revert to you if I figure it out.

1 Like

The documentation for Zigbee2MQTT explains how to enable automatic MQTT discovery for Home Assistant. If you want complete control of the configuration of every sensor, I suggest you disable this feature in zigbee2MQTT. You can then define the sensors manually (as you have already started to do) thereby giving complete control over their operation.

If you want to round the temperature values, you can do this:

    value_template: "{{ value_json.temperature | round(1) }}"

If it doesn’t work, it means the temperature is not received as a numeric value but as a string so you first have to convert it to a floating-point number before rounding it:

    value_template: "{{ value_json.temperature | float | round(1) }}"

If you switch to defining the sensors manually and still see duplicated sensors, it’s because the auto-discovery process has recorded the sensors in the entity registry. You will need to delete the auto-discovered versions from the entity registry (Configuration > Entity Registry. Click on an entity, a popup appears, click Delete).

2 Likes

@123: Thanks for your reply. Got it to run.
For those who are looking for the same…
sensors.yaml:

 - platform: mqtt
    name: "Wohnzimmer Temperatur"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: '°C'
    value_template: "{{ value_json.temperature | round(1) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "temperature"
    
  - platform: mqtt
    name: "Wohnzimmer Batterie"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: '%'
    value_template: "{{ value_json.battery }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "battery"
    
  - platform: mqtt
    name: "Wohnzimmer Luftdruck"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: 'hPa'
    value_template: "{{ value_json.pressure | round(0) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "pressure"
    
  - platform: mqtt
    name: "Wohnzimmer Spannung"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: 'V'
    value_template: "{{ value_json.voltage | multiply(0.001) }}"
    availability_topic: "zigbee2mqtt/bridge/state"

  - platform: mqtt
    name: "Wohnzimmer Linkqualität"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: '%'
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"

  - platform: mqtt
    name: "Wohnzimmer Luftfeuchtigkeit"
    state_topic: "zigbee2mqtt/Xiaomi"
    unit_of_measurement: '%'
    value_template: "{{ value_json.humidity | round(0) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "humidity"

groups.yaml:

Wohnzimmer_group:
  name: Wohnzimmer
  entities:
  - sensor.wohnzimmer_temperatur
  - sensor.wohnzimmer_batterie
  - sensor.wohnzimmer_luftdruck
  - sensor.wohnzimmer_spannung
  - sensor.wohnzimmer_linkqualitat
  - sensor.wohnzimmer_luftfeuchtigkeit

Results in:

1 Like

Hi @hajo62 . I’m considering doing what you’re doing for maximum flexibility. Are you still managing all your devices manually like this in config files? if so can you weigh in on the pros and cons? one immediate one that I can see is that there’s no longer a “device”, but just a group of entities?

Meanwhile I have changed to a docker setup. Here as example my docker-compose.yaml:

  zigbee2mqtt:
    container_name: zigbee2mqtt
    image: koenkk/zigbee2mqtt
    depends_on:
      - mqtt
    volumes:
      - /home/hajo/docker-volumes/zigbee2mqtt/data:/app/data
    devices:
      - /dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B00193648CA-if00
    restart: unless-stopped
    network_mode: host
    environment:
      - TZ=Europe/Berlin
      #- DEBUG=zigbee-herdsman*

My zigbee2mqtt configuration.yaml looks like this:

homeassistant: true
permit_join: true
mqtt:
  base_topic: zigbee2mqtt
  server: 'mqtt://192.168.178.3'
serial:
  port: >-
    /dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B00193648CA-if00
devices:
  '0x00158d0002b51ffff':
    friendly_name: XiaomiAqara-1
    retain: true
  '0x00158d00025effff':
    friendly_name: XiaomiAqara-2
    retain: true
  '0x00158d0002e2ffff':
    friendly_name: XiaomiSmart-1
    retain: true

With this my sensors are detected. But I want to correct some measured values and round the results. Therefore I still have defined the sensors in the sensors.yaml file; I haven’t found another way to correct or round. But unfortunately this creates new sensors and I’ve asked, if this is the best way.

# Xiaomi Temperatur-, Luftfeuchtigkeits und Luftdruck-Sensor #1
# Terrassse
  - platform: mqtt
    name: "XiaomiAqara-1 Temperature"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: '°C'
    value_template: "{{ value_json.temperature | round(1) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "temperature"

  - platform: mqtt
    name: "XiaomiAqara-1 Humidity"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: '%'
    value_template: "{{ (value_json.humidity - 0.2) | round(0) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "humidity"

  - platform: mqtt
    name: "XiaomiAqara-1 Pressure"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: 'hPa'
    value_template: "{{ (value_json.pressure + 9.0) | round(0) }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "pressure"
    
  - platform: mqtt
    name: "XiaomiAqara-1 Battery"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: '%'
    value_template: "{{ value_json.battery }}"
    availability_topic: "zigbee2mqtt/bridge/state"
    device_class: "battery"
    
  - platform: mqtt
    name: "XiaomiAqara-1 Voltage"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: 'V'
    value_template: "{{ value_json.voltage | multiply(0.001) | round(2) }}"
    availability_topic: "zigbee2mqtt/bridge/state"

  - platform: mqtt
    name: "XiaomiAqara-1 Linkquality"
    state_topic: "zigbee2mqtt/XiaomiAqara-1"
    unit_of_measurement: '%'
    value_template: "{{ value_json.linkquality }}"
    availability_topic: "zigbee2mqtt/bridge/state"

  - platform: template
    sensors:
      xiaomiaqara_1_last_changed:
        value_template: >
          {% set values = [
            states.sensor.xiaomiaqara_1_temperature.last_changed,
            states.sensor.xiaomiaqara_1_humidity.last_changed,
            states.sensor.xiaomiaqara_1_pressure.last_changed,
            states.sensor.xiaomiaqara_1_linkquality.last_changed,
            states.sensor.xiaomiaqara_1_voltage.last_changed,
            states.sensor.xiaomiaqara_1_battery.last_changed, ] %}
          {{ values | max }}
        device_class: timestamp

@hajo62 thanks for answering. So you are not actually able to see the device of those sensors right? You need to group them manually? My question is more about being able to see the entities grouped by device and less about general config. I’ll take a look at your question.

I do not use groups (the groups.yaml file) nor the area feature.
I manually group my sensors in the lovelace ui:

@hajo62 I understand that, thanks for sharing. These sensors are coming from one physical device I guess, so the question is: does the device appear under “Devices” when you go to your device-list in HA? Because I followed what you did and I can play with the different sensors of the same device, but the device itself isn’t recognized as a device.

No. I only see the doubled devices coming from my sensors.yaml file.

I believe you are seeing each device twice because you are defining them twice. The first time is when you manually define a device in sensors.yaml (as an MQTT Sensor). The second time is via MQTT Discovery because you have configured this ZigBee2MQTT option:

homeassistant: true

That instructs ZigBee2MQTT to publish its devices in a way that Home Assistant discovers them automatically.

If you want convenience then MQTT Discovery is a good choice. However, if you want greater control over each device’s definition, then manual configuration is preferable. If you choose both, you will get duplication.

1 Like

Okay, but…
when I now (after changing homeassistant: false) try to delete the entities, it says I can’t delete without removing the (I assume MQTT) integration first.

When I do that, I will still have my defined sensor from sensors.yaml running?

If you changed it the option to homeassistant: false it means Zigbee2MQTT will no longer creates devices via MQTT Discovery. You can confirm this by using an MQTT client like MQTT Explorer and look for topics similar to this:

homeassistant/sensor/my_sensor_name/config

There should be none.

Any entities that were originally created by MQTT Discovery will be automatically deleted. What will remain are the entities you had defined manually. Those won’t disappear unless you manually delete their configurations.

1 Like

Thanks for explaining that again. :+1:
I had that in the past but forgot that on my new setup. Now it’s like I want it to be: Only one sensor per sensor and value corrected and rounded.

1 Like