StecaGrid XML Data

I have a StecaGrid 3011 and trying to get some data out of it.

I found that it gives and XML readout.
http://192.168.0.251/measurements.xml


Is there and easy quick solution to get this data into home assistant?
Kinda only need this data <Measurement Value="365.0" Unit="W" Type="AC_Power"/>

Regards
Energy

1 Like

Well many ways to “skin a cat” but one way is using a REST sensor. Of course, you would use your own resource URL and maybe you want some other value as the state (especially if you want to update based on some changes). You may also only want to go after the attributes of interest to you. I typically read everything into an attribute and then use templates to find what I want because after implementation I always think, damn I need that too … and that.

So this would read the whole “root” element (note I tend to use split configuration files and this would be in sensor.yaml and included with sensor: in your configuration.yaml). If you don’t, this should have sensor: at the top of the YAML.

- platform: rest
  name: stecagrid
  resource: http://192.168.2.245:8123/local/Stecagrid/result.xml
  value_template: "{{ now() }}"
  json_attributes:
    - root

Now this would create a sensor with your entire XML as an attribute … root:

Then you can use in automations through Jinja. For example:

{{ state_attr('sensor.stecagrid','root').Device.Measurements }}

Would give me the Measurement array. I only put three in this sample but you could get them all:

Result type: dict
{
  "Measurement": [
    {
      "@Value": "228.0",
      "@Unit": "V",
      "@Type": "AC_Voltage"
    },
    {
      "@Value": "365.0",
      "@Unit": "W",
      "@Type": "AC_Power"
    },
    {
      "@Value": "344.8",
      "@Unit": "V",
      "@Type": "LINK_Voltage"
    }
  ]
}

You can then narrow down farther to those things you desire. If you do want to trigger off the changes (like a value exceeds some value) then you likely want to parse deeper and create individual attributes or even additional sensors that have a state that is only that value.

Like, if I knew that my entry is always the second one in the “0” indexed array, this would yield “365”:

{{ state_attr('sensor.stecagrid','root').Device.Measurements.Measurement[1]['@Value']  }}

That is really quick and dirty, you can use many other methods to find the “Measurement” of interest. You can also use “jq” to parse … as I said, many ways to “skin a cat”

Thank you for all this information. i get stuck early on.

I added this to configuration.yaml.

When i try to reboot it throws this error.
image
I can understand that the json_attributes: is wrong, but what im i missing?

Indenting on lines 18 - 22 is off. Needs to be indented. If you look above, the “p” in platform and the remainder of the lines are at same indentation.

I would note that I fond it much easier to chop up configuration.yaml into chunks. As your system grows having everything in one file will be a nightmare. Like in this case, I have my configuration.yaml with this line in it:

sensor: !include sensor.yaml

In fact, I have all these:

zone: !include zones.yaml
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
rest: !include rest.yaml
rest_command: !include rest_command.yaml
sensor: !include sensor.yaml
binary_sensor: !include binary_sensor.yaml
template: !include template.yaml
light: !include light.yaml
input_boolean: !include input_boolean.yaml
input_number: !include input_number.yaml
input_select: !include input_select.yaml
media_player: !include media_player.yaml
timer: !include timers.yaml
var: !include vars.yaml
camera: !include cameras.yaml

Then the individual files themselves are in the same directory with the names. So I have a sensor.yaml that has this:

- platform: rest
  name: stecagrid
  resource: http://192.168.2.245:8123/local/Stecagrid/result.xml
  value_template: "{{ now() }}"
  json_attributes:
    - root.Device.Measurements.Measurement

Actually I would not post here but it has about 18 sensors in it. It makes things much easier to organize and find (and change) things.

I just started using Home Assistant and also have a STECAGRID inverter. My goal was to be able to get solar data in HA and also upload the data to PVOutput. For the HA integration of the solar data from the STECA inverter I used information from this thread : Cannot parse @ in value template sensor - Configuration - Home Assistant Community (home-assistant.io)

As a complete noob in HA and scripting/programming it took me a few days to get things working. For the others out there who want to get the solar data from the STECAGRID, here is the code i used in the configuration.yaml :

# Solar - Reads the measurements.xml every 10 seconds
  - platform: rest
    resource: http://YourSTECAInverterIP/measurements.xml
    json_attributes:
      - root
      - Measurements
    name: solar_inverter
    scan_interval: 10
    value_template: 'OK'

