MQTT Binary Sensor and high database usage

Hi all, I have MQTT data coming in for my alarm system which is being used to update various MQTT binary sensors in HA. All is working great except that the MQTT sensors are set to send refreshed data every second. This is absolutely killing my database since HA seems to be writing to the events table (I’m on MariaDB using PhpMyAdmin to see what’s in there) literally every time there’s a MQTT message, even if the state is unchanged. I have seen similar behavior on States table as well. For 30 sensors updating every second… that’s a LOT of rows and it crashes the database after a while.

I have complete control of the code on the alarm system side since I wrote it myself, so the workaround here is to change the code on alarm system end to send topic updates only when state changes. That’s reasonable but the problem with that though is that my sensors clear themselves out whenever I have a HA restart or any other hiccup. I’ve also thought about a middle ground i.e. send updates every 30 seconds.

Would like to know if there is some kind of way to discard new events/states from being written to DB if the message sent matches the existing payload state? So if the sensor is already OFF and the MQTT message isn’t equal to payload_on, then discard the message and don’t write it to DB.

Or any other ideas welcomed… thanks!

Publishing the state messages on change only is the best way.

Publish the messages with the retained flag set. That way the last published message for each topic is retained in the broker.

Whenever home assistant connects to the broker (subscribes to topics), the broker will publish the last known states to home assistant.