bohdan-s / SunGather

GNU General Public License v3.0
167 stars 69 forks source link

mqtt: combine two registers #139

Open pohly opened 1 year ago

pohly commented 1 year ago

I have a SunGrow SG15RT with two strings attached. I would like to see the power generated by each string in HomeAssistant, because a) I am curious :grin: and b) that would allow me to configure the forecast differently for each part of my roof.

I could export mppt_1_voltage/mppt_2_voltage/mppt_1_current/mppt_2_current separately and multiple voltage with current in a HomeAssistant helper to get power, but that seems wasteful. Would it be possible to do the transformation directly in SunGather? Something like this:

  - name: mqtt
    topic: "solar/main"
    ...
    homeassistant: True
    ha_sensors:
      - name: "west"
        sensor_type: sensor
        dev_class: power
        state_class: measurement
        icon: "mdi:solar-panel-large"
        value_template: "{{ value_json.mppt_1_voltage * value_json.mppt_1_current | round(2) }}"

      - name: "east"
        sensor_type: sensor
        dev_class: power
        state_class: measurement
        icon: "mdi:solar-panel"
        value_template: "{{ value_json.mppt_2_voltage * value_json.mppt_2_current | round(2) }}"

I haven't tried it yet (don't want to disturb current measurements) but I don't expect it to work anyway because there is no register(s) field.

pohly commented 1 year ago

I added "register: mppt_1_voltage" and "register: mppt_2_voltage", otherwise loading the config failed.

But I am not sure whether it's working:

2023-10-18 06:20:40 DEBUG    Changing transaction state from 'PROCESSING REPLY' to 'TRANSACTION_COMPLETE'
2023-10-18 06:20:40 DEBUG    Using Inverter Time: 2023-10-18 8:20:38
2023-10-18 06:20:40 DEBUG    start_stop:Start work_state_1:Run
2023-10-18 06:20:40 INFO     Setting Initial Daily registers; daily_export_to_grid, daily_import_from_grid, last_reset
2023-10-18 06:20:40 INFO     Inverter: Successfully scraped in 3.105426 secs
2023-10-18 06:20:40 INFO     InfluxDB: Published
2023-10-18 06:20:40 DEBUG    MQTT: Topic; homeassistant/sensor/A21C090079_west/config, Message: {'state_topic': 'solar/main', 'value_template': '{{ value_json.mppt_1_voltage * value_json.mppt_1_current | round(2) }}', 'icon': 'mdi:solar-panel-large', 'name': 'west', 'state_class': 'measurement', 'unique_id': 'sungather_west_A21C090079', 'unit_of_measurement': 'V', 'device': {'name': 'Sungrow SG15RT', 'manufacturer': 'Sungrow', 'model': 'SG15RT', 'identifiers': 'A21C090079', 'via_device': 'SunGather', 'connections': [['address', 'sungrow-15']]}}
2023-10-18 06:20:40 DEBUG    MQTT: Topic; homeassistant/sensor/A21C090079_east/config, Message: {'state_topic': 'solar/main', 'value_template': '{{ value_json.mppt_2_voltage * value_json.mppt_2_current | round(2) }}', 'icon': 'mdi:solar-panel', 'name': 'east', 'state_class': 'measurement', 'unique_id': 'sungather_east_A21C090079', 'unit_of_measurement': 'V', 'device': {'name': 'Sungrow SG15RT', 'manufacturer': 'Sungrow', 'model': 'SG15RT', 'identifiers': 'A21C090079', 'via_device': 'SunGather', 'connections': [['address', 'sungrow-15']]}}
2023-10-18 06:20:40 INFO     MQTT: Published Home Assistant Discovery messages
2023-10-18 06:20:40 DEBUG    MQTT: Publishing Registers: solar/main : {"model": "SG15RT", "serial_number": "A21C090079", "level": 2, "scan_interval": 10, "use_local_time": false, "smart_meter": false, "connection": "http", "slave": 1, "start_time": "", "host": "sungrow-15", "port": "8082", "timeout": 10, "retries": 3, "RetryOnEmpty": false, "device_type_code": "SG15RT", "run_state": "ON", "last_reset": "2023-10-18 8:20:38", "daily_export_to_grid": 0.0, "daily_import_from_grid": 0.0, "nominal_active_power": 15.0, "output_type": "3P4L", "daily_power_yields": 0.0, "total_power_yields": 394.4, "total_running_time": 154, "internal_temperature": 27.6, "total_apparent_power": 117, "mppt_1_voltage": 482.7, "mppt_1_current": 0.1, "mppt_2_voltage": 428.2, "mppt_2_current": 0.1, "total_dc_power": 117, "phase_a_voltage": 228.1, "phase_b_voltage": 228.8, "phase_c_voltage": 230.0, "phase_a_current": 0.1, "phase_b_current": 0.1, "phase_c_current": 0.1, "total_active_power": 117, "total_reactive_power": -5, "power_factor": 1.0, "grid_frequency": 49.98, "work_state_1": "Run", "nominal_reactive_power": 7.5, "array_insulation_resistance": 3175, "active_power_regulation_setpoint": 15000, "reactive_power_regulation_setpoint": 0, "work_state_2": "Stop", "daily_running_time": 12, "monthly_power_yields": 18.8, "negative_voltage_to_the_ground": -330.1, "bus_voltage": 662.1, "pid_work_state": null, "pid_alarm_code": null, "export_power": 0, "power_meter": 0, "string_1_current": 0.0, "string_2_current": 0.0, "start_stop": "Start", "power_limitation_switch": "Enable", "power_limitation_setting": 100.0, "power_factor_setting": 0.0, "night_svg_switch": "Disable", "reactive_power_adjustment_mode": "Enable Q(P)", "reactive_power_percentage_setting": 0.0, "power_limitation_adjustment": 15.0, "reactive_power_adjustment": 0.0, "pid_recovery": "Disable", "timestamp": "2023-10-18 8:20:38", "export_to_grid": 0, "import_from_grid": 0}
2023-10-18 06:20:40 INFO     MQTT: Registers Published

