[geloest] expandJSON id Name als reading name

Begonnen von ch.eick, 01 April 2020, 13:14:26

Vorheriges Thema - Nächstes Thema

ch.eick

Hallo zusammen,

ich habe da mal ein Problem gemacht :-)

Mit expandJSON wandele ich meine Daten um und bekomme jeweils readings nach dem Muster

statistics_01_id Autark_Day
statistics_01_value 88.6721992368
statistics_02_id Autark_Month
statistics_02_value 88.6721992368


gibt es eine Moeglichkeit das wie folgt umzusetzen?

Autark_Day 88.6721992368
Autark_Month 88.6721992368


Der bisherige Weg meiner Daten ist wie folgt, damit Ihr meine jetzigen Moeglichkeiten seht.

1. Es gibt ein Pyhton Skript, das meine PV Anlage abfragt, um an die Statistiken zu kommen.

2. Die Daten erscheinen dann in einem Reading vom PV_Anlage_1
statistics [{"moduleid": "scb:statistic:EnergyFlow", "processdata": [{"id": "Statistic:Autarky:Day", "unit": "", "value": 88.6721992368}, {"id": "Statistic:Autarky:Month", "unit": "", "value": 88.6721992368}, {"id": "Statistic:Autarky:Total", "unit": "", "value": 43.110795956}, {"id": "Statistic:Autarky:Year", "unit": "", "value": 52.7201666638}, {"id": "Statistic:CO2Saving:Day", "unit": "", "value": 9301.3194345641}, {"id": "Statistic:CO2Saving:Month", "unit": "", "value": 9301.3194345641}, {"id": "Statistic:CO2Saving:Total", "unit": "", "value": 1307955.4783748263}, {"id": "Statistic:CO2Saving:Year", "unit": "", "value": 993802.3969573769}, {"id": "Statistic:EnergyHome:Day", "unit": "", "value": 13171.0439189734}, {"id": "Statistic:EnergyHome:Month", "unit": "", "value": 13171.0439189734}, {"id": "Statistic:EnergyHome:Total", "unit": "", "value": 3575733.43844843}, {"id": "Statistic:EnergyHome:Year", "unit": "", "value": 2122128.376580306}, {"id": "Statistic:EnergyHomeBat:Day", "unit": "", "value": 3598.2462178644}, {"id": "Statistic:EnergyHomeBat:Month", "unit": "", "value": 3598.2462178644}, {"id": "Statistic:EnergyHomeBat:Total", "unit": "", "value": 573229.8482352358}, {"id": "Statistic:EnergyHomeBat:Year", "unit": "", "value": 427291.9789922817}, {"id": "Statistic:EnergyHomeGrid:Day", "unit": "", "value": 1491.30016978}, {"id": "Statistic:EnergyHomeGrid:Month", "unit": "", "value": 1491.30016978}, {"id": "Statistic:EnergyHomeGrid:Total", "unit": "", "value": 2048840.7171583278}, {"id": "Statistic:EnergyHomeGrid:Year", "unit": "", "value": 1005481.3990161464}, {"id": "Statistic:EnergyHomePv:Day", "unit": "", "value": 8080.7056528676}, {"id": "Statistic:EnergyHomePv:Month", "unit": "", "value": 8080.7056528676}, {"id": "Statistic:EnergyHomePv:Total", "unit": "", "value": 968288.0318021489}, {"id": "Statistic:EnergyHomePv:Year", "unit": "", "value": 691494.2946195344}, {"id": "Statistic:OwnConsumptionRate:Day", "unit": "", "value": 87.8943903743}, {"id": "Statistic:OwnConsumptionRate:Month", "unit": "", "value": 87.8943903743}, {"id": "Statistic:OwnConsumptionRate:Total", "unit": "", "value": 82.5004383135}, {"id": "Statistic:OwnConsumptionRate:Year", "unit": "", "value": 78.8036670333}, {"id": "Statistic:Yield:Day", "unit": "", "value": 13287.5991922345}, {"id": "Statistic:Yield:Month", "unit": "", "value": 13287.5991922345}, {"id": "Statistic:Yield:Total", "unit": "", "value": 1868507.826249752}, {"id": "Statistic:Yield:Year", "unit": "", "value": 1419717.7099391099}]}]

