mqtt-io icon indicating copy to clipboard operation
mqtt-io copied to clipboard

[HA Discovery] Add state_class: measurement to config of sensor_inputs

Open a-x- opened this issue 4 years ago • 5 comments

Describe the bug It will fix ha graphs and storing

a) mqtt-io graph: EB4EB038-9241-4006-8BBA-6D68653A07A5

b) correct graph: C7B42105-3037-4F7F-9AFE-3F8E1460FDBE

Config

sensor_modules:
  - name: wc_climate_bme280
    module: bme280
    i2c_bus_num: 1
    chip_addr: 0x76

sensor_inputs:
  - name: wc_humidity
    module: wc_climate_bme280
    type: humidity
    retain: yes
    extra_announce_payload:
      device_class: "humidity"

Hardware

  • Platform: Raspberry Pi
  • Connected hardware: does not matter. In my case: bme280

System: does not matter

Additional context

HA Sensor Manual about state_class:
https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes

a-x- avatar Nov 07 '21 10:11 a-x-

Is it possible to add "unit_of_measurement" for sensors with HA autodiscovery? Temperature sensor example(from aqara temperature and humidity sensor): "unit_of_measurement": "°C", "state_class": "measurement",

Humidity sensor example: "unit_of_measurement": "%", "state_class": "measurement",

This trick will add easy-reading values to HA interface image

JSON: { "name": "BoilerRoom Humidity", "availability_topic": "boilerroom/status", "payload_available": "available", "payload_not_available": "unavailable", "device": { "manufacturer": "MQTT IO", "model": "v2.2.6", "identifiers": [ "mqtt-io", "RPi_UPS" ], "name": "Boiler Room" }, "device_class": "humidity", "unique_id": "RPi_UPS_dht22_sensor_boilerroom_humidity", "state_topic": "boilerroom/sensor/boilerroom_humidity", "unit_of_measurement": "%", "state_class": "measurement", "expire_after": 125 }

MrC0nst avatar Nov 12 '21 11:11 MrC0nst

I managed to enable this by changing following in config.yml

sensor_modules:

  • name: Temp1 module: ds18b type: DS18B20 address: 000004cd45ba

sensor_inputs:

  • name: Temp1 module: ds18b20 interval: 10 digits: 1 ha_discovery: name: Temp1 device_class: temperature unit_of_measurement: °C

stokket avatar Dec 02 '21 06:12 stokket

I managed to enable this by changing following in config.yml

sensor_modules:

  • name: Temp1 module: ds18b type: DS18B20 address: 000004cd45ba

sensor_inputs:

  • name: Temp1 module: ds18b20 interval: 10 digits: 1 ha_discovery: name: Temp1 device_class: temperature unit_of_measurement: °C

Really good solution, but i suggest to use quotes unit_of_measurement: "°C" for temperature or unit_of_measurement: "%" for humidity. Otherway you will have error raise ScannerError("while scanning for the next token", None, yaml.scanner.ScannerError: while scanning for the next token found character '%' that cannot start any token

MrC0nst avatar Dec 02 '21 14:12 MrC0nst

hey OP can you close this because it is already possible?

OzGav avatar Apr 28 '22 12:04 OzGav

As stated before you can simply do:

sensor_modules:
  - name: ds18b_module
    module: ds18b
    type: DS18B20
    address: 0000057b6ebc

sensor_inputs:
  - name: temp_sensor
    module: ds18b_module
    interval: 30
    ha_discovery:
      name: Temperature sensor
      device_class: temperature
      unit_of_measurement: °C

lucianojss avatar Nov 20 '22 00:11 lucianojss