Note the unexpanded value_template.

$ mosquitto_sub -t 'solar/main/#'
{"model": "SG15RT", "serial_number": "A21C090079", "level": 2, "scan_interval": 10, "use_local_time": false, "smart_meter": false, "connection": "http", "slave": 1, "start_time": "", "host": "sungrow-15", "port": "8082", "timeout": 10, "retries": 3, "RetryOnEmpty": false, "device_type_code": "SG15RT", "run_state": "ON", "last_reset": "2023-10-18 8:20:10", "daily_export_to_grid": 0.0, "daily_import_from_grid": 0.0, "nominal_active_power": 15.0, "output_type": "3P4L", "daily_power_yields": 0.0, "total_power_yields": 394.4, "total_running_time": 154, "internal_temperature": 28.3, "total_apparent_power": 179, "mppt_1_voltage": 487.6, "mppt_1_current": 0.2, "mppt_2_voltage": 429.0, "mppt_2_current": 0.2, "total_dc_power": 182, "phase_a_voltage": 228.8, "phase_b_voltage": 228.0, "phase_c_voltage": 231.2, "phase_a_current": 0.2, "phase_b_current": 0.2, "phase_c_current": 0.2, "total_active_power": 179, "total_reactive_power": -4, "power_factor": 1.0, "grid_frequency": 49.98, "work_state_1": "Run", "nominal_reactive_power": 7.5, "array_insulation_resistance": 3175, "active_power_regulation_setpoint": 15000, "reactive_power_regulation_setpoint": 0, "work_state_2": "Stop", "daily_running_time": 18, "monthly_power_yields": 18.8, "negative_voltage_to_the_ground": -330.0, "bus_voltage": 661.6, "pid_work_state": null, "pid_alarm_code": null, "export_power": 0, "power_meter": 0, "string_1_current": 0.0, "string_2_current": 0.0, "start_stop": "Start", "power_limitation_switch": "Enable", "power_limitation_setting": 100.0, "power_factor_setting": 0.0, "night_svg_switch": "Disable", "reactive_power_adjustment_mode": "Enable Q(P)", "reactive_power_percentage_setting": 0.0, "power_limitation_adjustment": 15.0, "reactive_power_adjustment": 0.0, "pid_recovery": "Disable", "timestamp": "2023-10-18 8:27:20", "export_to_grid": 0, "import_from_grid": 0}
$ mosquitto_sub -t 'homeassistant/sensor/A21C090079_west/#'
{"state_topic": "solar/main", "value_template": "{{ value_json.mppt_1_voltage * value_json.mppt_1_current | round(2) }}", "icon": "mdi:solar-panel-large", "name": "west", "state_class": "measurement", "unique_id": "sungather_west_A21C090079", "unit_of_measurement": "V", "device": {"name": "Sungrow SG15RT", "manufacturer": "Sungrow", "model": "SG15RT", "identifiers": "A21C090079", "via_device": "SunGather", "connections": [["address", "sungrow-15"]]}}

Is homeassistant/sensor what HA uses to detect sensors? Ah, indeed it is - I found "west" in HA as a sensor.

The formular seems to work, too. Only the unit (V) is incorrect. I suppose that's derived from register: mppt_1_voltage. When I use register: total_apparent_power, I get VA. export_power has W, which is what I was hoping to achieve.

This seems to be working now, it just wasn't obvious how to get there :grin: Also note that it publishes more data via MQTT than strictly needed, which in turn then (probably) all needs to be stored by HA. I was expecting that as with InfluxDB the configuration determines which data is published.

Perhaps add a bit more explanations what ha_sensors and it's fields do?