NEWS
[gelöst] Lovelace Apexchart data_generator aus JSON
-
Hallo!
Ich versuche verzweifelt mit der Apexchart-card mittels data_generator Daten darzustellen.
Die Daten kommen aus dem Awattar Adapter im JSON Format.Hier im Forum hat es @copystring schon geschafft zukünftige Daten auf dem PV-Forecast Adapter darzustellen. (https://forum.iobroker.net/topic/45315/test-pv-forecast-adapter/440?_=1719822087869)
Die Daten aus dem Awattar Adapter sehen so aus:
{ "object": "list", "data": [ { "start_timestamp": 1719698400000, "end_timestamp": 1719702000000, "marketprice": 59.94, "unit": "Eur/MWh" }, { "start_timestamp": 1719702000000, "end_timestamp": 1719705600000, "marketprice": 58.4, "unit": "Eur/MWh" }, { "start_timestamp": 1719705600000, "end_timestamp": 1719709200000, "marketprice": 61.27, "unit": "Eur/MWh" }, { "start_timestamp": 1719709200000, "end_timestamp": 1719712800000, "marketprice": 61.66, "unit": "Eur/MWh" }, { "start_timestamp": 1719712800000, "end_timestamp": 1719716400000, "marketprice": 59.58, "unit": "Eur/MWh" }, { "start_timestamp": 1719716400000, "end_timestamp": 1719720000000, "marketprice": 47.97, "unit": "Eur/MWh" }, { "start_timestamp": 1719720000000, "end_timestamp": 1719723600000, "marketprice": 43.05, "unit": "Eur/MWh" }, { "start_timestamp": 1719723600000, "end_timestamp": 1719727200000, "marketprice": 18.9, "unit": "Eur/MWh" }, { "start_timestamp": 1719727200000, "end_timestamp": 1719730800000, "marketprice": 6.83, "unit": "Eur/MWh" }, { "start_timestamp": 1719730800000, "end_timestamp": 1719734400000, "marketprice": 0.82, "unit": "Eur/MWh" }, { "start_timestamp": 1719734400000, "end_timestamp": 1719738000000, "marketprice": 0, "unit": "Eur/MWh" }, { "start_timestamp": 1719738000000, "end_timestamp": 1719741600000, "marketprice": 0, "unit": "Eur/MWh" }, { "start_timestamp": 1719741600000, "end_timestamp": 1719745200000, "marketprice": -0.02, "unit": "Eur/MWh" }, { "start_timestamp": 1719745200000, "end_timestamp": 1719748800000, "marketprice": -1.56, "unit": "Eur/MWh" }, { "start_timestamp": 1719748800000, "end_timestamp": 1719752400000, "marketprice": -1.13, "unit": "Eur/MWh" }, { "start_timestamp": 1719752400000, "end_timestamp": 1719756000000, "marketprice": 0, "unit": "Eur/MWh" }, { "start_timestamp": 1719756000000, "end_timestamp": 1719759600000, "marketprice": 0.94, "unit": "Eur/MWh" }, { "start_timestamp": 1719759600000, "end_timestamp": 1719763200000, "marketprice": 39.72, "unit": "Eur/MWh" }, { "start_timestamp": 1719763200000, "end_timestamp": 1719766800000, "marketprice": 63.23, "unit": "Eur/MWh" }, { "start_timestamp": 1719766800000, "end_timestamp": 1719770400000, "marketprice": 74.98, "unit": "Eur/MWh" }, { "start_timestamp": 1719770400000, "end_timestamp": 1719774000000, "marketprice": 80.77, "unit": "Eur/MWh" }, { "start_timestamp": 1719774000000, "end_timestamp": 1719777600000, "marketprice": 88.98, "unit": "Eur/MWh" }, { "start_timestamp": 1719777600000, "end_timestamp": 1719781200000, "marketprice": 97.47, "unit": "Eur/MWh" }, { "start_timestamp": 1719781200000, "end_timestamp": 1719784800000, "marketprice": 89.44, "unit": "Eur/MWh" }, { "start_timestamp": 1719784800000, "end_timestamp": 1719788400000, "marketprice": 91.94, "unit": "Eur/MWh" }, { "start_timestamp": 1719788400000, "end_timestamp": 1719792000000, "marketprice": 81.79, "unit": "Eur/MWh" }, { "start_timestamp": 1719792000000, "end_timestamp": 1719795600000, "marketprice": 74.6, "unit": "Eur/MWh" }, { "start_timestamp": 1719795600000, "end_timestamp": 1719799200000, "marketprice": 73.35, "unit": "Eur/MWh" }, { "start_timestamp": 1719799200000, "end_timestamp": 1719802800000, "marketprice": 70.93, "unit": "Eur/MWh" }, { "start_timestamp": 1719802800000, "end_timestamp": 1719806400000, "marketprice": 74.95, "unit": "Eur/MWh" }, { "start_timestamp": 1719806400000, "end_timestamp": 1719810000000, "marketprice": 85.91, "unit": "Eur/MWh" }, { "start_timestamp": 1719810000000, "end_timestamp": 1719813600000, "marketprice": 113.66, "unit": "Eur/MWh" }, { "start_timestamp": 1719813600000, "end_timestamp": 1719817200000, "marketprice": 123.86, "unit": "Eur/MWh" }, { "start_timestamp": 1719817200000, "end_timestamp": 1719820800000, "marketprice": 97.41, "unit": "Eur/MWh" }, { "start_timestamp": 1719820800000, "end_timestamp": 1719824400000, "marketprice": 95.95, "unit": "Eur/MWh" }, { "start_timestamp": 1719824400000, "end_timestamp": 1719828000000, "marketprice": 78.05, "unit": "Eur/MWh" }, { "start_timestamp": 1719828000000, "end_timestamp": 1719831600000, "marketprice": 74.31, "unit": "Eur/MWh" }, { "start_timestamp": 1719831600000, "end_timestamp": 1719835200000, "marketprice": 74.45, "unit": "Eur/MWh" }, { "start_timestamp": 1719835200000, "end_timestamp": 1719838800000, "marketprice": 70.94, "unit": "Eur/MWh" }, { "start_timestamp": 1719838800000, "end_timestamp": 1719842400000, "marketprice": 69.46, "unit": "Eur/MWh" }, { "start_timestamp": 1719842400000, "end_timestamp": 1719846000000, "marketprice": 70.98, "unit": "Eur/MWh" }, { "start_timestamp": 1719846000000, "end_timestamp": 1719849600000, "marketprice": 85.97, "unit": "Eur/MWh" }, { "start_timestamp": 1719849600000, "end_timestamp": 1719853200000, "marketprice": 87.95, "unit": "Eur/MWh" }, { "start_timestamp": 1719853200000, "end_timestamp": 1719856800000, "marketprice": 90.91, "unit": "Eur/MWh" }, { "start_timestamp": 1719856800000, "end_timestamp": 1719860400000, "marketprice": 90.03, "unit": "Eur/MWh" }, { "start_timestamp": 1719860400000, "end_timestamp": 1719864000000, "marketprice": 105.04, "unit": "Eur/MWh" }, { "start_timestamp": 1719864000000, "end_timestamp": 1719867600000, "marketprice": 108.26, "unit": "Eur/MWh" }, { "start_timestamp": 1719867600000, "end_timestamp": 1719871200000, "marketprice": 88.77, "unit": "Eur/MWh" } ], "url": "/at/v1/marketdata" }
Meine Versuche den Code von @copystring an das o.g. JSON anzupassen, schlugen leider alle fehl.
zBtype: custom:apexcharts-card graph_span: 2d span: start: day series: - entity: sensor.awattar_full type: column data_generator: | var data = []; var formattedJSON = JSON.parse(entity.state); console.log(formattedJSON); formattedJSON.forEach(obj => { data.push([obj.start_timestamp, obj.marketprice]) }); return data;
In der js console kommt dann immer der Fehler 'Error: formattedJSON.forEach is not a function'
Ich bin mir fast sicher das ich nur eine Kleinigkeit falsch mache.
Kann mir jemand weiterhelfen?Schöne Grüße!
-
@fhainz sagte: Die Daten aus dem Awattar Adapter sehen so aus:
Die Schleife muss über ein Array laufen. Ändere mal Zeile 10 in
var formattedJSON = JSON.parse(entity.state).data;
-
@paul53
Super, vielen Dank! Jetzt funktioniert es natürlich.type: custom:apexcharts-card experimental: color_threshold: true graph_span: 2day update_interval: 2m now: show: true label: Jetzt color: gray span: offset: +0day start: day yaxis: - id: primary decimals: 0 min: 0 header: show: true title: Strom Preis Verlauf - Heute & Morgen show_states: true colorize_states: true all_series_config: color_threshold: - value: -100 color: green - value: 5 color: orange - value: 10 color: red unit: ct/kWh type: line stroke_width: 0 show: legend_value: false in_header: true header_color_threshold: true series: - entity: sensor.awattar_full type: column name: Aktuell show: extremas: true in_header: before_now data_generator: | var data = []; var formattedJSON = JSON.parse(entity.state).data; console.log(formattedJSON); formattedJSON.forEach(obj => { data.push([obj.start_timestamp, (obj.marketprice/10*1.2)]) }); return data;