History Graph Card plots a staircase for sensor and a spline for input_number

Now at HA 0.108.3
As discussed here, sensor values are plotted as a staircase of values. I converted some sensors to input_number to retain their values after HA restart. I was surprised to discover the input_number values are plotted in a spline. I do not see a Lovelace option to control the plotting style so I created a template sensor for plotting based on the input_number value. This works.

  - platform: template
    sensors:
      furnace_power_plot:
        friendly_name: 'Furnace Power Plot'
        unit_of_measurement: 'kW'
        value_template: '{{ states.input_number.furnace_power.state}}'

Some people want to see the spline plot for a sensor. I do not see a template input_number for a similar workaround.

3 Likes

that’s interesting.
it might depend on how you set value to your input_numbers - could you tell more on it?

input_number is set in a script using a service:

- service: input_number.set_value
        data:
          entity_id: input_number.furnace_power
          value: 7.8

This picture show a sensor(Furnace Power Plot) and input_number(Furnace Power) for the same data:
plot

For those who want a spline plot you can use an automation like this to convert a sensor to an input_number:

  - alias: Last House Temperature
    initial_state: True
    trigger:
      platform: state
      entity_id: sensor.multisensor2_temperature
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.last_house_temp
          value: "{{ states('sensor.multisensor2_temperature')}}"

I don’t know if these workarounds will work in future HA versions since (I believe) this behavior is undocumented. It is an awkward way to control the plotting line style.

2 Likes

I don’t see why you would do this though.

Your furnace power sent values and they were plotted as horizontal lines with respect to time until a new state was received. And this is truthfully represented by the “staircase” plot.

Your input number representation spline graph shows an untruthful representation of states that were never received.

Spline graphs are the interpolation of staircase data. Spline graphs in certain situations are way more representative of what’s going on in real life. Depending on the data polling/received time period. Spline is more accurate for temperature given a small time period between data points, but for power, it may not be the whole story.

3 Likes

Yes, I want a staircase plot but I wanted to use input_number to retain last known last on HA restart. I solved the spline plot problem by creating a sensor synchronized to the input_number.

Other people in the referenced post, actually want a spline plot, so I have provided a workaround for them.
(For the power steps in my furnace the staircase is reality. However, a spline would be a more realistic representation of room temperature fluctuations.)

To get more representative graphs of room temperature all you have to do is shorten the update time. 3 minutes is usually plenty fast enough:

If you change the time scale you will see the steps again. The beauty of a spline is that it connects the dots without having to over sample which makes the database larger than necessary.
Personally, I don’t mind the temperature steps.

1 Like

@tom_l graham is right - try to delete the DB and when you have a few readings, you’ll see the steps, but it’ll become smoother as instead of say, 1 hour your graph will show you 1 day so because of that stretch/compress you see/don’t see fine details.
My sensor sends readings every 50s

And yes, I know why it’s staircase style and it doesn’t really bother me but I presume for displaying in frontend some would prefer something nicer :wink:

You missed the point.

Ok, I don’t mind asking - could you explain what I missed?

With a lot of temperature sensors, you don’t get to choose update frequency, they send a value when there’s a significant difference, in order to save battery.

For under $10 you can build an ESPHome temperature and humidity sensor you can put anywhere in the house that has a power point and get updates every second if you wish.

This is not relevant to this topic. I have provided a workaround to produce a spline plot for slow changing data. You may not appreciate it but others do. Please do not claim that oversampling is a solution.

3 Likes

Hey man if you want to plot fake data knock yourself out.

What you call "fake’ is better known as interpolation. I did some research and ran a Python program
to show you different types of interpolation used when plotting graphs.
spline

On this graph, the orange dots are the real data points from the sensor.
The blue is the HA staircase. An interpolation between the data points consisting of a horizontal followed by a vertical line.
The green is the linear interpolation drawn when HA plots an input_number.
The dashed orange line is a cubic interpolation. It connects the points in an even smoother way but would probably be a challenge to implement running code on a Raspberry PI.

Here is the code used to produce the above plot:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

x = np.array([0,1,2,2,3,3,4,4,5,5,6,7])
y = np.array([0,0,0,1,1,2,2,1,1,0,0,0])
xs = np.array([0,1,2,3,4,5,6,7])
ys = np.array([0,0,1,2,1,0,0,0])

f = interp1d(xs, ys)
f2 = interp1d(xs, ys, kind='cubic')

xnew = np.linspace(0, 7, num=41, endpoint=True)
# Staircase
plt.plot(x, y)
# Splines
plt.plot(xs, ys, 'o', xnew, f(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['stair', 'cubic','linear',], loc='best')
plt.show()

You can use it to see interpolation for other data points.

I have created a new post in “Share Your Project” which is a guide for smoother plots.

6 Likes

linear plots or spline graphs used to be the default for all data plots/graphs back about 50 or so major versions ago.

2 Likes

I actually want the opposite of what is described here. I have an input_number and want to plot it as a staircase instead of a spline. The value it is plotting is the number of people at home, so interpolated values don’t make a lot of sense there and actually make the graph harder to read. Has anyone found a way of setting the chart type for a graph? Or is the only solution to make a template sensor, since those appear to be plotted as a staircase chart.

Make a template sensor.

Hi Graham, thanks! I was thinking the same, but this isn’t making a graph at all:

This is simply a template sensor with value_template set to the input_number state. Can’t imagine what I’m doing wrong here… Might you have an idea?