Motivation:
Thanks to AVMs change in latest Fritz!Box devices firmware (7.50 release), the behavior for data provided using the AVM FRITZ!Box Tools - Home Assistant integration - precisely: sensor.fritzbox_gigabytes_received
and sensor.fritzbox_gigabytes_sent
changed:
Thanks to that change and a quite unreliable behavior of that input sensors, utility meters (new sensors) based on those two input sensors are completely screwed.
Problem:
- If input sensors data is set to
0.0
for a second/few seconds and switches back to the former value of e. g.1000
, that rise of 1000 is added to all utility meter sensor. - Affects per utility meter sensor
state
column instates
table as well asstate
andsum
tables instatistics_short_term
andstatistics
table. - In the end, it renders all those sensors data (LTS - long-term statistics) completely useless.
- Cleaning up all that mess takes me AT LEAST 3 to 4 hours PER INCIDENT - which could happen once a day
During the last days I spent roughly 17 hours in total already on this issue. I am totally frustrated, especially as this could happen (and is happening at the moment) every day.
Possible solutions / workarounds:
-
The manufacturer (AVM)
"...assumes no liability or warranty for any of the functions mentioned or for the correctness of the associated documentation. AVM reserves the right to change or omit functions in whole or in part at any time and without prior notice."
I need to block the source (Fritz! integration) to write0.0
to thestates
table for two entities (sensor.fritzbox_gigabytes_received
andsensor.fritzbox_gigabytes_sent
). -
I was thinking of āmaybe can the integration do something hereā as all information exists. Unfortunately, no response yet. - Update: see:
-
So what can affected users do on their own?
For this I thought of using a SQL TRIGGER (I use the default HA database - SQLite 3.40.1 with schema_version 30 on HA Core 2022.12) to block these write atttempts.
What I currently have
DELIMITER $$
CREATE TRIGGER fritzbox_gigabytes_no_zero
BEFORE INSERT ON "states"
FOR EACH ROW
BEGIN
IF (NEW.entity_id = "sensor.fritzbox_gigabytes_received" OR NEW.entity_id = "sensor.fritzbox_gigabytes_sent")
IF NEW.state = 0.0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Updates with value 0.0 are not allowed in this case - WORKAROUND for BUG 1.31';
END IF;
END IF;
END;
$$
DELIMITER ;
Questions
- General: Can SQL TRIGGERS be used safely in the HA-DB (SQLite)?
- General: Are they persistent there? Or are they at risk of being changed or deleted during HA Core updates (DB schema upgrades)?
- Will it have a notable impact on performance (or is the engine applying the filters efficiently so all other INSERTs are not getting slowed down)?
- Specifically in regards to the current trigger definition above:
Does the specifically considered one work (syntactically)? - In case you have a completely other approach/idea (not taking care of the data is not an option), please let me know as well.
- General: where does that error message text pop up? Will it be logged in the HA log (so I could discover and count āJep, I hit the issue and successfully avoided it againā?
For now, I feel like (and hope) the SQL TRIGGER could be a smart āsolutionā for this. At least for buying time. Time urgently needed currently
Regarding 4: I would test this as good as possible, likely by adding the trigger with trigger type UPDATE, use one row for testing purposes and try to write 0.0
to it. As this is a ātry & errorā I would be very thankful for experts having a look at this.