landonr / homeThing

Wifi Smart Home Remotes that run on ESPHome
https://homething.io
GNU General Public License v3.0
454 stars 35 forks source link

Graphs for sensors #30

Open abroooo opened 1 year ago

abroooo commented 1 year ago

Is your feature request related to a problem? Please describe. Sensors currently only show numeric values.

Describe the solution you'd like By pressing the sensor menu item it could show a graph over the last x min/hours

Describe alternatives you've considered Instead of drawing it could be a solution to receive a png (from HA) and display that. I wouldn't prefer showing pngs because it would require external configuration and I'm not sure it's possible with our device.

Additional context I investigated a bit and it;s it pretty doable to show a graph. The tricky part is getting the data to show. Natively esphome only supports collecting values over a period of time and show them in a graph. But I would like to see a graph for a sensor even if I just turned on the remote (or return it from deep sleep). Maybe this could be resolved by having ha send an array of values on demand but it would mean external configuration. AFAIK ha doesn't support that natively.

landonr commented 1 year ago

i tried the esphome png library and its super slow. I don't know if theres any alternatives for jpg or bmp https://github.com/esphome/esphome/pull/3255

you can get history from REST so we can try that. Decoding the REST might make the device lag for a second but it could work https://developers.home-assistant.io/docs/api/rest/

we might need to make our own graph by drawing points because the esphome graph just takes in a sensor

abroooo commented 1 year ago

Oh the ha rest api, nice idea - havent thought of that to be honest 👍 I don't think we need to implement our own graph. esphome::display::DisplayBuffer::graph() takes a pointer to an esphome::graph::Graph object. We can build this object ourselves.

[edit] fix typo

abroooo commented 1 year ago

I played a bit with esphome's httprequest (only via yaml not in code) but so far I couldn't get it to work with ha. It seems it doesn't like Bearer Authentication. I haven't looked into the esphome code though.

This works fine: curl -X GET -H "Authorization: Bearer <token>" -H "Content-Type: application/json" "http://<ip>:8123/api/history/period/2022-11-22T10:00:00+00:00?filter_entity_id=sensor.my_sensor"

but this fails with 401 - not authorized:

       - http_request.get:
           url: 'http://<ip>:8123/api/history/period/2022-11-22T10:00:00+00:00?filter_entity_id=sensor.my_sensor'
           headers:
             Authorization: 'Bearer <token>'
             Content-Type: application/json
           verify_ssl: false
           on_response:
             then:
               - logger.log:
                   format: 'Response status: %d'
                   args:
                    - status_code

I'll play some more and when I get it to work I'm really curious to see the performance parsing a that json 😆

landonr commented 1 year ago

are you using a long lived access token? https://developers.home-assistant.io/docs/auth_api/#making-authenticated-requests

abroooo commented 1 year ago

Yes I am

abroooo commented 1 year ago

After poking through the esphome code to no avail I found the error - it was the token 🤦‍♂️ I somehow missed a character. After fixing the token it works fine....

landonr commented 1 year ago

nice! pics

abroooo commented 1 year ago

yeah I'm pretty much at the beginning so there aren't any pics yet - sorry 😞

I think I'll approach it like this:

landonr commented 1 year ago

this is really interesting https://github.com/esphome/esphome/pull/3255#issuecomment-1397300681 this person loads a graph as a png created from https://weewx.com/ I think loading the json from history and drawing the graph on the device is the right move, but this is an interesting approach/hack

abroooo commented 1 year ago

oh yeah that's nice indeed. I agree, pulling the data from HA and visualizing it on device is the way to go. btw. it will take me some more time as life keeps me pretty busy atm 😐