Display future values in a graph

I have a JSON API that I want to consume and display the values in a graph in my dashboard.
It generates JSON like:


  "help": "https://www.energidataservice.dk/api/3/action/help_show?name=datastore_search_sql",
  "success": true,
  "result": {
    "records": [
      {
        "Minutes5DK": "2021-03-22T23:55:00",
        "Minutes5UTC": "2021-03-22T22:55:00+00:00",
        "CO2Emission": 135,
        "PriceArea": "DK2"
      },
      {
        "Minutes5DK": "2021-03-22T23:50:00",
        "Minutes5UTC": "2021-03-22T22:50:00+00:00",
        "CO2Emission": 134,
        "PriceArea": "DK2"
      },
      {
        "Minutes5DK": "2021-03-22T23:45:00",
        "Minutes5UTC": "2021-03-22T22:45:00+00:00",
        "CO2Emission": 134,
        "PriceArea": "DK2"
      }
    ],

And I would like to display CO2Emission values with the Minutes5DK as X axis.

I’ve been able to pull in the data with the rest platform:

  - platform: rest
    name: "Co2 prognose"
    resource: https://www.energidataservice.dk/proxy/api/datastore_search_sql?sql=SELECT%20%22Minutes5UTC%22,%20%22Minutes5DK%22,%20%22PriceArea%22,%20%22CO2Emission%22%20FROM%20%22co2emisprog%22%20WHERE%20%22PriceArea%22%20=%20%27DK2%27%20ORDER%20BY%20%22Minutes5UTC%22%20DESC%20LIMIT%203
    json_attributes:
      - records[0].Minutes5DK
      - records[0].CO2Emission
    value_template: 'OK'
    unit_of_measurement: "g/kWh"

But that’s about it.
I have no idea how to store multiple values in my entity (As attributes?)
from there I would imagine I can use ApexCharts card

Has anyone done something similar?

2 Likes

What you need is to set the complete content of result.records in an attribute.

json_attributes_path: '$.result'
json_attributes:
  - records

And then use data_generator apexcharts-card to display the content of the records attribute

1 Like

Brilliant. I thought there was a limit on the amount of data that could sit in the attributes. But it works nicely.

For the curious this is the result and the apex graph:
image

type: 'custom:apexcharts-card'
graph_span: 24h
span:
  start: day
now:
  show: true
  label: Now
header:
  show: true
  title: Co2 Prognose (g/kWh)
series:
  - entity: sensor.co2_prognose
    stroke_width: 3
    type: area
    opacity: 0.5
    color: '#730712'
    data_generator: |
      return entity.attributes.records.map((record, index) => {
              return [record.Minutes5DK, record.CO2Emission];
            });

4 Likes

If the chart is glitchy like it seems to be, it’s because the data needs to be ordered by time. So you can do it this way:

      return entity.attributes.records.map((record, index) => {
        return [new Date(record.Minutes5DK).getTime(), record.CO2Emission];
      }).sort((a, b) => { return a[0] - b[0] });

Yeah. that made it much nicer. Thanks

2 Likes

For those in the netherlands that use ANWB energie :grin:

series:
  - entity: sensor.anwb_stroomprijzen
    data_generator: |
      return entity.attributes.Prices.map((record, index) => {
              return [record.readingDate, record.price];
            });
    type: column
    stroke_width: 5
1 Like

I have the same usecase, but I cannot seem to make it work.

I’m trying to pull the data like this

rest:
  - authentication: basic
    scan_interval: 90
    resource: https://api.awattar.de/v1/marketdata
    sensor:
      - name: "Spotpreise"
        json_attributes_path: '$'
        value_template: "OK"
        json_attributes:
          - data

the url delivers a JSON much like the one of the OP:

{
  "object": "list",
  "data": [
    {
      "start_timestamp": 1676394000000,
      "end_timestamp": 1676397600000,
      "marketprice": 199.99,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676397600000,
      "end_timestamp": 1676401200000,
      "marketprice": 192.97,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676401200000,
      "end_timestamp": 1676404800000,
      "marketprice": 175.81,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676404800000,
      "end_timestamp": 1676408400000,
      "marketprice": 160.09,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676408400000,
      "end_timestamp": 1676412000000,
      "marketprice": 151.03,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676412000000,
      "end_timestamp": 1676415600000,
      "marketprice": 141.73,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676415600000,
      "end_timestamp": 1676419200000,
      "marketprice": 134.81,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676419200000,
      "end_timestamp": 1676422800000,
      "marketprice": 130.95,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676422800000,
      "end_timestamp": 1676426400000,
      "marketprice": 130,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676426400000,
      "end_timestamp": 1676430000000,
      "marketprice": 130.01,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676430000000,
      "end_timestamp": 1676433600000,
      "marketprice": 126.66,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676433600000,
      "end_timestamp": 1676437200000,
      "marketprice": 136.53,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676437200000,
      "end_timestamp": 1676440800000,
      "marketprice": 161.2,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676440800000,
      "end_timestamp": 1676444400000,
      "marketprice": 185,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676444400000,
      "end_timestamp": 1676448000000,
      "marketprice": 188.76,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676448000000,
      "end_timestamp": 1676451600000,
      "marketprice": 171.22,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676451600000,
      "end_timestamp": 1676455200000,
      "marketprice": 152.52,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676455200000,
      "end_timestamp": 1676458800000,
      "marketprice": 132.16,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676458800000,
      "end_timestamp": 1676462400000,
      "marketprice": 120.79,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676462400000,
      "end_timestamp": 1676466000000,
      "marketprice": 121.45,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676466000000,
      "end_timestamp": 1676469600000,
      "marketprice": 121.38,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676469600000,
      "end_timestamp": 1676473200000,
      "marketprice": 128.71,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676473200000,
      "end_timestamp": 1676476800000,
      "marketprice": 144.99,
      "unit": "Eur/MWh"
    },
    {
      "start_timestamp": 1676476800000,
      "end_timestamp": 1676480400000,
      "marketprice": 158.8,
      "unit": "Eur/MWh"
    }
  ],
  "url": "/de/v1/marketdata"
}


but it doesn’t work.
no matter what configuration i try i get the same log entry:

 Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:170
Integration: RESTful (documentation, issues)
First occurred: 17:53:56 (35 occurrences)
Last logged: 18:45:16
JSON result was not a dictionary or list with 0th element a dictionary

has anyone an idea?
Any help is appreciated!

This is awkward… I solved it myself… after 5min more of fiddeling.
So for the record:

In my case the

json_attributes_path: '$'

was the issue. I just left it out:

rest:
  - authentication: basic
    scan_interval: 90
    resource: https://api.awattar.de/v1/marketdata
    sensor:
      - name: "Spotpreise"
        value_template: "OK"
        json_attributes:
          - data

all data is here now

Hi,

I have been trying for days to get this to work
tried dozens of combinations of below.

Just rest without the adding to the attributes, using below, give the correct data, e.g. gives the 7th number in the list of 100 or so values the json returns. tested in templates & all seem to come back correct.
value_template: ‘{{ value_json[“hourly”][“diffuse_radiation”][7] }}’

trying to display in apex chart just says loading …

Any ideas, I was to have chart of the next x hours forecast. mostly solar for the moment.

- platform: rest
  scan_interval: 90
  resource: https://api.open-meteo.com/v1/forecast?latitude=51.15&longitude=-0.81&hourly=direct_radiation,diffuse_radiation&windspeed_unit=mph&forecast_days=3&timezone=Europe%2FLondon
  method: GET
  name: "Open-MeteoAPI"
#  value_template: "{{ hourly.direct_radiation }}"
#  value_template: '{{ value_json["hourly"]["diffuse_radiation"] }}'
  
  value_template: 'OK'
##  json_attributes_path: '$.result'
  json_attributes:
    value_json.hourly.time
    value_json.hourly.diffuse_radiation

#    hourly.time
#    hourly.direct_radiation
#  unit_of_measurement: "W/m"

Hello Patrick,
I am new to homeassistant and also want to implement current and future prices from the awattar api. Where do I have to put the code in and do I have to install a rest addon or something to make this work?
Thank you

you need to add this to your configuration.yaml
I added individual sensors that give the value xx hours ahead

rest:
  - authentication: basic
    scan_interval: 90
    resource: https://api.awattar.de/v1/marketdata
    sensor:
      - name: "Spotpreise"
        value_template: "OK"
        json_attributes:
          - data
      - name: "Spotpreis_00h"
        json_attributes_path: '$.data.0'
        value_template: 'OK'
        json_attributes:
          - 'start_timestamp'
          - marketprice
      - name: "Spotpreis_01h"
        json_attributes_path: '$.data.1'
        value_template: 'OK'
        json_attributes:
          - 'start_timestamp'
          - marketprice
      - name: "Spotpreis_02h"
        json_attributes_path: '$.data.2'
        value_template: 'OK'
        json_attributes:
          - 'start_timestamp'
          - marketprice
      - name: "Spotpreis_03h"
        json_attributes_path: '$.data.3'
        value_template: 'OK'
        json_attributes:
          - 'start_timestamp'
          - marketprice