#Fixing negative PV history data

1 messages · Page 1 of 1 (latest)

void leaf
#

So I want to fix data on my solar production energy panel. Everyday it starts negative and its all because the sensor was defined as "total" instead of "total_increasing". i was not aware and so i have months of data like that. i wrote this script but I'm afraid to ruin everything

#

```CREATE DEFINER=homeassistant@% PROCEDURE fix_deye(IN metadata INT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE curr_id INT;
DECLARE prev_value double;
DECLARE curr_value double;
DECLARE sum_value double;
DECLARE cur1 CURSOR FOR SELECT id, state FROM statistics where metadata_id = metadata;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur1;
FETCH cur1 INTO curr_id, curr_value;

update statistics set sum = curr_value where id = curr_id;

set sum_value = curr_value;
set prev_value = curr_value;

FETCH cur1 INTO curr_id, curr_value;

read_loop: LOOP

IF done THEN
  LEAVE read_loop;
END IF;

IF curr_value - prev_value >= 0 THEN
    set sum_value = sum_value + ( curr_value - prev_value );
ELSE
    set sum_value = sum_value + curr_value;
END IF;

update statistics set sum = sum_value where id = curr_id;

set prev_value = curr_value;

FETCH cur1 INTO curr_id, curr_value;

END LOOP;

CLOSE cur1;
END```