StephanJoubert / home_assistant_solarman

Home Assistant component for Solarman collectors used with a variety of inverters.
Apache License 2.0
507 stars 191 forks source link

Set time after inverter start to fix total increase when blocking cloud access #567

Open Panoramiac opened 2 months ago

Panoramiac commented 2 months ago

The total yield is not reset when blocking the cloud (and we all should block these inverters from reaching the cloud as this is not secure): image

I found an interesting hint here:

Note that with internet access blocked, the inverter never receives any time information. This breaks the Yield today counter as it will never properly reset unless you manually set the time on each boot using modbus register 22, 23 and 24.

The question now is how to set these registers and if this could be integrated into this addon?

pretorh commented 1 month ago

I had a similar problem where the inverter's time drifted after a few weeks, causing the daily counters from the inverter to only reset at ex 12:02am

My workaround is to use an automation that runs every 6 hours, which sets the time on the inverter using home assistant's time, using python scripts

This works on my deye hybrid, may need to adjust for other inverters.

Python:

def write_values(register, value):
    service_data = {
        "register": register,
        "values": value,
    }
    hass.services.call("solarman", "write_multiple_holding_registers", service_data, blocking=True)

def int16(high, low):
    return (high << 8) + low

now = datetime.datetime.now()
values = [
    now.year - 2000,
    now.month,
    now.day,
    now.hour,
    now.minute,
    now.second,
]
i16 = [
    int16(values[0], values[1]),
    int16(values[2], values[3]),
    int16(values[4], values[5]),
]

write_values(22, i16[0])
write_values(23, i16[1])
write_values(24, i16[2])

automation:

alias: "cron: set inverter time"
description: ""
trigger:
  - platform: time_pattern
    hours: /6
    minutes: "58"
condition: []
action:
  - service: python_script.update_solarman_datetime
    metadata: {}
    data: {}
mode: single

the Python code was reversed from https://github.com/kellerza/sunsynk/blob/main/src/sunsynk/rwsensors.py#L142