# Solar - Specifies the sensors and values
  - platform: template
    sensors:
      solar_live_voltage:
        value_template: >
            {% set root = state_attr('sensor.solar_inverter', 'root') %}
            {% if root.Device.Measurements.Measurement[0]["@Value"] is defined %}
                {{ root.Device.Measurements.Measurement[0]["@Value"] }}
            {% else %}
                0
            {% endif %}
        device_class: Voltage
        unit_of_measurement: 'V'
        friendly_name: AC Voltage

      solar_live_output:
        value_template: >
            {% set root = state_attr('sensor.solar_inverter', 'root') %}
            {% if root.Device.Measurements.Measurement[2]["@Value"] is defined %}
                {{ root.Device.Measurements.Measurement[2]["@Value"] | round(0)}}
            {% else %}
                0
            {% endif %}
        device_class: power
        unit_of_measurement: 'Watt'
        friendly_name: Live power

To upload the solar data to PVOutput i use this :

# Solar - Sensor for 5 min average generation for Pvoutput
  - platform: statistics
    name: solar_energy_generation_5min
    entity_id: sensor.solar_live_output
    state_characteristic: mean
    max_age:
      minutes: 5
    sampling_size: 300
    
# Solar - Upload solar_energy_generation_5min to pvoutput
rest_command:
  pvoutput_generation:
    method: POST
    url: https://pvoutput.org/service/r2/addstatus.jsp
    headers:
      X-Pvoutput-Apikey: YourAPIKeyHere
      X-Pvoutput-SystemId: YourSytemIDHere
    payload: 'd={{now().strftime("%Y%m%d")}}&t={{now().strftime("%H:%M")}}&v2={{states.sensor.solar_energy_generation_5min.state|round(1)}}'
    content_type: "application/x-www-form-urlencoded"

And last but not least there is an automation (in the automations.yaml) that activates the upload of the data every 5 minutes :

alias: PVOutput Uploader (rest_command)
description: Uploads values to PVOutput
trigger:
  - platform: time_pattern
    minutes: /5
    seconds: '0'
condition: []
action:
  - service: rest_command.pvoutput_generation
    data: {}
mode: single

I would like to have some detail about the solar production, Daily production, Monthly production and Yearly production. But I have not yet figured that out. Maybe someone can help?

Cheers

Hi!

Thank you for the work so far. I took your work and expanded on it.
My code is stored in github, GitHub - jleivo/HA-stegagrid: Configurations to show Stegagrid data in HomeAssistant . I just got this evening the statistics to work show that the data can be shown in Energy dashboard.
Does my expansion fit your needs? I am hoping that the Energy dashboard gives the statistics :slight_smile:

2023-02-11 21:40
Jumping the gun here. Statistics don’t work yet :D. Any way working on it when I have the time

1 Like

For statistical data I’d suggest getting hourly accumulated reading from the inverter.
The biggest advantage being if network or HA goes down – history data collected by HA becomes incorrect for that hour of the day. The inverter, I assume, has some more sophisticated calculation algorithm and persistence.

In my use-case I don’t care much for keeping the history, but I need to know how many Wh exactly has been generated during the current hour, so I can spend those Wh on water heating. Here’s the python script I’ve used for my StecaGrid 3203:

from urllib.request import urlopen, Request
from datetime import datetime
from json import loads as json_loads


def get_readings_for_current_hour():
    """
        Get readings for current hour so far in kWh.

        Assumes that subsequent readings for current hour are 0 kW
    """
    now = datetime.now()
    try:
        response = urlopen(Request("http://192.168.1.16/yields.json"))
    except:
        print("Can't reach the panels")
        return []
    r = response.read().decode("utf-8")
    data = json_loads(r).get("DayCurves").get("Datasets")[0].get("Data")
    # Inverter groups readings for every 10 minutes. Reading for 12:00 are what's accumulated during 11:50-12:00
    readings_count_this_hour = now.minute // 10 + 1
    for day_data in data:
        parsed_date = datetime.strptime(day_data.get("Timestamp"), "%Y-%m-%d").date()
        offset = day_data.get("Offset")
        if (parsed_date == now.date()):
            # Getting only readings for current hour, which are the last n ones in the list
            return round((sum(day_data.get("Data")[-readings_count_this_hour:])/6000), 2)