Notification when database gets corrupt

Hey guys,

Is there a way to get a notification when de database gets corrupted.
I had a power outtage a few days ago, and the server rebooted after the power got back, no big deal.
But I checked the energy consumption today and I saw that I lost 1,5 year of energy data consumption.
Im a sucker for that data so I my heart screamed like a little girl.

Luckily I make backups every night, but now I lost 5 days worth of data.

Is it possible to get a notification at least? Now HA just renames the db as CORRUPT and starts a new db…
Would really appreciate a notification or some sort.

Thanks!

2 Likes

That would be a feature request (that I’d happily +1)

1 Like

Or an ability to set the DB to self prune.

My Database got to over 4GB when it got corrupted. And triggered a daily outage. I don’t need/use history so I deleted it and everything is fine now… But it Would be nice if would could set a max DB size and have the OS prune old entries.

It does self-prune, but by time rather than by size.
If you don’t care, just set “purge_keep_days” to 1. You’ll still have long-term statistics (aggregated data), which use basically no space.

My db just got corrupt 2 days ago again.
I switched to mariadb and will start clean.

I have no clue why my sqlite db gets corrupted the whole time.

Hope mariadb woning have these problems.

HA has stated that they prefer users to use the default sqlite over mariadb at the moment. Myself I had a corrupt message today as well. I managed to recover the corrupt db file and reuse it so I only lost todays information instead of all previous history.

I’ve created a feature request:

I converted back to sqlite since.
But I just had a recent db corrupt again, last week.
Made a new topic for that.

Because HA team preferred sqlite over mariadb I decided a few months ago to go back.

I just set up the following automation. Just manually tested by triggering a fake event. Did not test with real database corruption. Fingers crossed that my DB is never corrupt again. But if so, this may help.

You need to enable the following: System Log - Home Assistant

alias: Notify when database is corrupt
description: ""
triggers:
  - trigger: event
    event_type: system_log_event
    event_data:
      level: ERROR
conditions:
  - condition: template
    value_template: "{{ 'corrupt database' in trigger.event.data.message|lower() }}"
actions:
  - action: notify.mobile_app_pixel_6
    metadata: {}
    data:
      title: Database is corrupted
      message: "{{ trigger.event.data.message[0] }}"
      data:
        persistent: true
        channel: Alarm
        importance: high
        notification_icon: mdi:alert-octagram
1 Like

Thanks - I had the same problem, didn’t know my db was corrupted and lost several days of data. Have you (or anyone following) verified that this automation works?