LenzGr / pytalent-monitor

A Python Script to gather some data from the Talent Monitoring REST API
MIT License
6 stars 2 forks source link

Info: Application of pytalent-monitor with my home assistant Installation. #4

Closed melibokus-h2 closed 4 months ago

melibokus-h2 commented 1 year ago

Ich versuche mal die Lösung in meiner Umgebung zu nutzen, bin aber blutiger Anfänger (nutze deshalb auch Issue zur Kommentierung, weil ich gerade keine andere Möglichkeit finde).

! Work in progress at the moment - no results so far, but to e continued here.

Environment

  1. User/Interfaces 1.1. User experience level: Rookie in terms if Home Assistant and Python, but computer scientist 1.2. User Interface 1: Talent Home App (Android) 1.3. User Interface 2: Talent Pro App (Android) 1.4. User Interface 3: Talent Monitoring Web Site 1.5. User Interface 4: Home Assistant App (Android) 1.6. User Interface 5: Local Home Assistant Website 1.7. User Interface 6: Local FritzBox Website
  2. Home assistant platform: Home Assistant 2023.9.2 on Raspberry Pi 4 + Home Assistant Operating System
  3. Small home PV (Balkonsolaranlage): TSUN (Vertrieb DE: Solakon, 810W / 600W Balkonkraftwerk Upgradebar Photovoltaik Stecker Solaranlage) 3.1. Inverter: MS 600 {correct type name to be checked} 3.2. Storage: Anker SOLIX Solarbank E1600 (expected to come)
  4. Additional measuring device: FritzDect 200

Objectives

  1. Make data from Talent Monitoring Web Site available with Home Assistant dashboards
  2. Allow specific control of charging to Anker Solix battery (perspectively)

Proceeding

After trying out different approaches (see below) I now will try out an approach on base of Home Assistant Development Environment. Motivation was the below citation: "One Home Assistant rule is that the integration should never interface directly with devices. Instead, it should interact with a third-party Python 3 library. This way, Home Assistant can share code with the Python community and keep the project maintainable."

  1. Read through Home Assistant Developer Docs and install Docker, Visual Studio code and Git

  2. Install environment as described

Former/Failed Approaches

  1. Check how to integrate Lenzens pytalent-monitor with my home assistant installation 1.1. Google: Use custom python script with home assistant. https://www.home-assistant.io/integrations/python_script/ 1.2. Question occured: What is the runtime environment in Home Assistant to be used executing the pytalent-monitor? Answer: appdaemon? 1.3. Using python_script integration of Home Assistant. There are three different python integrations available with Home Assistant 1.3.1. Python_script Tried it unsuccessfully most likely due to beginner errors. However it states: “It is not possible to use Python imports with this integration. If you want to do more advanced scripts, you can take a look at AppDaemon or pyscript" 1.3.2 AppDaemon I'll try to use this. 1.3.3 Pyscript Didn't produce easy results due to my lack of expertise. 1.4. After gambeling around unsuccesfully due to lack of expertise I decided for Appdaemon and will follow the promising article - Building custom integrations with python 1.4.1. Preparation 1.4.1.2. Install appdaemon with Home Assistant as described 1.4.1.3. With ssh access to home assistant mkdir /config/download cd /config/download git clone https://github.com/LenzGr/pytalent-monitor cp pytalent-monitor/pytalent-monitor.py /config/appdaemon/apps cd /config/appdaemon/apps

Add to apps.yaml: pytalent-monitor: module: pytalent-monitor class: TalentSolarMonitor

Add to pytalent-monitor.py Import hass.Hass.... ... to be continued here

Outlook 1, citation

"One Home Assistant rule is that the integration should never interface directly with devices. Instead, it should interact with a third-party Python 3 library. This way, Home Assistant can share code with the Python community and keep the project maintainable."

Trying to come up with some definitions:

Sackgasse 2 1.4.1.4. After ./pytalent-monitor.py --username <> --password <> got:

