I think i can answer my own question, as it looks my problem is by design. I have made a test setup with 2 motion sensors, one of them connected to ZHA and second to Z2M.
Each time movement is recognized and cleared (one on and off action) uses 4 database entries for Z2M and only 2 for ZHA. Here are 2 screenshots:
This happens because of the attributes, since there is a change in the attributes the db links to the old and new value. For my sensor the stored attributes look like this:
{"battery":100,"detection_interval":5,"device_temperature":26,"illuminance":54,"linkquality":54,"motion_sensitivity":"high","occupancy":true,"temperature":25,"trigger_indicator":true,"update":{"state":null},"update_available":null,"voltage":3025,"device_class":"motion","friendly_name":"Presence Arbeitszimmer"}
{"battery":100,"detection_interval":5,"device_temperature":26,"illuminance":54,"linkquality":54,"motion_sensitivity":"high","occupancy":false,"temperature":25,"trigger_indicator":true,"update":{"state":null},"update_available":null,"voltage":3025,"device_class":"motion","friendly_name":"Presence Arbeitszimmer"}
So because of the change of occupancy in the attributes there is written a new line in the attributes table and a new line in the states table linking to the new attributes_id.
But if that’s not enough, since every sensor in Z2M has all the attributes of the entity (yes, the battery sensor of this entity also has the occupancy attribute), every time motion is detected the db writes lines for the illuminace, battery and temperature sensor (this is an Aqara P1 Sensor which has 4 entities in Z2M), even nothing of them has changed. So i can be happy my DB size only doubles
ZHA only writes one line per change and only for the binary_sensor, the battery sensor for example is not written since the battery not changed.
If something i wrote is bullshit please correct me, but this is how it looks for me atm.
@NODeeJay, this does not explain why your database is growing so much. Your tradfri gateway sensor has this many entries because of the linked attributes. You should look in the attribute table what changes, every line stands for the sensor with a change in the attributes.
A last question, is it possible to have the entities without the / some attributes? I have autodiscovery enabled so once connected to Z2M the sensors automatically appear but with all the attributes in every sensor…
EDIT: Setting advanced options in Z2M disables the attributes in each sensor, so my problem should be solved. I will report in a week when i see it works or not …
legacy_entity_attributes: false
From the docs from Z2M