3. Mit einem userreading formatiere ich das dann etwas um.
statistics_clean:statistics.* { my $x =  ReadingsVal($NAME,"statistics",0);; $x =~ s/Statistic:|, "unit": ""|^\[|\]$//g;; $x =~ s/[a-z]:/_/g;; $x =~ s/processdata/statistics/g ;; return $x }

4. Nun sehen die Daten so aus.
statistics_clean {"moduleid": "sc_statisti_EnergyFlow", "statistics": [{"id": "Autark_Day", "value": 88.6721992368}, {"id": "Autark_Month", "value": 88.6721992368}, {"id": "Autark_Total", "value": 43.110795956}, {"id": "Autark_Year", "value": 52.7201666638}, {"id": "CO2Savin_Day", "value": 9301.3194345641}, {"id": "CO2Savin_Month", "value": 9301.3194345641}, {"id": "CO2Savin_Total", "value": 1307955.4783748263}, {"id": "CO2Savin_Year", "value": 993802.3969573769}, {"id": "EnergyHom_Day", "value": 13171.0439189734}, {"id": "EnergyHom_Month", "value": 13171.0439189734}, {"id": "EnergyHom_Total", "value": 3575733.43844843}, {"id": "EnergyHom_Year", "value": 2122128.376580306}, {"id": "EnergyHomeBa_Day", "value": 3598.2462178644}, {"id": "EnergyHomeBa_Month", "value": 3598.2462178644}, {"id": "EnergyHomeBa_Total", "value": 573229.8482352358}, {"id": "EnergyHomeBa_Year", "value": 427291.9789922817}, {"id": "EnergyHomeGri_Day", "value": 1491.30016978}, {"id": "EnergyHomeGri_Month", "value": 1491.30016978}, {"id": "EnergyHomeGri_Total", "value": 2048840.7171583278}, {"id": "EnergyHomeGri_Year", "value": 1005481.3990161464}, {"id": "EnergyHomeP_Day", "value": 8080.7056528676}, {"id": "EnergyHomeP_Month", "value": 8080.7056528676}, {"id": "EnergyHomeP_Total", "value": 968288.0318021489}, {"id": "EnergyHomeP_Year", "value": 691494.2946195344}, {"id": "OwnConsumptionRat_Day", "value": 87.8943903743}, {"id": "OwnConsumptionRat_Month", "value": 87.8943903743}, {"id": "OwnConsumptionRat_Total", "value": 82.5004383135}, {"id": "OwnConsumptionRat_Year", "value": 78.8036670333}, {"id": "Yiel_Day", "value": 13287.5991922345}, {"id": "Yiel_Month", "value": 13287.5991922345}, {"id": "Yiel_Total", "value": 1868507.826249752}, {"id": "Yiel_Year", "value": 1419717.7099391099}]}

5. Mit einem expandJSON Device wird dann "statistics_clean" in die bisherigen readings umgewandelt.
statistics_01_id Autark_Day
statistics_01_value 88.6721992368
statistics_02_id Autark_Month
statistics_02_value 88.6721992368

6. Ab dieser Stelle komme ich momentan nicht weiter. Es waere halt schoen, wenn es wie folgt aussieht
Autark_Day 88.6721992368
Autark_Month 88.6721992368

Dann waeren es nur halb so viele readings, die auch besser nach Namen sortiert waeren.


Gruss
     Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

Und schon ein Update :-)

Ich habe es nun etwas brutal, durch die Manipulation des JSON , bereits soweit gebracht.

