Seite 1 von 1
JSON auslesen und darstellen
Verfasst: So 6. Feb 2022, 20:32
von kurt boehm
Ich habe eine PV-Anlage mit 2 WRs von SMA, die per BT die Werte zu einer Sunny-Webbox senden.
Jetzt gibt es mehrere Möglichkeiten außerhalb von HA, diese auswerten bzw. anzeigen zu lassen. Das fängt bei der Webbox selbst an, geht über das Sunnyportal und hört bei mir im Moment bei einem eigenen Webserver, wo die Daten über ein php-Script aufbereitet werden, auf.
Jetzt möchte ich gern die Daten über eine einfache Abfrage von der Webbox in HA darstellen lassen. Allerdings bekomme ich in der Antwort mehrere gleiche Datenfelder. Wie kann ich diese teilen, um sie auch richtig zuordnen zu können?
Die Antwort von der Webbox sieht folgendermaßen aus (der Code steht eigentlich in 1 Zeile):
Code: Alles auswählen
{"result":
{"overview":
[
{
"meta":"GriPwr",
"name":"Leistung",
"unit":"W",
"value":"0"
},
{
"meta":"GriEgyTdy",
"name":"Tagesertrag",
"unit":"kWh",
"value":"1.324"
},
{
"meta":"GriEgyTot",
"name":"Gesamtertrag",
"unit":"kWh",
"value":"69282.927"
},
{
"meta":"OpStt",
"name":"Zustand",
"value":"Ok, Ok"
},
{
"meta":"Msg",
"name":"Meldung",
"value":""
}
]
},
"format":"JSON","proc":"GetPlantOverview","version":"1.0","id":"1"
}
Re: JSON auslesen und darstellen
Verfasst: Mo 7. Feb 2022, 22:32
von kurt boehm
Ich habe mal ein wenig mit dem Template-Editor in HA rumgespielt.
Und so bekam ich einige neue Erkenntnisse.
Code: Alles auswählen
{## Imitate available variables: ##}
{% set value_json = {"result":{"overview":[{"meta":"GriPwr","name":"Leistung","unit":"W","value":"0"},{"meta":"GriEgyTdy","name":"Tagesertrag","unit":"kWh","value":"17.021"},{"meta":"GriEgyTot","name":"Gesamtertrag","unit":"kWh","value":"69299.948"},{"meta":"OpStt","name":"Zustand","value":"Ok, Ok"},{"meta":"Msg","name":"Meldung","value":""}]},"format":"JSON","proc":"GetPlantOverview","version":"1.0","id":"1"} %}
{{ value_json.result.overview.0.name }}: {{ value_json.result.overview.0.value }} {{ value_json.result.overview.0.unit }}
{{ value_json.result.overview.1.name }}: {{ value_json.result.overview.1.value }} {{ value_json.result.overview.1.unit }}
{{ value_json.result.overview.2.name }}: {{ value_json.result.overview.2.value }} {{ value_json.result.overview.2.unit }}
{{ value_json.result.overview.3.name }}: {{ value_json.result.overview.3.value }}
{{ value_json.result.overview.4.name }}: {{ value_json.result.overview.4.value }}
...erzeugt folgendes:
Code: Alles auswählen
Ergebnistyp: string
Leistung: 0 W
Tagesertrag: 17.021 kWh
Gesamtertrag: 69299.948 kWh
Zustand: Ok, Ok
Meldung:

mühsam ernährt sich das Eichhörnchen.
Re: JSON auslesen und darstellen
Verfasst: Di 8. Feb 2022, 18:44
von PX80
Danke fürs teilen. Sollte ich mal eine Ausgabe in JSON habe, werde ich mich an deinen Post erinnern

Re: JSON auslesen und darstellen
Verfasst: Di 8. Feb 2022, 20:34
von kurt boehm
PX80 hat geschrieben: ↑Di 8. Feb 2022, 18:44
Danke fürs teilen.
Das gehört doch in einem Forum dazu. Genauso wie im Handwerk mit den Lehrlingen. Wissen oder Erfahrungen kann man ruhig teilen. Das tut dem einen nicht weh, bei dem anderen kommt es nur darauf an, was er daraus macht.

Re: JSON auslesen und darstellen
Verfasst: So 13. Feb 2022, 18:57
von kurt boehm
Nachdem ich jetzt an 3 Abenden keinen Erfolgsschimmer verzeichnen konnte, habe ich jetzt am 4. Tag eine Lösung gefunden, alle "relevanten" Werte meiner PV-Anlage darstellen zu können. Anscheinend hat es wohl nur an einem aussagekräftigen Beispiel gelegen. Und dann ging alles ganz fix. Ich mache ja auch alles per Trial & Error.
Geholfen hat mir dabei ein
JSON-Parser.
Das Gerüst steht nun. Feinheiten wären noch die Icons anzupassen und die Sensoren, die statische Werte zurückgeben.
Code: Alles auswählen
sensor:
# SMA Webbox Status
- platform: rest
name: sma_webbox_status
scan_interval: 900
resource: http://192.168.30.10/rpc?RPC={%22version%22:%221.0%22,%22proc%22:%22GetPlantOverview%22,%22id%22:%221%22,%22format%22:%22JSON%22}
value_template: '{{ "Sunny Webbox " + value_json["proc"].title() }}'
json_attributes_path: "$.result"
json_attributes:
- overview
- platform: template
sensors:
sma_webbox_momentanleistung:
value_template: '{{ states.sensor.sma_webbox_status.attributes["overview"][0]["value"] }}'
friendly_name: "Momentanleistung"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: W
unique_id: "sma-webbox-momentanleistung"
sma_webbox_tagesertrag:
value_template: '{{ states.sensor.sma_webbox_status.attributes["overview"][1]["value"] }}'
friendly_name: "Tagesertrag"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: kWh
unique_id: "sma-webbox-tagesertrag"
sma_webbox_gesamtertrag:
value_template: '{{ states.sensor.sma_webbox_status.attributes["overview"][2]["value"] }}'
friendly_name: "Gesamtertrag"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: kWh
unique_id: "sma-webbox-gesamtertrag"
sma_webbox_zustand:
value_template: '{{ states.sensor.sma_webbox_status.attributes["overview"][3]["value"] }}'
friendly_name: "Zustand"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-zustand"
sma_webbox_meldung:
value_template: '{{ states.sensor.sma_webbox_status.attributes["overview"][4]["value"] }}'
friendly_name: "Meldung"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-meldung"
# SMA Webbox Devices
- platform: rest
name: sma_webbox_devices
resource: http://192.168.30.10/rpc?RPC={%22version%22:%20%221.0%22,%22proc%22:%20%22GetDevices%22,%22id%22:%20%221%22,%22format%22:%20%22JSON%22}
value_template: '{{ "Sunny Webbox " + value_json["proc"].title() }}'
json_attributes_path: "$.result"
json_attributes:
- devices
- platform: template
sensors:
sma_webbox_key1:
value_template: '{{ states.sensor.sma_webbox_devices.attributes["devices"][0]["key"] }}'
friendly_name: "SB4000TL-20 Key"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-key-wr1"
sma_webbox_serial1:
value_template: '{{ states.sensor.sma_webbox_devices.attributes["devices"][0]["name"] }}'
friendly_name: "SB4000TL-20 Serial"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-serial-wr1"
sma_webbox_key2:
value_template: '{{ states.sensor.sma_webbox_devices.attributes["devices"][1]["key"] }}'
friendly_name: "SB2500HF-30 Key"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-key-wr2"
sma_webbox_serial2:
value_template: '{{ states.sensor.sma_webbox_devices.attributes["devices"][1]["name"] }}'
friendly_name: "SB2500HF-30 Serial"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-webbox-serial-wr2"
# SMA SB4000TL-20 Status
- platform: rest
name: sma_wr1_status
scan_interval: 900
resource: http://192.168.30.10/rpc?RPC={%22version%22:%221.0%22,%22proc%22:%22GetProcessData%22,%22id%22:%221%22,%22format%22:%22JSON%22,%22params%22:{%22devices%22:[{%22key%22:%22!secret_key!%22,%22channels%22:null}]}}}
value_template: '{{ "SB4000TL-20 " + value_json["proc"].title() }}'
json_attributes_path: "$.result.devices[0]"
json_attributes:
- channels
- platform: template
sensors:
sma_wr1_dc_strom_eingang:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][0]["value"] }}'
friendly_name: "DC Strom Eingang"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: A
unique_id: "sma-wr1-dc-strom-eingang"
sma_wr1_dc_spannung_eingang:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][1]["value"] }}'
friendly_name: "DC Spannung Eingang"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: V
unique_id: "sma-wr1-dc-spannung-eingang"
sma_wr1_netzfrequenz:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][5]["value"] }}'
friendly_name: "Netzfrequenz"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: Hz
unique_id: "sma-wr1-netzfrequenz"
sma_wr1_netzstrom:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][6]["value"] }}'
friendly_name: "Netzstrom"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: A
unique_id: "sma-wr1-netzstrom"
sma_wr1_momentanleistung:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][7]["value"] }}'
friendly_name: "Momentanleistung"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: W
unique_id: "sma-wr1-momentanleistung"
sma_wr1_isolationswiderstand:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][8]["value"] }}'
friendly_name: "Isolationswiderstand"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: Ohm
unique_id: "sma-wr1-isolationswiderstand"
sma_wr1_einspeisezeit:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][10]["value"] }}'
friendly_name: "Einspeisezeit"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: h
unique_id: "sma-wr1-einspeisezeit"
sma_wr1_betriebszeit:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][11]["value"] }}'
friendly_name: "Betriebszeit"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: h
unique_id: "sma-wr1-betriebszeit"
sma_wr1_gesamtertrag:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][12]["value"] }}'
friendly_name: "Gesamtertrag"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: kWh
unique_id: "sma-wr1-gesamtertrag"
sma_wr1_anzahl_netzzuschaltungen:
value_template: '{{ states.sensor.sma_wr1_status.attributes["channels"][13]["value"] }}'
friendly_name: "Anzahl Netzzuschaltungen"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-wr1-anzahl-netzzuschaltungen"
# SMA SB 2500HF-30 Status
- platform: rest
name: sma_wr2_status
scan_interval: 900
resource: http://192.168.30.10/rpc?RPC={%22version%22:%221.0%22,%22proc%22:%22GetProcessData%22,%22id%22:%221%22,%22format%22:%22JSON%22,%22params%22:{%22devices%22:[{%22key%22:%22!secret_key!%22,%22channels%22:null}]}}}
value_template: '{{ "SB 2500HF-30 " + value_json["proc"].title() }}'
json_attributes_path: "$.result.devices[0]"
json_attributes:
- channels
- platform: template
sensors:
sma_wr2_dc_strom_eingang:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][0]["value"] }}'
friendly_name: "DC Strom Eingang"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: A
unique_id: "sma-wr2-dc-strom-eingang"
sma_wr2_dc_spannung_eingang:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][1]["value"] }}'
friendly_name: "DC Spannung Eingang"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: V
unique_id: "sma-wr2-dc-spannung-eingang"
sma_wr2_netzfrequenz:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][5]["value"] }}'
friendly_name: "Netzfrequenz"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: Hz
unique_id: "sma-wr2-netzfrequenz"
sma_wr2_netzstrom:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][6]["value"] }}'
friendly_name: "Netzstrom"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: A
unique_id: "sma-wr2-netzstrom"
sma_wr2_momentanleistung:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][7]["value"] }}'
friendly_name: "Momentanleistung"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: W
unique_id: "sma-wr2-momentanleistung"
sma_wr2_isolationswiderstand:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][8]["value"] }}'
friendly_name: "Isolationswiderstand"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: Ohm
unique_id: "sma-wr2-isolationswiderstand"
sma_wr2_einspeisezeit:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][10]["value"] }}'
friendly_name: "Einspeisezeit"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: h
unique_id: "sma-wr2-einspeisezeit"
sma_wr2_betriebszeit:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][11]["value"] }}'
friendly_name: "Betriebszeit"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: h
unique_id: "sma-wr2-betriebszeit"
sma_wr2_gesamtertrag:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][12]["value"] }}'
friendly_name: "Gesamtertrag"
icon_template: mdi:lightning-bolt-outline
unit_of_measurement: kWh
unique_id: "sma-wr2-gesamtertrag"
sma_wr2_anzahl_netzzuschaltungen:
value_template: '{{ states.sensor.sma_wr2_status.attributes["channels"][13]["value"] }}'
friendly_name: "Anzahl Netzzuschaltungen"
icon_template: mdi:lightning-bolt-outline
unique_id: "sma-wr2-anzahl-netzzuschaltungen"
Morgen mache ich dann mal ein Bild vom UI. Jetzt ist das wenig aussagekräftig. Ist ja dunkel draußen.
Re: JSON auslesen und darstellen
Verfasst: Mo 14. Feb 2022, 15:16
von kurt boehm
.

- sma.jpg (145.37 KiB) 636 mal betrachtet