Rate limiting temperature/humidity sensors that generate too many database entries

I recently installed some Govee BLE temperature/hygrometer sensors into my hass installation. The ad copy for the sensors on Amazon said 2 seconds response time, and they sure meant it! This sensor reports many times per minute and will easily generate up to 1,440 database entries per day per sensor. This wastes DB space, slows history generation and increases the number of SD card writes.

If you have template sensors that are derived from these sensors, then the result can be multiplicative. A derived sensor measuring the difference in humidity from two different Govee’s may change state once per second if both sensors are jittering between two different values each poll.

By default the Govee sensors report temperatures in Fahrenheit to 2 decimal places X.XX and humidity to one decimal place XX.X. For the purposes of my automations, I don’t need 2 second update intervals and tenth to one hundredth levels of measurement when the hysteresis of the on/off for my automations is 2 degrees F and 4% relative humidity. Most of the database traffic is going to be irrelevant jitter in the measurements.

So here is how to reduce the number of states that go into the database.

  1. Rename the offending sensor entity_ids into a namespace that can be easily excluded via the recorder: section of configuration.yaml
recorder:
  purge_keep_days: 45 
  db_url: XXX
  exclude:
    entity_globs:
      - sensor.govee_*
  1. Create trigger-based template sensors that will periodically poll the source entities, using a time_pattern as a trigger and apply rounding to the sensor states. These template sensors replace the raw Govee entities for the purposes of graphing historic data.
template:
  - trigger:
      - platform: time_pattern
        seconds: 0
      - platform: homeassistant
        event: start
    sensor:
      - name: Upstairs Temperature
        unique_id: GOVEE_LR_TEMP_FILTERED
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: 'measurement'
        state: "{{states('sensor.govee_upstairs_temperature')|round(1,default='unavailable')}}"
      - name: Upstairs Humidity
        unique_id: GOVEE_LR_HUMID_FILTERED
        unit_of_measurement: "%"
        device_class: 'humidity'
        state: "{{states('sensor.govee_upstairs_humidity')|round(0,default='unavailable')}}"

The result should be the number state changes of these sensors should be reduced to a much more reasonable number.

2 Likes