diff --git a/server/mqtt.go b/server/mqtt.go index 451f18d26..7c399e968 100644 --- a/server/mqtt.go +++ b/server/mqtt.go @@ -14,6 +14,7 @@ import ( "github.com/evcc-io/evcc/core/vehicle" "github.com/evcc-io/evcc/plugin/mqtt" "github.com/evcc-io/evcc/util" + "github.com/samber/lo" ) // MQTT is the MQTT server. It uses the MQTT client for publishing. @@ -53,7 +54,7 @@ func NewMQTT(root string, site site.API) (*MQTT, error) { func (m *MQTT) encode(v any) string { // nil should erase the value - if v == nil { + if v == nil || lo.IsNil(v) { return "" } diff --git a/server/mqtt_test.go b/server/mqtt_test.go index 8b35f713d..30b886ac6 100644 --- a/server/mqtt_test.go +++ b/server/mqtt_test.go @@ -97,6 +97,13 @@ func (suite *mqttSuite) TestSlice() { suite.Equal([]string{"2", "10", "20"}, suite.payloads, "payloads") } +func (suite *mqttSuite) TestNilInterface() { + var ptr *time.Time + suite.publish("test", false, ptr) + suite.Equal([]string{"test"}, suite.topics, "topics") + suite.Equal([]string{""}, suite.payloads, "payloads") +} + func (suite *mqttSuite) TestMeasurement() { topics := lo.Map([]string{ "title", "icon", "power", "energy", "powers", "currents", "excessDCPower", "capacity", "soc", "controllable", "forecast",