some of us have a thing about units, data and graphs

The Home Assistant sensor graph offers painless autoscaling with options to set detail and timespan. But how do I alter the data itself?

A car fuel tank measures in gallons but it’s more useful to know how many miles we can travel or how much fuel the tank needs to fill up. When collecting data, sometimes you want to change say, a Watt value into kW or a cost in $ or £. When my oil tank sensor gives me a distance (cm) I need to turn that into a volume of oil (in litres/gallons).

I’m getting ahead of my skills but mention this now to remind that useful calculations are possible. Some sensor calculations and adjustments are easier to code than others. The wider community may even have shared their working. There are add-ons (I think Grafana is one) which do maths. Otherwise you must use the built-in Home Assistant template platform (and really finicky awful Jinga code) to do calculations.

Home Assistant’s filter platform (read here) applies maths functions to data – it calculates a moving average, or it throttles or removes outlying values. A low-pass filter smooths a graph’s peaks and valleys. The beauty of the trick is you take any sensor data and rework it, which in this case makes the graph line less ragged. For example:

# Use of a filter to select only some of the data measured 
sensor:
  - platform: filter 
    name: "bathroom_humidity" 
    entity_id: sensor.bathroom_humidity 
    filters: 
      - filter: lowpass
        time_constant: 10
        precision: 2 
# Can even use a template to choose a different sensor attribute  
  - platform: template     
    sensors:
      sun_angle:         
        friendly_name: "sun elevation"         
        unit_of_measurement: 'degrees'         
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"       
#   see https://www.home-assistant.io/integrations/template/
Really interesting graphs showing the sun’s angle over a couple of days. This is noiseless data. It doesn’t need smoothing.
Graphs showing ambient temperature levels. This data is also noiseless. The graphs are only ugly because the resolution of the sensors is low. Notice that the temperature is measured to 0.2 or 0.5 degrees, hence the bumpiness. A filter is available should you want to see a smooth graph – but you will lose useful information if you want to make it look nice.

smoothing a graph is like turning a sharp hi-res TV into a blurry old low-res TV

RF
Important: two graphs where smoothing might not be the best option. If you smooth these, a lot of information will be averaged and effectively lost. For example, the power graph tells me that a storage heater twice got hot enough during the night. Also I might investigate those many spikes of high usage. Some of the pin sharp spikes are probably me making a tea with an electric kettle.

Leave a Reply

Your email address will not be published. Required fields are marked *