flyte / mqtt-io

Expose GPIO modules (Raspberry Pi, Beaglebone, PCF8754, PiFace2 etc.) and digital sensors (LM75 etc.) to an MQTT server for remote control and monitoring.
MIT License
464 stars 157 forks source link

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

Open a-x- opened 2 years ago

a-x- commented 2 years ago

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

System: does not matter

Additional context

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

MrC0nst commented 2 years ago

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 }

stokket commented 2 years ago

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

sensor_modules:

sensor_inputs:

MrC0nst commented 2 years ago

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

OzGav commented 2 years ago

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

lucianojss commented 1 year ago

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