jscruz / sensor.carbon_intensity_uk

Carbon Intensity UK Sensor for Home Assistant
MIT License
8 stars 11 forks source link

Adding Future Forecast Graph #6

Open dr-harper opened 2 years ago

dr-harper commented 2 years ago

Is your feature request related to a problem? Please describe. I thought it would be useful to be able to display a future value of the Carbon Intensity as a graph, to compliment your table. I am sharing here an MVP idea.

Seems possible that it could be cleaned up slightly and then integrated?

Describe the solution you'd like Using ApexCharts it is possible to display future data using the data_generator option. Please note I have very poor Javascript knowledge so I actually just copied in all the values below, but I am sure this could be made with a loop:

type: custom:apexcharts-card
graph_span: 2d
header:
  show: true
  title: Carbon Intensity Forecast
now:
  show: true
span:
  start: hour
series:
  - entity: sensor.carbon_intensity_uk
    name: Forecast
    data_generator: >
      return [[entity.attributes.forecast[0].from,
      entity.attributes.forecast[0].intensity],
      [entity.attributes.forecast[1].from,
      entity.attributes.forecast[1].intensity],
      [entity.attributes.forecast[3].from,
      entity.attributes.forecast[3].intensity],
      [entity.attributes.forecast[4].from,
      entity.attributes.forecast[4].intensity],
      [entity.attributes.forecast[5].from,
      entity.attributes.forecast[5].intensity],
      [entity.attributes.forecast[6].from,
      entity.attributes.forecast[6].intensity],
      [entity.attributes.forecast[7].from,
      entity.attributes.forecast[7].intensity],
      [entity.attributes.forecast[8].from,
      entity.attributes.forecast[8].intensity],
      [entity.attributes.forecast[9].from,
      entity.attributes.forecast[9].intensity],
      [entity.attributes.forecast[10].from,
      entity.attributes.forecast[10].intensity],
      [entity.attributes.forecast[11].from,
      entity.attributes.forecast[11].intensity],
      [entity.attributes.forecast[12].from,
      entity.attributes.forecast[12].intensity],
      [entity.attributes.forecast[13].from,
      entity.attributes.forecast[13].intensity],
      [entity.attributes.forecast[14].from,
      entity.attributes.forecast[14].intensity],
      [entity.attributes.forecast[15].from,
      entity.attributes.forecast[15].intensity],
      [entity.attributes.forecast[16].from,
      entity.attributes.forecast[16].intensity],
      [entity.attributes.forecast[17].from,
      entity.attributes.forecast[17].intensity],
      [entity.attributes.forecast[18].from,
      entity.attributes.forecast[18].intensity],
      [entity.attributes.forecast[19].from,
      entity.attributes.forecast[19].intensity],
      [entity.attributes.forecast[20].from,
      entity.attributes.forecast[20].intensity],
      [entity.attributes.forecast[21].from,
      entity.attributes.forecast[21].intensity],
      [entity.attributes.forecast[22].from,
      entity.attributes.forecast[22].intensity],
      [entity.attributes.forecast[23].from,
      entity.attributes.forecast[23].intensity],
      [entity.attributes.forecast[24].from,
      entity.attributes.forecast[24].intensity],
      [entity.attributes.forecast[25].from,
      entity.attributes.forecast[25].intensity],
      [entity.attributes.forecast[26].from,
      entity.attributes.forecast[26].intensity],
      [entity.attributes.forecast[27].from,
      entity.attributes.forecast[27].intensity],
      [entity.attributes.forecast[28].from,
      entity.attributes.forecast[28].intensity],
      [entity.attributes.forecast[29].from,
      entity.attributes.forecast[29].intensity],
      [entity.attributes.forecast[30].from,
      entity.attributes.forecast[30].intensity],
      [entity.attributes.forecast[31].from,
      entity.attributes.forecast[31].intensity],
      [entity.attributes.forecast[32].from,
      entity.attributes.forecast[32].intensity],
      [entity.attributes.forecast[33].from,
      entity.attributes.forecast[33].intensity],
      [entity.attributes.forecast[34].from,
      entity.attributes.forecast[34].intensity],
      [entity.attributes.forecast[35].from,
      entity.attributes.forecast[35].intensity],
      [entity.attributes.forecast[36].from,
      entity.attributes.forecast[36].intensity],
      [entity.attributes.forecast[37].from,
      entity.attributes.forecast[37].intensity],
      [entity.attributes.forecast[38].from,
      entity.attributes.forecast[38].intensity],
      [entity.attributes.forecast[39].from,
      entity.attributes.forecast[39].intensity],
      [entity.attributes.forecast[40].from,
      entity.attributes.forecast[40].intensity],
      [entity.attributes.forecast[41].from,
      entity.attributes.forecast[41].intensity],
      [entity.attributes.forecast[42].from,
      entity.attributes.forecast[42].intensity],
      [entity.attributes.forecast[43].from,
      entity.attributes.forecast[43].intensity],
      [entity.attributes.forecast[44].from,
      entity.attributes.forecast[44].intensity],
      [entity.attributes.forecast[45].from,
      entity.attributes.forecast[45].intensity],
      [entity.attributes.forecast[46].from,
      entity.attributes.forecast[46].intensity],
      [entity.attributes.forecast[47].from,
      entity.attributes.forecast[47].intensity]];

image

Describe alternatives you've considered I googled around for other graphing libraries that can show future values in Home Assistant but this seemed like one of the best.

dr-harper commented 2 years ago

P.S. I wasn't sure which GitHub repository is the most up to date but @jfparis might also be interested: https://github.com/jfparis/sensor.carbon_intensity_uk

jfparis commented 2 years ago

Hi @dr-harper

Yes, I have also done some graphs in the fork you mentioned above. (I just added the code for the card)

It might be worth merging with @jscruz but I might have done a bit too much dirty hacking (merging underlying python client for the API into a single dirty blob :smile: )

jscruz commented 2 years ago

@dr-harper, it's a great idea. @jfparis, I'll check the code in your repository soon and see what is the best way to add your contribution. Thanks!