How to scrape data from an xml?

Hi,

First of all my apologies, to hijack this topic.
This is my first post over here and I’m struggling with yaml.
My goal is to get input from a steca.
I have made an stecagrid.yaml (also included it in configurations.yaml) and copied the solution of MichaelNeys.
Developer tools showed me a configuration warning:

  • Integration error: stecagrid - Integration ‘stecagrid’ not found.

All right,
lets copy it into sensors.yaml.
Then developer tools showed me:

  • Invalid config for ‘sensor’ at sensors.yaml, line 1: required key ‘platform’ not provided.

Could anyone be so kind to tell me what I’m doing wrong?
Many thanks.

I’m sure that a solution will help me to create much more sensors😁

Probably good to tell, is the fact I’m running
Home Assistant Core Update 2024.2.2

Hi,

Are you able to see the data of the inverter when visiting its IP with /measurements.xml behind it? (eg. http://10.0.0.13/measurements.xml)

In my config file I have rest: !include rest.yaml and this is inside my rest.yaml file:

- resource: http://10.0.0.13/measurements.xml
  scan_interval: 2
  sensor:
    - name: "Solar AC Voltage"
      unique_id: solar_ac_voltage
      unit_of_measurement: "V"
      json_attributes_path: $.root.Device.Measurements.Measurement[0]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[0]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar AC Current"
      unique_id: solar_ac_current
      unit_of_measurement: "A"
      json_attributes_path: $.root.Device.Measurements.Measurement[1]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[1]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar AC Power"
      unique_id: solar_ac_power
      unit_of_measurement: "W"
      icon: 'mdi:solar-power'
      json_attributes_path: $.root.Device.Measurements.Measurement[2]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[2]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar AC Power fast"
      unique_id: solar_ac_power_fast
      unit_of_measurement: "W"
      icon: 'mdi:solar-power'
      json_attributes_path: $.root.Device.Measurements.Measurement[3]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[3]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar AC Frequency"
      unique_id: solar_ac_frequency
      unit_of_measurement: "Hz"
      json_attributes_path: $.root.Device.Measurements.Measurement[4]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[4]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar DC Voltage1"
      unique_id: solar_dc_voltage1
      unit_of_measurement: "V"
      json_attributes_path: $.root.Device.Measurements.Measurement[5]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[5]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar DC Voltage2"
      unique_id: solar_dc_voltage2
      unit_of_measurement: "V"
      json_attributes_path: $.root.Device.Measurements.Measurement[6]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[6]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar DC Current1"
      unique_id: solar_dc_current1
      unit_of_measurement: "A"
      json_attributes_path: $.root.Device.Measurements.Measurement[7]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[7]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Solar DC Current2"
      unique_id: solar_dc_current2
      unit_of_measurement: "A"
      json_attributes_path: $.root.Device.Measurements.Measurement[8]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[8]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "LINK Voltage"
      unique_id: solar_link_voltage
      unit_of_measurement: "V"
      json_attributes_path: $.root.Device.Measurements.Measurement[9]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[9]['@Value'] }}"
      device_class: energy
      state_class: measurement
    - name: "Steca Omvormer Derating"
      unique_id: omvormer_derating
      unit_of_measurement: "%"
      icon: 'mdi:solar-panel'
      json_attributes_path: $.root.Device.Measurements.Measurement[14]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[14]['@Value'] }}"
      device_class: energy
      state_class: measurement

It’s possible the json_attrivutes_path will be different for you if you have a different model. You can see my XML in my first post on this thread.

My scan_interval is very frequently but you can edit this if you want.
I was struggling with this a very long time too because when I begun with Home-Assistant I didn’t work with rest and hadn’t much coding experience. Well next year I’ll be studying Informatics at uni just because I one day decided to start with this simple things lol.

Make some changes to the rest.yaml so it fits for your sensors. Like to hear when it doesn’t work for you!

1 Like

