foogod / go-powerwall

A Go library for communicating with Tesla Powerwall appliances via the local-network API
MIT License
6 stars 7 forks source link

Error while unmarshaling grid_faults.decoded_alert #4

Open Cazzar opened 2 years ago

Cazzar commented 2 years ago

While using powerwall_exporter I noticed that from my powerwall I am seeing the decoded_alert is showing a string of json data instead of directly nesting JSON data:

example JSON (some details redacted for privacy:

{
    "command_source": "Configuration",
    "battery_target_power": 0,
    "battery_target_reactive_power": 0,
    "nominal_full_pack_energy": 0,
    "nominal_energy_remaining": 0,
    "max_power_energy_remaining": 0,
    "max_power_energy_to_be_charged": 0,
    "max_charge_power": 0,
    "max_discharge_power": 0,
    "max_apparent_power": 0,
    "instantaneous_max_discharge_power": 0,
    "instantaneous_max_charge_power": 0,
    "instantaneous_max_apparent_power": 0,
    "grid_services_power": -0,
    "system_island_state": "SystemGridConnected",
    "available_blocks": 0,
    "battery_blocks": [
        {
            "Type": "",
            "PackagePartNumber": "3012170-05-C",
            "PackageSerialNumber": "gateway-serial",
            "disabled_reasons": [
                "DisabledBatteryBreakerOpen"
            ],
            "pinv_state": "",
            "pinv_grid_state": "",
            "nominal_energy_remaining": 3920,
            "nominal_full_pack_energy": 14807,
            "p_out": null,
            "q_out": null,
            "v_out": null,
            "f_out": null,
            "i_out": null,
            "energy_charged": null,
            "energy_discharged": null,
            "off_grid": false,
            "vf_mode": false,
            "wobble_detected": false,
            "charge_power_clamped": false,
            "backup_ready": false,
            "OpSeqState": "Standby",
            "version": "a54352e6706386"
        }
    ],
    "ffr_power_availability_high": 0,
    "ffr_power_availability_low": 0,
    "load_charge_constraint": 0,
    "max_sustained_ramp_rate": 1250000,
    "grid_faults": [
        {
            "timestamp": 1654832966470,
            "alert_name": "PINV_a008_vfCheckRocof",
            "alert_is_fault": false,
            "decoded_alert": "[{\"name\":\"PINV_alertID\",\"value\":\"PINV_a008_vfCheckRocof\"},{\"name\":\"PINV_alertType\",\"value\":\"Warning\"}]",
            "alert_raw": 576460752303423488,
            "git_hash": "a54352e6706386",
            "site_uid": "my-site-uid",
            "ecu_type": "TEPINV",
            "ecu_package_part_number": "1081100-33-V",
            "ecu_package_serial_number": "my-serial"
        },
        {
            "timestamp": 1654832966327,
            "alert_name": "PINV_a004_vfCheckUnderVoltage",
            "alert_is_fault": false,
            "decoded_alert": "[{\"name\":\"PINV_alertID\",\"value\":\"PINV_a004_vfCheckUnderVoltage\"},{\"name\":\"PINV_alertType\",\"value\":\"Warning\"},{\"name\":\"PINV_a004_uv_amplitude\",\"value\":32,\"units\":\"Vrms\"}]",
            "alert_raw": 288265560523800576,
            "git_hash": "a54352e6706386",
            "site_uid": "my-site-uid",
            "ecu_type": "TEPINV",
            "ecu_package_part_number": "1081100-33-V",
            "ecu_package_serial_number": "my-serial"
        },
        {
            "timestamp": 1654832328491,
            "alert_name": "PINV_a004_vfCheckUnderVoltage",
            "alert_is_fault": false,
            "decoded_alert": "[{\"name\":\"PINV_alertID\",\"value\":\"PINV_a004_vfCheckUnderVoltage\"},{\"name\":\"PINV_alertType\",\"value\":\"Warning\"},{\"name\":\"PINV_a004_uv_amplitude\",\"value\":11,\"units\":\"Vrms\"}]",
            "alert_raw": 288242470779617280,
            "git_hash": "a54352e6706386",
            "site_uid": "my-site-uid",
            "ecu_type": "TEPINV",
            "ecu_package_part_number": "1081100-33-V",
            "ecu_package_serial_number": "my-serial"
        },
        {
            "timestamp": 1654832328391,
            "alert_name": "PINV_a008_vfCheckRocof",
            "alert_is_fault": false,
            "decoded_alert": "[{\"name\":\"PINV_alertID\",\"value\":\"PINV_a008_vfCheckRocof\"},{\"name\":\"PINV_alertType\",\"value\":\"Warning\"}]",
            "alert_raw": 576460752303423488,
            "git_hash": "a54352e6706386",
            "site_uid": "my-site-uid",
            "ecu_type": "TEPINV",
            "ecu_package_part_number": "1081100-33-V",
            "ecu_package_serial_number": "my-serial"
        }
    ],
    "can_reboot": "Yes",
    "smart_inv_delta_p": 0,
    "smart_inv_delta_q": 0,
    "last_toggle_timestamp": "2022-06-10T13:46:18.208909457+10:00",
    "solar_real_power_limit": -1,
    "score": 10000,
    "blocks_controlled": 1,
    "primary": true,
    "auxiliary_load": 0,
    "inverter_nominal_usable_power": 0,
    "expected_energy_remaining": 0
}

error message from powerwall_exporter

{"err":"json: cannot unmarshal number into Go struct field GridFaultData.grid_faults.decoded_alert of type string","level":"error","msg":"Error fetching system_status info","time":"2022-06-14T02:17:32+10:00"}
Cazzar commented 2 years ago

From some further digging, and debugging, I found that the issue is due to the alert for example:

{
    "name": "PINV_a004_uv_amplitude",
    "value": 11,
    "units": "Vrms"
}

As it seems that value can contain int values, updating https://github.com/foogod/go-powerwall/blob/76b5c92c9b515125d0870d3df42a3707838e5483/types.go#L64 to

 Value interface{} `json:"value"` 

will make it parse correctly in my case, though the other issues have not been investigated.

Though given that this change on it's own will then drop the units value, another option might be best looked into.

grawlinson commented 1 year ago

Any chance of getting the current issues/PRs reviewed and merged?

andig commented 1 year ago

I‘ve done a fork- feel free to open PRs there.

grawlinson commented 1 year ago

Oof, that's a shame but thanks for stepping up.