Remove statistics of disabled (not deleted!) entities from recorder

Addendum:

Running this (from @vinzcenzo / Statistics - Fix Issue - large amount of data) in your database gives you an overview of affected entities.

    SELECT
    	CASE WHEN entity_id IS NULL
    		THEN 'Orphaned'
    		ELSE 'OK'
    	END Issue,
    	entity_id,
    	statistic_id
    FROM	(
    		SELECT
    			b.entity_id,
    			a.statistic_id				
    		FROM
    			statistics_meta a
    		LEFT JOIN 'states' b
    			ON a.statistic_id = b.entity_id
    		GROUP BY
    			a.statistic_id -- we want to avoid duplicates
    		)
    ORDER BY Issue DESC;

and this gives you the number of affected entities:

SELECT COUNT (*) AS "Orphaned statistic entities"
FROM	(
		SELECT 
			b.entity_id, 
			a.statistic_id				
		FROM 
			statistics_meta a
		LEFT JOIN 'states' b 
			ON a.statistic_id = b.entity_id
		GROUP BY 
			a.statistic_id -- we want to avoid duplicates
		)
WHERE "entity_id" IS NULL;

grafik