diff --git a/lib/controller.js b/lib/controller.js index d5e527bb..f1906eca 100644 --- a/lib/controller.js +++ b/lib/controller.js @@ -249,10 +249,12 @@ class Controller { messagePayload.device = this.getDeviceInfoForMqtt(entityID); } - if (settings.get().experimental.output === 'json') { - this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options); - } else if (settings.get().experimental.output === 'attribute') { - this.iteratePayloadForAttrOutput(entity.friendlyName+'/', messagePayload, options); + if (Object.entries(messagePayload).length) { + if (settings.get().experimental.output === 'json') { + this.mqtt.publish(entity.friendlyName, JSON.stringify(messagePayload), options); + } else if (settings.get().experimental.output === 'attribute') { + this.iteratePayloadForAttrOutput(entity.friendlyName+'/', messagePayload, options); + } } } diff --git a/test/controller.test.js b/test/controller.test.js index 7048788d..4f792c82 100644 --- a/test/controller.test.js +++ b/test/controller.test.js @@ -162,5 +162,22 @@ describe('Controller', () => { expect(JSON.parse(mqttPublish.mock.calls[1][1])).toStrictEqual({humidity: 2}); expect(JSON.parse(mqttPublish.mock.calls[2][1])).toStrictEqual({temperature: 3}); }); + + it('Should not send empty messages', () => { + jest.spyOn(settings, 'get').mockReturnValue({ + mqtt: { + include_device_information: false, + }, + advanced: { + cache_state: true, + }, + experimental: { + output: 'json', + }, + }); + + controller.publishEntityState('0x12345678', {}); + expect(mqttPublish).toHaveBeenCalledTimes(0); + }); }); });