Do you have any suggestions on how I could make this value into a Wh for the production in HA because now It’s just W and the values are wrong with what I use… Plus the energy dashboard also needed a total increasing or am I wrong?

use the reiman sum integration to turn it into Wh or kWh

Saw that somewhere else to but it’s still weird, should I maybe use left or right?

use whatever works best for you

1 Like

Hi,

Back again. I have now a sensor which will give me the output of my device. Unfortunatly the sensor has to be reloaded every morning, since the output is 0 during the night. To automate such a thing I have created a sensor (ESPHome) with a photoresistor which will trigger the reloading when the lux output reaches a chosen value for over 5 minutes.
So far so good.
To see the output of the day, I create a helper with a Riemann sum, but thats not very reliable. The xml output will also not return the total output of a day.
I have now done some research and have seen that the total output of the day can be seen in the following link
http://steca.ip/gen.yield.day.chart.js (there is also a month, a year and a total :wink: )

Is it possible to scrape the right info to create some sensors or charts or something like that?

My coding skills are minimal, so I don’t have a clue…

The link you provided did not work for me. I just use the data from the XML and register it every 2 seconds, it wil not be exactly right but 2 seconds is already pretty pricise and create a Riemannsum with a helper.

It’s a very clever solution with the lux-sensor although I don’t understand why it wouldn’t work after the value was 0. I personally don’t know how HA handles it but I think it maybe could have something to do with device class of the sensor?

Hi Michael

Been very busy so excuse me for the late reply. I don’t know either why my sensor isn’t starting after 0 output, but that work around works :slight_smile:
Perhaps we have different inverters i’ve got a StecaGrid 5503
http://10.0.0.xxx/gen.yield.day.chart.js is working for me and creates:

