History chart or graph for hardware display in ESPHome

I love using ESPHome and I’ve used it with hardware displays (like the ssd1306). It works very well for multiple pages and using fonts/text. I really want to make a chart of temperature history (like history graph in Home Assistant but on a hardware display), to show like last 12 hours or something. I’ve looked though the docs and it doesn’t seem to be supported. Does anyone know how to go about doing that with ESPHome?

I think you should convert the chart to an image (png or jpeg probably) and then display that.

Yeah, I hadn’t thought of doing it that way. It’d probably work but it feels so low-tech. Wouldn’t you rather have live data from ESPHome into a native chart? Thinking through the chart to image idea, I’m guessing you’re thinking:

  1. taking the data from the ESPHome device out to Home Assistant
  2. building a chart
  3. saving as a jpg or png
  4. then picking that up with ESPHome somehow to display it?

ugh. My brain hurts just thinking about that process… Is that what you were thinking or is there a simpler way?

The live data is already in home assistant, but yeah that is the process.

I know someone did this for an e-ink display, but I can’t find the thread now. Of course e-ink is a different kettle of fish, and quite slow to update.

If I find that thread I’ll post it here.

Of course there are probably libraries for arduino that can draw graphs, but esphome doesn’t really hold data more than ephemerally, ie there is no history, except on the HA machine.

A custom c++ code would be needed in your display lambda.

Just musing here, don’t ask me to implement it!

1 Like

Here you go!

http://users.telenet.be/goosst/epaper/

there is actually a way to store historic values in ESPHome directly:

  1. Create a globals array
  2. In the sensor statement, update that array with an on_value statement
  3. Use the array values to print historic values.

Please be aware that arrays in ESPHome are barely documented… :nerd_face:

Here are some code sniplets that worked for me:

globals array

globals:
   - id: myhistory
     type: float[10]
     restore_value: no
     initial_value: "{'0','1','2','3','4','5','6','7','8','9'}"

sensor statement filling historic values on_value updates

sensor:
  - platform: homeassistant
    id: mysensor
    entity_id: sensor.whatever_sensor_you_have
    internal: true
    on_value:
      then:
        - lambda: |-
            for (int i = 9; i > 0; i--) {
              id(myhistory)[i] = id(myhistory)[i-1];
              }
            id(myhistory)[0] = id(mysensor).state;

This worked well to show some historic values on an M5 stack.

I will try some custom c++ code for some minigraphs next.

3 Likes

Thanks @Fronk this is a really awesome base setup for the chart data!

v2021.10 will include a graph component that can be used for this.

Holy hell!! This is awesome: Display Component — ESPHome

Thanks for the notice @oxan!

i have tried this, but my graph is empty.

This method is correct, I have used it.

Apologies for resurrecting an old thread; but this is exactly the problem I’m facing. I can see how the solution that @Fronk proposed can capture a historic record of a sensors readings that will populate over time. Similarly I can see that same functionality is now built directly into the graph component of the ESPHome display support.

But in both cases, if I want to draw a constantly updating graph of (say) the last 24 hours of data, then for the first 24 hours after starting the ESPHome device, I won’t have all the data I need. Clearly I could just draw the graph with whatever data I do have, autoscaling the X-axis as appropriate, or just let the graph “build” over time, but I’d prefer to not do either of those things.

After all, I’m connected to HA that has a complete historical record of all my sensors readings in its history database. So is it possible to load an appropriate amount of historical data into ESPHome from HAs history? In an ideal world, this would be done in a manner that would allow me to still use the graph component, but if not, I expect I can do the graphing myself if I can get the data into an array.

Any ideas?

Yes, on_boot load the historical data into the esp using the ha SQL SQL - Home Assistant

Then @Fronk 's method should be pre populated.

1 Like

Thanks @nickrout I’ve not seen that integration before - but if I read the docs correctly I think I can only extract individual values using that SQL integration.

What you have done though is remind me we have a REST API, and it looks like I can get all the data from a period of time using the GET /api/history/period/ action. Would be nice if I can use that to precache the native “traces” data structures in the graph component, but if not I can just draw the graphs myself

I also want to be able to keep a certain amount of history for a sensor, but figure that making it the graph’s job to store the data is crossing concerns a bit too much. My use case is to make history available for the new improved web_server: component, which doesn’t need the display: or graph: components.

My thinking is to define a SensorHistory class, which can be a field of sensor::Sensor, and configured under the sensor entity using something like:

sensor:
  - platform: template
    history: # 24 hours
      size: 24 # entries
      interval: 1 hour # between entries

Then both web server and graph/display components can draw from the history as required. And with the storage of the history standardised, it might then make sense to expose a HomeAssistant statistics service that can easily provide data that matches the specified history duration/interval, to make it simple to repopulate on startup if desired.