JasperE84 / PyFusionSolarDataRelay

Interface to relay PV inverter data from FusionSolar public kiosk mode to InfluxDB/VictoriaMetrics, PVOutput.org, MQTT and Home Assistant
MIT License
20 stars 2 forks source link

Connect via MQTT to existing Home Assistant Docker instance #7

Closed arboeh closed 1 year ago

arboeh commented 1 year ago

I have problems using this project with an existing Home Assistant instance and the MQTT Broker addon from HA. I just need the connection to FusionSolar-Kiosk and MQTT-Entities in HA. I don't need another MQTT server and the InfluxDB stuff. Unfortunately MQTT Explorer doesn't show up topic "energy/pyfusionsolar" I modified the composer file to my need and created an user for the MQTT broker, but I'm not sure which port (1883/9001) and host (HA host, 0.0.0.0, localhost) to use to connect to the HAs' MQTT broker:

version: '3'

services:
  pyfusionsolar:
    container_name: pyfusionsolardatarelay
    image: jsprnl/pyfusionsolardatarelay:latest
    restart: unless-stopped

    environment:
      - pvdebug=True

      - pvfusionsolar=True
      - pvfusionsolarkkid=<KIOSK_TOKEN>
      - pvfusionminutecron=5

      - pvmqtt=True
      - pvmqtthost=??????????
      - pvmqttport=??????????
      - pvmqttauth=True
      - pvmqttuser=XXXXXXXXXX
      - pvmqttpasswd=XXXXXXXXXX

    volumes:
      - /etc/localtime:/etc/localtime:ro

Portainer-Log:


2022-10-10 14:17:08,135 - root - DEBUG - Conf class instantiated
2022-10-10 14:17:08,135 - root - DEBUG - Setting default conf values
2022-10-10 14:17:08,135 - root - INFO - Processing environment variables to running config
2022-10-10 14:17:08,136 - root - DEBUG - Pulled 'pvdebug=True' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvfusionsolar=True' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvfusionsolarkkid=XXXXXXXXXX' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvfusionminutecron=5' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvmqtt=True' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvmqtthost=XXXXXXXXXX' from the environment
2022-10-10 14:17:08,137 - root - DEBUG - Pulled 'pvmqttport=??????????' from the environment
2022-10-10 14:17:08,138 - root - DEBUG - Pulled 'pvmqttauth=True' from the environment
2022-10-10 14:17:08,139 - root - DEBUG - Enabled verbose logging
2022-10-10 14:17:08,139 - root - INFO - Current settings:
2022-10-10 14:17:08,139 - root - INFO - _Generic:
2022-10-10 14:17:08,139 - root - INFO - debug:   True
2022-10-10 14:17:08,140 - root - INFO - _FusionSolar:
2022-10-10 14:17:08,140 - root - INFO - enabled: True
2022-10-10 14:17:08,140 - root - INFO - fusionsolarurl: https://region01eu5.fusionsolar.huawei.com/rest/pvms/web/kiosk/v1/station-kiosk-file?kk=
2022-10-10 14:17:08,140 - root - INFO - fusionsolarkkid: XXXXXXXXXX
2022-10-10 14:17:08,141 - root - INFO - sysname: inverter01
2022-10-10 14:17:08,141 - root - INFO - fusionhourcron: *
2022-10-10 14:17:08,141 - root - INFO - fusionminutecron: 5
2022-10-10 14:17:08,141 - root - INFO - _Influxdb:
2022-10-10 14:17:08,141 - root - INFO - influx: False
2022-10-10 14:17:08,141 - root - INFO - influx2: True
2022-10-10 14:17:08,142 - root - INFO - host: localhost
2022-10-10 14:17:08,142 - root - INFO - port: 8086
2022-10-10 14:17:08,142 - root - INFO - _Influxdb_v1:
2022-10-10 14:17:08,142 - root - INFO - database: fusionsolar
2022-10-10 14:17:08,142 - root - INFO - user: fusionsolar
2022-10-10 14:17:08,142 - root - INFO - password: **secret**
2022-10-10 14:17:08,142 - root - INFO - _Influxdb_v2:
2022-10-10 14:17:08,142 - root - INFO - protocol: https
2022-10-10 14:17:08,143 - root - INFO - organization: acme
2022-10-10 14:17:08,143 - root - INFO - bucket: fusionsolar
2022-10-10 14:17:08,143 - root - INFO - token: XXXXXXX
2022-10-10 14:17:08,143 - root - INFO - _PVOutput.org:
2022-10-10 14:17:08,143 - root - INFO - Enabled: False
2022-10-10 14:17:08,143 - root - INFO - System ID: 12345
2022-10-10 14:17:08,144 - root - INFO - API Key: yourapikey
2022-10-10 14:17:08,144 - root - INFO - API Url: https://pvoutput.org/service/r2/addstatus.jsp
2022-10-10 14:17:08,144 - root - INFO - API BatchUrl: https://pvoutput.org/service/r2/addbatchstatus.jsp
2022-10-10 14:17:08,144 - root - INFO - _MQTT
2022-10-10 14:17:08,144 - root - INFO - Enabled: True
2022-10-10 14:17:08,144 - root - INFO - Host: ??????????
2022-10-10 14:17:08,144 - root - INFO - Port: ??????????
2022-10-10 14:17:08,144 - root - INFO - Auth: True
2022-10-10 14:17:08,145 - root - INFO - User: XXXXXXXXXX
2022-10-10 14:17:08,145 - root - INFO - Passwd: XXXXXXXXXX
2022-10-10 14:17:08,145 - root - INFO - Topic: energy/pyfusionsolar
2022-10-10 14:17:08,145 - root - INFO - _GridRelay
2022-10-10 14:17:08,145 - root - INFO - Enabled: False
2022-10-10 14:17:08,145 - root - INFO - System name: transformer01
2022-10-10 14:17:08,145 - root - INFO - Interval: 43200
2022-10-10 14:17:08,145 - root - INFO - PVOutput span: 2
2022-10-10 14:17:08,146 - root - INFO - Kenter URL: https://webapi.meetdata.nl
2022-10-10 14:17:08,146 - root - INFO - Kenter EAN: XXX
2022-10-10 14:17:08,146 - root - INFO - Kenter MeterId: XXX
2022-10-10 14:17:08,146 - root - INFO - Kenter User: user
2022-10-10 14:17:08,146 - root - INFO - Kenter Passwd: passwd
2022-10-10 14:17:08,146 - root - INFO - Days back: 3
2022-10-10 14:17:08,147 - root - DEBUG - PvRelay class instantiated
2022-10-10 14:17:08,147 - root - DEBUG - PvFusionSolar class instantiated
2022-10-10 14:17:08,147 - root - DEBUG - PvOutputOrg class instantiated
2022-10-10 14:17:08,147 - root - DEBUG - PvMqtt class instantiated
2022-10-10 14:17:08,147 - root - DEBUG - PvInflux class instantiated
2022-10-10 14:17:08,147 - root - INFO - Starting PvRelay on separate thread
2022-10-10 14:17:08,148 - root - DEBUG - PvRelay waiting 5sec to initialize docker-compose containers
2022-10-10 14:17:13,315 - apscheduler.scheduler - INFO - Adding job tentatively -- it will be properly scheduled when the scheduler starts
/usr/local/lib/python3.10/site-packages/apscheduler/util.py:436: PytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
  return tzinfo.localize(dt)