Traceback (most recent call last): File "/config/appdaemon/apps/./pytalent-monitor.py", line 7, in import requests ModuleNotFoundError: No module named 'requests'

Sackgasse 2:

As Lenzen uses import, this is probably not a solution (see error below) 1.3.1. Create configuraton.yaml entry as described 1.3.2. ./. 1.3.3. Download Lezens script dorthin with ssh/wget 1.3.3.4. Open a ssh shell with Home Assistant instance by eg uding the Addon "Advanced SSH & Web Terminal" within Home Assistant UI 1.3.3.5. Create the directory for python scripts and dowload Lenzens script from github mkdir /configure/python_scripts cd /configure/python_scripts wget https://github.com/LenzGr/pytalent-monitor/blob/main/pytalent-monitor.py chmod 777 pytalent-monitor.py 1.3.4. Execute script with appropriate parameter settings ./pytalent-monitor.py --username --password ==> results to ./pytalent-monitor.py: line 1: payload:allShortcutsEnabled:false: command not found

LenzGr commented 1 year ago

Thanks a lot for the detailed feedback and for sharing your research, I greatly appreciate it. Unfortunately, I am pretty much stuck at a similar step - figuring out how to embed this script into Home Assistant so that the retrieved values can be collected and visualized. The learning curve is steep. I initially thought of creating a "real" HA integration, but that requires many additional steps that I don't know about.

I haven't looked deeply into your approach yet, I planned to upload the script to HA and run it via the Command Line Integration.

LenzGr commented 1 year ago

The key challenge is that you need to find a way to run the Python script so that HA treats its output as sensor data. As far as I understand it, your approaches simply execute scripts as services.

melibokus-h2 commented 1 year ago

... thanks for the answers. Please note I'm just playing around and try to find solutions. At the moment I would not encourage you to dig to much into my approach as it is still just my learning curve. However I'm trying to understand how a custom integration would work as "just executing the python script" didn't produce the quick results I expected. I will continue to work on the topic but it will take some time ..

StephanU commented 5 months ago

Hi @melibokus-h2 and @LenzGr, I would like to support the implementation of the HA integration. Imho, the fastest/easiest way is implementing a custom integration for HACS. Implementing such a custom integration is (almost) the same as implementing a "real HA integration" with one exception being that we don't have to wait for HA releases to see the code changes in the integration. Instead we can just add the git repository of "our" :) integration as custom repository to HACS and have the updated version right away in our HA instances. This is especially helpful during the development phase. The first step would be to have this library published in PyPi. (see #3). I would than create a skeleton project and start implementing the integration. We might need to change this library a bit in order to use it in a HA integration, but we'll see which changes are needed.

LenzGr commented 5 months ago

Hi @StephanU - thank you for your offer to help, much appreciated! That HACS route sounds like a workable approach to me. I'll look into merging your PR and submitting the library to PyPi.

LenzGr commented 5 months ago