statistics_clean:Total_DC_Power.* { my $x =  ReadingsVal($NAME,"statistics",0);; $x =~ s/id": "Statistic:|, "unit": "", "value"|^\[|\]$//g;; $x =~ s/moduleid/statistics_00_moduleid/g;; $x =~ s/processdata/statistics/g;; return $x }



statistics_00_moduleid scb:statistic:EnergyFlow
statistics_01_Autarky_Day 92.2207885948
statistics_02_Autarky_Month 92.2207885948
statistics_03_Autarky_Total 43.2063604331
statistics_04_Autarky_Year 52.8538312948
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

#2
Und nun die naechste Folge in meinem Sebstgespraech ;-)


statistics_clean:Total_DC_Power.* { my $x =  ReadingsVal($NAME,"statistics",0);; $x =~ s/\{"moduleid": "scb:statistic:EnergyFlow", "processdata": \[|id": "|, "unit": "", "value"|^\[|\]\}\]$//g;; $x =~ s/\}\, \{/\, /g;; return $x }

Danach sieht die JSON Struktur nun wie folgt aus

{"Statistic:Autarky:Day": 92.2207885948, "Statistic:Autarky:Month": 92.2207885948, "Statistic:Autarky:Total": 43.2063604331, "Statistic:Autarky:Year": 52.8538312948, "Statistic:CO2Saving:Day": 17931.5485221883, "Statistic:CO2Saving:Month": 17931.5485221883, "Statistic:CO2Saving:Total": 1316585.7074624505, "Statistic:CO2Saving:Year": 1002432.6260450011, "Statistic:EnergyHome:Day": 19189.1590185024, "Statistic:EnergyHome:Month": 19189.1590185024, "Statistic:EnergyHome:Total": 3581751.55354796, "Statistic:EnergyHome:Year": 2128146.491679835, "Statistic:EnergyHomeBat:Day": 3600.9952051276, "Statistic:EnergyHomeBat:Month": 3600.9952051276, "Statistic:EnergyHomeBat:Total": 573232.597222499, "Statistic:EnergyHomeBat:Year": 427294.7279795448, "Statistic:EnergyHomeGrid:Day": 1492.3168785892, "Statistic:EnergyHomeGrid:Month": 1492.3168785892, "Statistic:EnergyHomeGrid:Total": 2048841.7338671372, "Statistic:EnergyHomeGrid:Year": 1005482.4157249557, "Statistic:EnergyHomePv:Day": 14095.7631406107, "Statistic:EnergyHomePv:Month": 14095.7631406107, "Statistic:EnergyHomePv:Total": 974303.0892898919, "Statistic:EnergyHomePv:Year": 697509.3521072774, "Statistic:OwnConsumptionRate:Day": 69.0820183476, "Statistic:OwnConsumptionRate:Month": 69.0820183476, "Statistic:OwnConsumptionRate:Total": 82.2795761864, "Statistic:OwnConsumptionRate:Year": 78.5454153263, "Statistic:Yield:Day": 25616.4978888405, "Statistic:Yield:Month": 25616.4978888405, "Statistic:Yield:Total": 1880836.724946358, "Statistic:Yield:Year": 1432046.608635716}


Das bringt nun folgendes Ergebniss

Statistic_Autarky_Day 92.2207885948
Statistic_Autarky_Month 92.2207885948
Statistic_Autarky_Total 43.2063604331
Statistic_Autarky_Year 52.8538312948


Hierbei ist nun dringend zu beachten, dass durch das userreading die komplette Struktur des JSON Output manipuliert wird und somit eine neue JSON Struktur entsteht.

Viele Gruesse
      Christian
RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick

ch.eick

RPI4; Docker; CUNX; Eltako FSB61NP; SamsungTV H-Serie; Sonos; Vallox; Luxtronik; 3x FB7490; Stromzähler mit DvLIR; wunderground; Plenticore 10 mit BYD; EM410; SMAEM; Modbus TCP
Contrib: https://svn.fhem.de/trac/browser/trunk/fhem/contrib/ch.eick