2022-10-10 14:17:13,320 - apscheduler.scheduler - INFO - Added job "PvRelay.process_fusionsolar_request" to job store "default"
2022-10-10 14:17:13,321 - apscheduler.scheduler - INFO - Scheduler started
2022-10-10 14:17:13,321 - apscheduler.scheduler - DEBUG - Looking for jobs to run
2022-10-10 14:17:13,321 - apscheduler.scheduler - DEBUG - Next wakeup is due at 2022-10-10 15:05:00+02:00 (in 2866.678750 seconds)```
arboeh commented 1 year ago

It works :-D Got it work myself, but the format for "pvfusionminutecron" doesn't work as meantioned in the docu. The format 0,30 gives an error:

2022-10-10 15:20:56,304 - root - DEBUG - Pulled 'pvfusionminutecron=0,30' from the environment
Traceback (most recent call last):
  File "/code/pv.py", line 19, in <module>
    conf = PvConf(logger)
  File "/code/pvconf.py", line 8, in __init__
    self.apply_environment_settings()
  File "/code/pvconf.py", line 141, in apply_environment_settings
    self.fusionminutecron = int(self.getenv("pvfusionminutecron"))
ValueError: invalid literal for int() with base 10: '0,30'

How often is it possible to pull the API?

ghost commented 1 year ago

If you set the minutes in the scheduler the request is made each hour at the minutes set. For example, if you set the minutes to 30, the task will launch at 0:30, 1:30, 2:30, not each half hour.

The correct syntax is: sched.add_job(self.process_fusionsolar_request, trigger='cron', hour=self.conf.fusionhourcron, minute='0, 30') or sched.add_job(self.process_fusionsolar_request, trigger='cron', 'interval', hours=0.5)

JasperE84 commented 1 year ago

Hi Jorge and arboeh, Nice to see you two have been using this project. Its good to see that the effort of sharing the code helps others out.

The cron behaviour of triggering each full half our on the clock is actually intended. Initially I used a 30min timer, but this resulted in problems in displaying the solar production and grid usage data on pvoutput.org. I'll elaborate; pvoutput.org puts data in 15min intervals of the clock (0:15, 0:30, 0:45, 1:00, etc). Because fusionsolar only refreshes each half our, the fusionsolar data and grid usage data must be kept in sync. That's why I use cron to trigger fusionsolar on each half our on the clock, instead of each half our from a random start time.

I'll demonstrate from pvoutput.org, this is what the cron sync achieves: image image

Without it, data looks like this image image