@StephanU I merged your PR (#3) and tested the script, but it now fails with an interesting error (this does not seem to be related to your changes) - see #8 for details. Does the script still work for you?

StephanU commented 5 months ago

I created an account using the "Talent Pro" app (which was a bit difficult due to the inconclusive password rules) and the script was able to login using my credentials, but crashed in https://github.com/LenzGr/pytalent-monitor/blob/main/pyTalentMonitor/__init__.py#L71, since I don't have a power station registered (yet). So yes, the login using the script does work for me.

StephanU commented 5 months ago

I created a skeleton project for a custom integration and started implementing: https://github.com/StephanU/ha-talent-monitor :) The integration contains a copy of the pytalent-monitor script (until the script is published in PyPi).

You can try out the current state of the integration by installing it via HACS (see https://github.com/StephanU/ha-talent-monitor?tab=readme-ov-file#via-hacs).

The integration will ask for your username/password, login to the talent monitor api and also fetch the data, but it does not create any devices/entities, yet. The fetched data will be logged to the HA server logs (if you enable debug logging for the integration).

SInce I don't have my PV (yet), I do not receive any data from the talent monitor api. So, it would be really helpful 😄 , if you could try out the integration and maybe send me your server logs (by posting them here or by sending me an email). If you do so, please replace/delete any data in the server logs you don't want to share.

LenzGr commented 5 months ago

Hi @StephanU wow, you're on fire! Thank you so much for your support. I feel like I'm being a bottleneck here. I've just merged your pending PR. I'd be glad to make you either a contributor on this project or encourage you to take it over by forking it and perform the publishing to PyPi on your own. I haven't been able to devote much time to this project lately, so I'd be more than happy to hand it over to someone with more available bandwidth.

LenzGr commented 5 months ago

I'll try to get your integration installed in my local HA environment - thank you for preparing this.

LenzGr commented 5 months ago

OK, setting up HACS and adding the integration was a bit tricky, but I think I got it done. I'll improve my PR that updates the README accordingly.

Here's the debug log output - I redacted a number of values in the output for privacy reasons:

2024-05-26 09:02:05.977 DEBUG (MainThread) [custom_components.talent_monitor] async_setup_entry
2024-05-26 09:02:05.977 DEBUG (MainThread) [custom_components.talent_monitor.coordinator] _async_update_data 
2024-05-26 09:02:06.165 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Login successful - received token: [redacted]
2024-05-26 09:02:06.469 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] GUID: [redacted]
2024-05-26 09:02:06.525 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Data for powerstation GUID [redacted]: {"msg": "Operation successful", "code": 200, "data": {"yearBattDischargeEnergy": 0.0, "yearLoadEnergyNamed": "0.00 Wh", "enableFitInApp": false, "monthEnergy": 44590.0, "yearBattChargeEnergy": 0.0, "totalEnergy": 301180.0, "peakHour": 0.14634146341463414, "stationType": "1", "totalActivePower": 94.4, "gridSidePower": 0.0, "lastDataUpdateTime": "2024-05-26T06:52:41", "statusNamed": "Online", "yearEnergyNamed": "118.06 kWh", "stationTypeNamed": "Household use", "yearBattChargeEnergyNamed": "0.00 Wh", "timezoneOffset": "+02:00", "gridSidePowerNamed": "0.00 W", "layoutMeta": "{}", "locationLongitude": [redacted], "dayEnergyNamed": "120.00 Wh", "totalPeakPower": 94.4, "totalPeakPowerNamed": "94.40 W", "dayEnergy": 120.0, "monthBattChargeEnergyNamed": "0.00 Wh", "images": [], "deptId": 101, "timeZone": "Europe/Berlin", "yearGridsellEnergyNamed": "0.00 Wh", "monthBattDischargeEnergy": 0.0, "monthLoadEnergy": 0.0, "yearEnergy": 118060.0, "yearBattDischargeEnergyNamed": "0.00 Wh", "monthLoadEnergyNamed": "0.00 Wh", "electricityGain": 0.0, "deptCode": "[redacted]", "yearGridbuyEnergyNamed": "0.00 Wh", "status": "ready", "isFavorite": 0, "totalActivePowerNamed": "94.40 W", "monthBattChargeEnergy": 0.0, "gridConnectedType": "1", "monthGridbuyEnergyNamed": "0.00 Wh", "co2Reduced": "300.28 KG", "yearGridsellEnergy": 0.0, "treesPlanted": "0.82", "buildDate": "2023-06-17", "monthGridsellEnergy": 0.0, "yearLoadEnergy": 0.0, "monthEnergyNamed": "44.59 kWh", "installedCapacity": 820.0, "lightingHours": "23.19K", "gridConnectedTypeNamed": "Full access to the Internet", "powerStationId": [redacted] "stationName": "priwatt priWall duo", "monthGridbuyEnergy": 0.0, "currency": "EUR", "monthGridsellEnergyNamed": "0.00 Wh", "powerStationGuid": "[redacted]", "owner": "User [redacted]", "monthBattDischargeEnergyNamed": "0.00 Wh", "locationLatitude": [redacted], "battSidePowerNamed": "0.00 W", "userId": "[redacted]", "ownerEmail": "[redacted]", "totalEnergyNamed": "301.18 kWh", "earnings": "0.00", "lastDataUpdateTimeOffseted": "2024-05-26T08:52:41", "ownerUserId": [redacted], "installedCapacityNamed": "820.00 Wp", "guests": [], "location": "[redacted]", "yearGridbuyEnergy": 0.0, "businessType": "1", "battSidePower": 0.0}}
2024-05-26 09:02:08.012 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Data for inverter GUID [redacted]: {"msg": "Operation successful", "code": 200, "data": {"pv1CumulativePowerGeneration": 240510.0, "pv": [{"current": 1.48, "powerNamed": "49.90 W", "power": 49.9, "key": "PV1", "voltage": 33.6}, {"current": 1.44, "powerNamed": "48.50 W", "power": 48.5, "key": "PV2", "voltage": 33.5}, {"current": 0.0, "powerNamed": "0.00 W", "power": 0.0, "key": "PV3", "voltage": 0.0}, {"current": 0.0, "powerNamed": "0.00 W", "power": 0.0, "key": "PV4", "voltage": 0.0}, {"current": null, "powerNamed": null, "power": null, "key": "PV5", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV6", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV7", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV8", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV9", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV10", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV11", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV12", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV13", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV14", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV15", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV16", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV17", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV18", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV19", "voltage": null}, {"current": null, "powerNamed": null, "power": null, "key": "PV20", "voltage": null}], "totalActivePower": 94.4, "lastDataUpdateTime": "2024-05-26T06:52:41", "pv4PowerNamed": "0.00 W", "detectionStatusWord1": "0", "energyTodayNamed": "120.00 Wh", "acPhaseExpress": "A,B,C", "deviceId": [redacted], "clientTime": "2024-05-26T06:52:41", "statusNamed": "Online", "productName": "Microinv", "yearEnergyNamed": "198.38 kWh", "pv3CumulativePowerGeneration": 0.0, "timezoneOffset": "+02:00", "firmwareVersion3": "20497", "firmwareVersion2": "0", "detectionStatusWord0": "2", "phaseAActivePower": 94.4, "firmwareVersion1": "V5.0.11", "pv1DailyPowerGeneration": 60.0, "isEnableOta": true, "model": "TSOL-MS600", "phase": [{"activePowerNamed": "94.40 W", "current": 0.4, "reactivePower": null, "apparentPower": null, "lineVoltage": null, "reactivePowerNamed": null, "voltage": 234.8, "frequency": 50.0, "powerFactor": null, "activePower": 94.4}, {"activePowerNamed": null, "current": null, "reactivePower": null, "apparentPower": null, "lineVoltage": null, "reactivePowerNamed": null, "voltage": null, "frequency": null, "powerFactor": null, "activePower": null}, {"activePowerNamed": null, "current": null, "reactivePower": null, "apparentPower": null, "lineVoltage": null, "reactivePowerNamed": null, "voltage": null, "frequency": null, "powerFactor": null, "activePower": null}], "ratedPower": 800, "serialNumber": "[redacted]", "brandEn": "TSUN", "inverterTemp": 24.0, "phaseAActivePowerNamed": "94.40 W", "pv4DailyPowerGeneration": 0.0, "deviceTypeGuid": "[redacted]", "standbyTime": 0.0, "status": "ready", "isFavorite": 0, "totalActivePowerNamed": "94.40 W", "pv2PowerNamed": "48.50 W", "creationTime": "2023-07-05T12:16:06", "phaseACurrent": 0.4, "pv2CumulativePowerGeneration": 217070.0, "monthEnergyNamed": "75.43 kWh", "pvCount": 3, "powerStationId": [redacted], "pv3DailyPowerGeneration": 0.0, "pv4CumulativePowerGeneration": 0.0, "powerStationGuid": "[redacted]", "pv2DailyPowerGeneration": 60.0, "phaseAVoltage": 234.8, "pv1PowerNamed": "49.90 W", "energyTotal": 457580.0, "brandCn": "TSUN", "pv3PowerNamed": "0.00 W", "phaseAFrequency": 50.0, "energyToday": 120.0, "updateTime": "2023-07-05T12:16:06", "acPhaseCount": 1, "stringNumber": 2, "energyTotalNamed": "457.58 kWh", "parentDeviceGuid": "[redacted]", "faultCode2": 0.0, "ratedPowerNamed": "800.00 W", "faultCode1": 0.0, "faultCode4": 0.0, "nameOfManufacturer": "TSUN", "deviceGuid": "[redacted]", "faultCode3": 0.0, "timezoneId": "Europe/Berlin"}}
2024-05-26 09:02:08.012 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Status: ready
2024-05-26 09:02:08.012 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] StationName: priwatt priWall duo
2024-05-26 09:02:08.013 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] TotalActivePower(W): 94.4
2024-05-26 09:02:08.013 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] DailyEnergy(Wh): 120.0
2024-05-26 09:02:08.013 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] MonthlyEnergy(Wh): 44590.0
2024-05-26 09:02:08.013 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] YearlyEnergy(Wh): 118060.0
2024-05-26 09:02:08.014 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel1Voltage(V): 33.6
2024-05-26 09:02:08.014 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel1Current(A): 1.48
2024-05-26 09:02:08.014 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel1Power(W): 49.9
2024-05-26 09:02:08.014 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel2Voltage(V): 33.5
2024-05-26 09:02:08.014 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel2Current(A): 1.44
2024-05-26 09:02:08.015 DEBUG (MainThread) [custom_components.talent_monitor.pyTalentMonitor] Panel2Power(W): 48.5
2024-05-26 09:02:08.015 DEBUG (MainThread) [custom_components.talent_monitor.coordinator] Finished fetching talent_monitor data in 2.038 seconds (success: True)
2024-05-26 09:02:08.015 DEBUG (MainThread) [custom_components.talent_monitor] received data null
LenzGr commented 5 months ago

