solmoller / eversolar-monitor

Script to capture data and create statistics from Eversolar/zeversolar Solar Inverters. Includes easy install image files for Raspberry Pi. Working edition since 2012 :-)
https://github.com/solmoller/eversolar-monitor/blob/wiki/Introduction.md
MIT License
30 stars 20 forks source link

MQTT ha discovery is incomplete with multiple inverters due to nonunique ids #63

Closed LeighAS closed 4 months ago

LeighAS commented 1 year ago

image as seen in mqtt explorer, the second inverter has been given the unique ID of the first one this is most likely the reason that the home assistant instance can not auto discover the second inverter as a separate device.

I am raising this as i'm not a perl or mqtt pro and would rather not make erroneous changes to the perl script. I will have a go if there is no help forthcoming. I hope I am being of some use here.

LeighAS commented 1 year ago

image finally fixed it. basically its seems its a problem of scope.

my $mqtt_serial = $inverters{$inverter}{'serial'}; is being declared and set outside of the sub ha_disc_config() which when I look at mqtt explorer is only setting one of the inverter serials for all of the mqtt messages sent with auto discovery. the non HA auto discovery messages are however correct so to fix for my rpi instance I brought the initialising the variable inside to a local one image

This was cause I didn't want to mess about which the non home assistant auto discovery serial which appeared to be coming out ok.

I wouldn't say this is perfect as a fix but certainly now home assistant now sees both devices and will probably auto find the 3rd inverter when I fix it and add it in.

open to suggestions if people think there is a better solution?

nagydavid commented 1 year ago

Can you please submit your changes as a PR, and I can review it and see if it needs any adjustment

LeighAS commented 1 year ago

sent PR. sorry it took me a while.

solmoller commented 4 months ago

I can confirm that this change works, here is the modified code: sub ha_disc_config { my $mqtt_serial_HA = $inverters{$inverter}{'serial'}; my %config_data = ( device => { identifiers => [ $mqtt_serial_HA, ], manufacturer => "Eversolar", model => $mqtt_inverter_model, name => "Solar Inverter" }, state_topic => "$mqtt_topic_prefix/$mqtt_serial_HA/$_[0]", unique_id => "$mqtt_serial_HA\_$_[0]", state_class => "measurement", );

I now finally have both inverters in HA

solmoller commented 4 months ago

Added to code