littleyoda / Home-Assistant-Tripower-X-MQTT

Script which reads out the current measured values of an inverter of the SMA Tripower x (STP XX-50) series and makes them available via MQTT Home Assistant.
The Unlicense
10 stars 3 forks source link

Tripower X20 "KeyError: 'content-length'" #2

Closed Trashmee closed 1 year ago

Trashmee commented 1 year ago

Hi,

I'd really like to get your script working, decoupeling scripts via MQTT is my preferred design. I just got an Tripower X20 installed, but did not yet get your script to work.

requests not installed:

 ./sma2mqtt.py
Traceback (most recent call last):
  File "/sma2mqtt/./sma2mqtt.py", line 5, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'

I fixed that by pip3 install requests. Just wondering if that's maybe not the right module, as its not mentioned in preqs and the area is similar to below issue.

KeyError: 'content-length'

./sma2mqtt.py
# Debug output:
{'Date': 'Wed, 12 Apr 2023 13:03:14 GMT', 'Cache-Control': 'no-cache,no-store,must-revalidate, no-store', 'Pragma': 'no-cache', 'Set-Cookie': 'JSESSIONID=node0tsg8tkvsge6y15og14ifxqiu15.node0;Path=/;HttpOnly', 'Expires': 'Thu, 01 Jan 1970 00:00:00 GMT', 'Content-Type': 'application/json', 'Transfer-Encoding': 'chunked'}

Traceback (most recent call last):
  File "/sma2mqtt/./sma2mqtt.py", line 59, in <module>
    if (x.headers["Content-Length"] == '0'):
  File "/usr/local/lib/python3.10/site-packages/requests/structures.py", line 52, in __getitem__
    return self._store[key.lower()][1]
KeyError: 'content-length'

When providing a wrong password I get a different output, so username / password seems to be ok?

./sma2mqtt.py

{'Date': 'Wed, 12 Apr 2023 13:10:33 GMT', 'Cache-Control': 'no-cache,no-store,must-revalidate, no-store', 'Pragma': 'no-cache', 'Content-Length': '0'}

Username or Password wrong.
Please test the following URL in a browser: https://192.168.177.30

My inverter (or browser?) redirects me to https:// , but even when replacing http:// with https:// in your script (and setting verify=false due to the self signed certificate that SMA uses) I get above error.

Did you edit any of the Inverters parameter to make this work? Which type of user access is required for the user to connect with? I tried user + admin. Any help to debug is appreciated, I tried to find stuff in my Browser console, but debugging webapps is not an area i have knowledge in.

littleyoda commented 1 year ago

I am not sure what the cause of your problem is.

  1. I fixed the crash that was caused by the missing Content-Length header. Not sure if this solves your problem.
  2. A added a requirements.txt for the needed libraries
  3. A "normal" user should be enough.
Trashmee commented 1 year ago

Hi LIttleYoda,

that's it - i get (a lot!) of data via MQTT now, thanks a lot for your superfast response.

Thanks, I'll start playing & let you know if I find something of interest, e.g. with HA integration.

ItsMee

littleyoda commented 1 year ago

Thanks, I'll start playing & let you know if I find something of interest, e.g. with HA integration.

Just to be sure and avoid misunderstanding. If you have the mqtt integration active in HA, the sensor values should be automatically recognised as a new device.

Trashmee commented 1 year ago

Nice, didn't use this one, but especially if there is a lot of not yet known parameters it seems very useful. Started working once i reverted the mqtt prefix to homeassistant - thanks for the hint.

ItsMee

Trashmee commented 1 year ago

@littleyoda: Did you integrated your inverter into the Energy dashboard? If so, do you have a more elegant variant than a template sensor on top of what the mqtt integration delivers?

  - sensor:
    - name: "PV energy (total Wh)"
      unique_id: "pv_energy_total"
      unit_of_measurement: "Wh"
      device_class: 'energy'
      state_class: total_increasing
      state: |
                 {{ states('sensor.metering_totwhout_pv_sma_sunny_tripower_serial') | float(default=0.0) }}
littleyoda commented 1 year ago

the MQTT implementation is indeed not optimal because of the problem with the conversion of the formats. I have to take a closer look again.

Until now, I have not found a better way than via Templates. I therefore also request the inverter power via Modbus and use this for the energy dashboard.

 - name: SMA
    type: tcp
    host: 192.168.2.161
    port: 502
    sensors:
      - name: sma_power_ac_raw
        state_class: measurement
        unit_of_measurement: W
        slave: 3
        address: 30775
        count: 2
        data_type: int32
        scan_interval: 10
        min_value: 0

      - name: sma_power_ac_l1_raw
        unit_of_measurement: W
        state_class: measurement
        slave: 3
        address: 30777
        count: 2
        data_type: int32
        scan_interval: 10
        min_value: 0

      - name: sma_power_ac_l2_raw
        unit_of_measurement: W
        state_class: measurement
        slave: 3
        count: 2
        address: 30779
        data_type: int32
        scan_interval: 10
        min_value: 0

      - name: sma_power_ac_l3_raw
        unit_of_measurement: W
        state_class: measurement
        slave: 3
        count: 2
        address: 30781
        data_type: int32
        scan_interval: 10
        min_value: 0

      - name: sma_power_total_production
        unit_of_measurement: Wh
        state_class: total_increasing
        device_class: energy
        slave: 3
        address: 30513
        scan_interval: 10
        data_type: int64

      - name: sma_status
        slave: 3
        address: 30201
        scan_interval: 10
        count: 2
        data_type: int32