Code Monkey home page Code Monkey logo

Comments (3)

dekiesel avatar dekiesel commented on July 23, 2024

The drawback with this method is that you always need to unpack attributes for every row and I am afraid this might make my dashboard sluggish in the feature so I wanted to put the hourly sensors into their own table so I could use an index when computing daily,weekly,monthly,yearly averages.

Create a new table eloverblik:

CREATE TABLE eloverblik(
   sensor varchar(100),    
   metering_date date,  
   value numeric,
   insource_date timestamp without time zone,	
   PRIMARY KEY (metering_date, sensor)
);

Optional: Add the values you have saved so far:


with CTE as ( 
	select *
	,row_number() over (partition by entity_id, state, metering_date order by time desc) as rn
	from (
			select time,
					entity_id,
				   cast(state as decimal)
					,to_date(replace(cast(json_extract_path(attributes::json,'metering_date') as text),'"',''),'YYYY-MM-DD') as metering_date
					
			from public.ltss
			where entity_id like 'sensor.eloverblik_%'

	) a
)

insert into eloverblik
select entity_id as sensor,
		metering_date,
		state as value,
		time as insource_date

from cte where rn = 1 and entity_id != 'sensor.eloverblik_energy_total'

I am excludingtotal because I don't want to mix the types of sensors (hourly/total) and because I can compute the daily total from the hourly sensors.

To automatically update eloverblik create this function and trigger:

CREATE OR REPLACE FUNCTION eloverblik_insert()
  RETURNS TRIGGER 
  LANGUAGE PLPGSQL
  AS
$$
BEGIN
	if NEW.entity_id like 'sensor.eloverblik_energy_%' 
	and NEW.entity_id <> 'sensor.eloverblik_energy_total'
	THEN
		 INSERT INTO eloverblik(sensor, metering_date, value, insource_date)
		 VALUES(NEW.entity_id,to_date(replace(cast(json_extract_path(NEW.attributes::json,'metering_date') as text),'"',''),'YYYY-MM-DD'),cast(NEW.state as decimal),now())
		ON CONFLICT (metering_date,sensor) DO UPDATE SET   (value, insource_date) = (cast(NEW.state as decimal),now());
	END IF;

	RETURN NEW;
END;
$$

CREATE TRIGGER eloverblik_insert
  AFTER INSERT
  ON ltss
  FOR EACH ROW
  EXECUTE PROCEDURE eloverblik_insert();

Let me know if you'd rather have this in the wiki.

from homeassistant-eloverblik.

JonasPed1 avatar JonasPed1 commented on July 23, 2024

it should not be in the issue tracker. You are welcome to create a PR to include the information in some documentation.

from homeassistant-eloverblik.

JonasPed1 avatar JonasPed1 commented on July 23, 2024

Closing the issue as no PR have been received and this really do not belong in the issue tracker.

from homeassistant-eloverblik.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.