Entity history not shown correctly

Hopefully the images are clear; I know for a fact that this light turns on and off very often
In fact you can see it was turned off a few minutes ago
image

However, in the history plot it shows as if it has always been off
image

A few weeks ago, “history” wasn’t showing anything, so I restarted HA and it started working again, but now here I am with a similar problem

Are you using the Home Assistant Shelly integration?

If so, take particular note of this:

Shelly devices use the CoIoT protocol to communicate with integration. For Shelly firmware 1.10.0 or newer, CoIoT must be enabled in the device settings. Navigate to the local IP address of your Shelly device, Internet & Security >> ADVANCED - DEVELOPER SETTINGS and check the box Enable CoIoT.

And this:

We recommend using unicast for communication. To enable this, enter the local IP address of the Home Assistant server and port 5683 into the CoIoT peer field and push SAVE button. This is mandatory for Shelly Motion with firmware 1.1.0 or newer. After changing the CoIoT peer, the Shelly device needs to be manually restarted.

https://www.home-assistant.io/integrations/shelly/#shelly-device-configuration-generation-1

Also make sure your Shelly device firmware is up to date.

I’m using Shellies with MQTT, so it’s not that

Any other entity just doesn’t have any history

That’s a different kettle of fish.

If all entities have no history you database is likely corrupt.

Check Configuration / Logs for database errors.

If your database is corrupt, delete config/home-assistant_v2.db and restart.

You will lose any saved history.

That file was only 500kB; I’ve deleted it and restarted
It works now, but how long will it last?

That depends on how stable your system is. Mine hasn’t had an issue for three years (though I am using the MariaDB addon).

I’m also using the MariaDB add-on;
I’ve rebuilt the system multiple times trying to get rid of unwanted restarts, but without a log that is persistent on restarts it’s almost impossible to do
I’m down to a random restart every 2-3 weeks now

There is now a persistent log for before the last restart. It has 1 appended to it. You can view it with a text editor.