var chartData =
{
"labels":
[
"00:00", "", "", "", "", "",
"01:00", "", "", "", "", "",
"02:00", "", "", "", "", "",
"03:00", "", "", "", "", "",
"04:00", "", "", "", "", "",
"05:00", "", "", "", "", "",
"06:00", "", "", "", "", "",
"07:00", "", "", "", "", "",
"08:00", "", "", "", "", "",
"09:00", "", "", "", "", "",
"10:00", "", "", "", "", "",
"11:00", "", "", "", "", "",
"12:00", "", "", "", "", "",
"13:00", "", "", "", "", "",
"14:00", "", "", "", "", "",
"15:00", "", "", "", "", "",
"16:00", "", "", "", "", "",
"17:00", "", "", "", "", "",
"18:00", "", "", "", "", "",
"19:00", "", "", "", "", "",
"20:00", "", "", "", "", "",
"21:00", "", "", "", "", "",
"22:00", "", "", "", "", "",
"23:00", "", "", "", "", ""
],
"datasets":
[
{
"strokeColor": style_strokecolor.color,
"data": [
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,12,
36,30,42,66,60,66,102,186,162,210,258,282,384,558,534,
408,384,306,600,618,750,1086,1530,1500,1716,1788,1428,2298,2478,1008,
732,456,444,444,438,468,576,900,732,630,582,732,930,726,684,
876,1170,1218,1134,624,426,378,462,606,480,396,330,312,300,354,
288,252,366,300,222,216,174,222,186,132,102,42,18,6,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0]
}
]
}
var max = 5750;
var steps = 23;
var input = document.getElementById("inputId");
input.setAttribute("min",   "2024-03-12");
input.setAttribute("max",   "2024-04-11");
input.setAttribute("value", "2024-04-11");
document.getElementById("labelValueId").innerHTML = "   7.030kWh 11.04.2024";
document.getElementById("buttonPrevId").disabled  = false;
document.getElementById("buttonNextId").disabled  = true;
var myLine = new Chart(document.getElementById("canvasId")
.getContext("2d"))
.Line(chartData,
{
bla bla

so scraping blabla.inner HTML would be nice…

What do you want to scrape in here? Is it to replace your workaround?
This is all that I have to scrape my wattage out of the XML, I did not need any more info from my inverter:

    - name: "Solar AC Power"
      unique_id: solar_ac_power
      unit_of_measurement: "W"
      icon: 'mdi:solar-power'
      json_attributes_path: $.root.Device.Measurements.Measurement[2]
      json_attributes:
        - Value
      value_template: "{{ value_json.root.Device.Measurements.Measurement[2]['@Value'] }}"
      device_class: energy
      state_class: measurement

If there is no data provided at night by my inverter the sensor in Home-Assistant just shows 0, I did not have to configure anything for that. The ‘0’ is also 1 measurement so not every 2 seconds, when the inverter starts when there is sun again the scrapper automatically begins. So sorry, I don’t have a clue for that.

I have a different inverter btw, the StecaGrid 3611.

I have spent a lot of time on Google and with ChatGPT/Gemini and unfortunately have not been able to find a solution to my problem.

I found this forum and @petro posts and hope he can help me too. What do I want? I have an XML file with my children’s school schedule. I want to extract certain data and output it as attributes or sensors (depending on what works).

The XML looks like this:

<WplanVp>
<Kopf>
<planart>K</planart>
<zeitstempel>10.01.2025, 06:39</zeitstempel>
<DatumPlan>Dienstag, 14. Januar 2025</DatumPlan>
<datei>WPlanKl_date.xml</datei>
</Kopf>
<FreieTage>
...
</FreieTage>
<Klassen>
<Kl>
<Kurz>9o</Kurz>
<Kurse/>
<Unterricht>
<Ue>
<UeNr UeLe="Rog" UeFa="And">179</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Bau" UeFa="BIO">180</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Vol" UeFa="CH">181</UeNr>
</Ue>
<Ue>
<UeNr UeLe="SeiJe" UeFa="DE">182</UeNr>
</Ue>
<Ue>
<UeNr UeLe="HeiT" UeFa="EN">183</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Pic" UeFa="GE">184</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Ded" UeFa="GEO">185</UeNr>
</Ue>
<Ue>
<UeNr UeLe="EDa" UeFa="GK">186</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Götz" UeFa="INF">187</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Rog" UeFa="KU">188</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Löf" UeFa="MA">189</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Frö" UeFa="MU">190</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Lüd" UeFa="PH">191</UeNr>
</Ue>
<Ue>
<UeNr UeLe="KunFr" UeFa="RE/e">192</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Hein" UeFa="SPA">193</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Frö" UeFa="SPO">194</UeNr>
</Ue>
<Ue>
<UeNr UeLe="Plö" UeFa="WTH">195</UeNr>
</Ue>
<Ue>
<UeNr UeLe="HärI" UeFa="WTH">362</UeNr>
</Ue>
</Unterricht>
**<Pl>**
**<Std>**
**<St>1</St>**
**<Fa>MA</Fa>**
**<Le>Löf</Le>**
**<Ra>B.109</Ra>**
**<Nr>189</Nr>**
**<If/>**
**</Std>**
**<Std>**
**<St>2</St>**
**<Fa>MA</Fa>**
**<Le>Löf</Le>**
**<Ra>B.109</Ra>**
**<Nr>189</Nr>**
**<If/>**
**</Std>**
**<Std>**
**<St>3</St>**
**<Fa>BIO</Fa>**
**<Le>Bau</Le>**
**<Ra>B.110</Ra>**
**<Nr>180</Nr>**
**<If/>**
**</Std>**
**<Std>**
**<St>4</St>**
**<Fa>BIO</Fa>**
**<Le>Bau</Le>**
**<Ra>B.110</Ra>**
**<Nr>180</Nr>**
**<If/>**
**</Std>**
**<Std>**
**<St>5</St>**
**<Fa>GE</Fa>**
**<Le>Pic</Le>**
**<Ra>B.209</Ra>**
**<Nr>184</Nr>**
**<If/>**
**</Std>**
**<Std>**
**<St>6</St>**
**<Fa>GE</Fa>**
**<Le>Pic</Le>**
**<Ra>B.209</Ra>**
**<Nr>184</Nr>**
**<If/>**
**</Std>**
**</Pl>**
</Kl>
</Klassen>
</WplanVp>

The bold one is what I need. If I use JSON PATH

$.Klassen[12].Pl[0]

I got

[
  {
    "St": "1",
    "Fa": "MA",
    "Le": "Löf",
    "Ra": "B.109",
    "Nr": "189",
    "If": []
  }
]

So far so good. But how do I get this into Home Assistant? Unfortunately, my knowledge is not sufficient. And I hope you can help me. This is my current yaml config:

rest:
  - authentication: basic
    username: !secret rest_username
    password: !secret rest_password
    scan_interval: 3600
    resource: "https://www.stundenplan24.de/schoolnumber//wplan/wdatenk/WPlanKl_20250117.xml"
    method: GET
    headers:
      Accept: application/xml
      Content-Type: application/xml
    sensor:
      - name: "Plan 9o"
        json_attributes_path: "$.Klassen[12].Pl[0]"
        value_template: "{{ value_json['@St'] }}"

The double // after schoolnumber is correct. What am I doing wrong?

For your help many thanks in advance. Have a nice week and please stay healthy.

Additional here the error message:

Logger: homeassistant.helpers.template
Quelle: helpers/template.py:2758
Erstmals aufgetreten: 19:18:53 (1 Vorkommnisse)
Zuletzt protokolliert: 19:18:53

Template variable warning: 'dict object' has no attribute '@St' when rendering '{{ value_json['@St'] }}'

Ronny

Totally unclear if your source data contains all those “**”

I would do this first:

sensor:
      - name: "Plan 9o"
        value_template: "{{ value_json }}"

Then see what you get to navigate, it looks lke this:

{
  "WplanVp": {
    "Kopf": {
      "planart": "K",
      "zeitstempel": "10.01.2025, 06:39",
      "DatumPlan": "Dienstag, 14. Januar 2025",
      "datei": "WPlanKl_date.xml"
    },
    "FreieTage": "\n    ...\n  ",
    "Klassen": {
      "Kl": {
        "Kurz": "9o",
        "Kurse": {},
        "Unterricht": {
          "Ue": [
            {
              "UeNr": {
                "UeLe": "Rog",
                "UeFa": "And",
                "#text": 179
              }
            },
            {
              "UeNr": {
                "UeLe": "Bau",
                "UeFa": "BIO",
                "#text": 180
              }
            },
            {
              "UeNr": {
                "UeLe": "Vol",
                "UeFa": "CH",
                "#text": 181
              }
            },
            {
              "UeNr": {
                "UeLe": "SeiJe",
                "UeFa": "DE",
                "#text": 182
              }
            },
            {
              "UeNr": {
                "UeLe": "HeiT",
                "UeFa": "EN",
                "#text": 183
              }
            },
            {
              "UeNr": {
                "UeLe": "Pic",
                "UeFa": "GE",
                "#text": 184
              }
            },
            {
              "UeNr": {
                "UeLe": "Ded",
                "UeFa": "GEO",
                "#text": 185
              }
            },
            {
              "UeNr": {
                "UeLe": "EDa",
                "UeFa": "GK",
                "#text": 186
              }
            },
            {
              "UeNr": {
                "UeLe": "Götz",
                "UeFa": "INF",
                "#text": 187
              }
            },
            {
              "UeNr": {
                "UeLe": "Rog",
                "UeFa": "KU",
                "#text": 188
              }
            },
            {
              "UeNr": {
                "UeLe": "Löf",
                "UeFa": "MA",
                "#text": 189
              }
            },
            {
              "UeNr": {
                "UeLe": "Frö",
                "UeFa": "MU",
                "#text": 190
              }
            },
            {
              "UeNr": {
                "UeLe": "Lüd",
                "UeFa": "PH",
                "#text": 191
              }
            },
            {
              "UeNr": {
                "UeLe": "KunFr",
                "UeFa": "RE/e",
                "#text": 192
              }
            },
            {
              "UeNr": {
                "UeLe": "Hein",
                "UeFa": "SPA",
                "#text": 193
              }
            },
            {
              "UeNr": {
                "UeLe": "Frö",
                "UeFa": "SPO",
                "#text": 194
              }
            },
            {
              "UeNr": {
                "UeLe": "Plö",
                "UeFa": "WTH",
                "#text": 195
              }
            },
            {
              "UeNr": {
                "UeLe": "HärI",
                "UeFa": "WTH",
                "#text": 362
              }
            }
          ]
        },
        "#text": "\n      **",
        "Pl": {
          "#text": "**\n        **",
          "Std": {
            "#text": "**\n          **",
            "St": 1,
            "#text1": "**\n          **",
            "Fa": "MA",
            "#text2": "**\n          **",
            "Le": "Löf",
            "#text3": "**\n          **",
            "Ra": "B.109",
            "#text4": "**\n          **",
            "Nr": 189,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text1": "**\n        **",
          "Std#1": {
            "#text": "**\n          **",
            "St": 2,
            "#text1": "**\n          **",
            "Fa": "MA",
            "#text2": "**\n          **",
            "Le": "Löf",
            "#text3": "**\n          **",
            "Ra": "B.109",
            "#text4": "**\n          **",
            "Nr": 189,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text2": "**\n        **",
          "Std#2": {
            "#text": "**\n          **",
            "St": 3,
            "#text1": "**\n          **",
            "Fa": "BIO",
            "#text2": "**\n          **",
            "Le": "Bau",
            "#text3": "**\n          **",
            "Ra": "B.110",
            "#text4": "**\n          **",
            "Nr": 180,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text3": "**\n        **",
          "Std#3": {
            "#text": "**\n          **",
            "St": 4,
            "#text1": "**\n          **",
            "Fa": "BIO",
            "#text2": "**\n          **",
            "Le": "Bau",
            "#text3": "**\n          **",
            "Ra": "B.110",
            "#text4": "**\n          **",
            "Nr": 180,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text4": "**\n        **",
          "Std#4": {
            "#text": "**\n          **",
            "St": 5,
            "#text1": "**\n          **",
            "Fa": "GE",
            "#text2": "**\n          **",
            "Le": "Pic",
            "#text3": "**\n          **",
            "Ra": "B.209",
            "#text4": "**\n          **",
            "Nr": 184,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text5": "**\n        **",
          "Std#5": {
            "#text": "**\n          **",
            "St": 6,
            "#text1": "**\n          **",
            "Fa": "GE",
            "#text2": "**\n          **",
            "Le": "Pic",
            "#text3": "**\n          **",
            "Ra": "B.209",
            "#text4": "**\n          **",
            "Nr": 184,
            "#text5": "**\n          **",
            "If": {},
            "#text6": "**\n          **"
          },
          "#text6": "**\n        **"
        },
        "#text1": "**\n    "
      }
    }
  }
}
1 Like

Hello @kbrown01 .

Thanks a lot for helping me. When I use a online tool for coverting the downloaded XML file to a JSON File, I got exactly this:

I changed

    sensor:
      - name: "Plan 9o"
        json_attributes_path: "$.Klassen[12].Pl[0]"
        value_template: "{{ value_json['@St'] }}"

to

    sensor:
      - name: "Plan 9o"
        json_attributes_path: "$.Klassen[12].Pl[0]"
        value_template: "{{ value_json }}"

How can I check in Home Assistant, if it works? The sensor shown “unknown”.

Thanks again. Have a good start in this day.

Ronny

P.S. Here additional the error message from Home Assistant log:

Logger: homeassistant.helpers.entity
Quelle: helpers/entity.py:1227
Erstmals aufgetreten: 06:05:19 (2 Vorkommnisse)
Zuletzt protokolliert: 06:05:19

Failed to set state for sensor.plan_9o, fall back to unknown
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1227, in _async_write_ha_state
    hass.states.async_set_internal(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        entity_id,
        ^^^^^^^^^^
    ...<5 lines>...
        time_now,
        ^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2364, in async_set_internal
    state = State(
        entity_id,
    ...<8 lines>...
        timestamp,
    )
  File "/usr/src/homeassistant/homeassistant/core.py", line 1808, in __init__
    validate_state(state)
    ~~~~~~~~~~~~~~^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 237, in validate_state
    raise InvalidStateError(
    ...<2 lines>...
    )
homeassistant.exceptions.InvalidStateError: Invalid state with length 36324. State max length is 255 characters.

I’ll be honest: unfortunately, I can’t do much with it.

json_attributes_path is used for selecting the level of the data structure that contains the keys listed in json_attributes from which sensor attributes are read (docs).

value_template is used for filtering the full response down to a single sensor state.

They are unrelated: one does not affect the other. Your error message is because you were reading the entire 36324 byte response (not filtered by json_attributes_path) into the sensor state, which has a 255 character limit.

I’ve taken the XML above, removed your formatting (please don’t do that as it makes the data unreadable), converted to JSON and minified it. Paste this into the Template Editor:

{% set value_json = {"WplanVp":{"Kopf":{"planart":"K","zeitstempel":"10.01.2025, 06:39","DatumPlan":"Dienstag, 14. Januar 2025","datei":"WPlanKl_date.xml"},"Klassen":{"Kl":{"Kurz":"9o","Kurse":"","Unterricht":{"Ue":[{"UeNr":{"_UeLe":"Rog","_UeFa":"And","__text":"179"}},{"UeNr":{"_UeLe":"Bau","_UeFa":"BIO","__text":"180"}},{"UeNr":{"_UeLe":"Vol","_UeFa":"CH","__text":"181"}},{"UeNr":{"_UeLe":"SeiJe","_UeFa":"DE","__text":"182"}},{"UeNr":{"_UeLe":"HeiT","_UeFa":"EN","__text":"183"}},{"UeNr":{"_UeLe":"Pic","_UeFa":"GE","__text":"184"}},{"UeNr":{"_UeLe":"Ded","_UeFa":"GEO","__text":"185"}},{"UeNr":{"_UeLe":"EDa","_UeFa":"GK","__text":"186"}},{"UeNr":{"_UeLe":"Götz","_UeFa":"INF","__text":"187"}},{"UeNr":{"_UeLe":"Rog","_UeFa":"KU","__text":"188"}},{"UeNr":{"_UeLe":"Löf","_UeFa":"MA","__text":"189"}},{"UeNr":{"_UeLe":"Frö","_UeFa":"MU","__text":"190"}},{"UeNr":{"_UeLe":"Lüd","_UeFa":"PH","__text":"191"}},{"UeNr":{"_UeLe":"KunFr","_UeFa":"RE/e","__text":"192"}},{"UeNr":{"_UeLe":"Hein","_UeFa":"SPA","__text":"193"}},{"UeNr":{"_UeLe":"Frö","_UeFa":"SPO","__text":"194"}},{"UeNr":{"_UeLe":"Plö","_UeFa":"WTH","__text":"195"}},{"UeNr":{"_UeLe":"HärI","_UeFa":"WTH","__text":"362"}}]},"Pl":{"Std":[{"St":"1","Fa":"MA","Le":"Löf","Ra":"B.109","Nr":"189","If":""},{"St":"2","Fa":"MA","Le":"Löf","Ra":"B.109","Nr":"189","If":""},{"St":"3","Fa":"BIO","Le":"Bau","Ra":"B.110","Nr":"180","If":""},{"St":"4","Fa":"BIO","Le":"Bau","Ra":"B.110","Nr":"180","If":""},{"St":"5","Fa":"GE","Le":"Pic","Ra":"B.209","Nr":"184","If":""},{"St":"6","Fa":"GE","Le":"Pic","Ra":"B.209","Nr":"184","If":""}]}}}}} %}
{{ value_json['WplanVp']['Klassen']['Kl']['Pl']['Std'][0] }}
{{ value_json['WplanVp']['Klassen']['Kl']['Pl']['Std'][0]['St'] }}

That returns the same data as you’ve pasted above, and then selects St from it. Is that what you want?

If it is, put that last line as your value_template and delete the json_attributes_path.

1 Like

Hello @Troon.

Also a big thank you for helping me.

I’m very sorry. I didn’t know that. I will try your hint today. Unfortunately I’m not so good in coding. :wink:

“Much to learn, you still have.” YODA

No need to apologise. Have a go at what I suggested, which should get you the 1 from the St value.

If you want other data — the whole structure you highlighted in bold, for example — carefully explain which bits of that data you want, what they represent and how you want them.

For example, you could add:

json_attributes_path: "$.WplanVp.Klassen.Kl.Pl"
json_attributes:
  - Std

and your sensor would contain all of the “Pl.Std” list within its Std attribute.

1 Like

Hello @Troon

Thanks a lot - again. :wink: Thats helps me to understand the code a lit bit.

The XML file is the school plan for my kids with the following structur:

Kl = class → here class 9o
Pl = plan for the day xx
St = lesson no. (example 1, 2, 3 and so on)
Fa = school subject
Le = teacher in short form
Ra = subject room

I want to show the school plan on my dashboard. For this a need attributes or sensors, which I can bring in card on the dashbord. The other is a text output like:

  1. Maths Room B.123 Löf
  2. Maths Room B.123 Löf
  3. German Room A.148 Sei

and so on. When there is no lesson no. (7, 8, or so), the attribute or sensor should be shown nothing. I hope I was able to explain my plan a little. Unfortunately, my English is not the best. :smile:

Have a nice day and week.

Ronny

Yes, it is. It gave you extra trouble and I was brought up to be polite. For me, apologising for mistakes is part and parcel of good manners. :wink:

Have you manually cut the XML down to just class 9o, or is that what you get from the URL?

If you’ve posted the real, full XML above, then try the json_attributes I suggested in my last post to give:

    sensor:
      - name: "Plan 9o"
        value_template: "{{ now() }}"
        json_attributes_path: "$.WplanVp.Klassen.Kl.Pl"
        json_attributes:
          - Std

and see what that returns. I’m hoping the sensor state will be the timestamp it was read, and you’ll see a Std attribute containing this (with all six lessons not just two):

[
    {
      "St": "1",
      "Fa": "MA",
      "Le": "Löf",
      "Ra": "B.109",
      "Nr": "189",
      "If": ""
    },
    {
      "St": "6",
      "Fa": "GE",
      "Le": "Pic",
      "Ra": "B.209",
      "Nr": "184",
      "If": ""
    }
]
1 Like

I have shortened it manually for the forum enquiry. The class is

Kurz>9o<\Kurz>

Should I post the complete XML file later again? I had thought that I could just reduce it with the JSON-PATH and thus already filter the class 9o.

If the complete XML is not filtered, I would still have to search for the parameter ‘Kurz’ and the value ‘9o’, wouldn’t I?

Yes, which is easy to do. So does it look like this, with lots of Kl elements inside a single Klassen?

<Klassen>
<Kl>
<Kurz>9o</Kurz>
[...]
</Kl>
<Kl>
<Kurz>another class</Kurz>
[...]
</Kl>
</Klassen>

If so, this might work:

sensor:
      - name: "Plan 9o"
        value_template: "{{ now() }}"
        json_attributes_path: "$.WplanVp.Klassen.Kl[[email protected]=='9o'].Pl"
        json_attributes:
          - Std