@StephanU does this output help you? Please let me know if you need anything else.

StephanU commented 5 months ago

@LenzGr Perfect, thank you. I will have a look!

LenzGr commented 5 months ago

Looks like the output includes a number of metrics that I was planning on adding to the output of the Python module (#2)

StephanU commented 5 months ago

I just released a new version of the integration which now should show some sensors for "power stations" (which shows aggregated metrices for the associated inverters - as far as I understood) and inverters.

If you installed the integration via HACS already, you should be able to update it via HACS, then restart HA.

For each power stations and each inverter (for the small PVs "Balkonkraftwerke" it should always be one power station and one inverter) a device is created in HA. The device for the power station contains (some of) the power station's metrics as entities and the device for the inverter contains (some of) the inverter's metrics as entities (and information about manufacturer, model, serial number).

Bildschirmfoto 2024-06-06 um 22 25 41

Unfortunately, there is no way of creating a hierarchy of devices in HomeAssistant. Thus there is no way to show that the inverter is part of the power station. The same "problem" would be the case regarding the PVs: in the API those are part of the inverter data. We could create additional devices for each (of the two) or we could add entities to the inverter which show the PVs metrics, eg. "PV1 Current", "PV1 Power", "PV1 Voltage" and "PV2 Current", "PV2 Power", "PV2 Voltage". Not sure what's better...

StephanU commented 5 months ago

Inverter details:

Bildschirmfoto 2024-06-06 um 22 27 29
LenzGr commented 5 months ago

Great progress, @StephanU - nice work! I just updated my installation and it works! Thanks a lot for your support. There's a slight unit conversion issue that I reported here: https://github.com/StephanU/ha-talent-monitor/issues/8

LenzGr commented 4 months ago

@melibokus-h2 in case you're still interested, I think Stephan's integration is ready for testing - it works well for me.

I'll resolve this issue, let's move future discussions/questions to his project.