diff options
author | Dennis Eriksen <d@ennis.no> | 2023-01-25 06:51:43 +0100 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2023-01-25 06:51:43 +0100 |
commit | 69dea3d6f06e0c97048ac8ce3eaf4c0f1df4e4f3 (patch) | |
tree | 603a4bf7d4589df56405ab2c658c8bb7e948911e | |
parent | expand to two days, else we do not get all of tomorrow (diff) | |
download | energyscripts-69dea3d6f06e0c97048ac8ce3eaf4c0f1df4e4f3.tar.gz |
add neohub
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | env.sample | 9 | ||||
-rwxr-xr-x | neohub2pgsql.py | 40 | ||||
-rw-r--r-- | requirements.txt | 4 | ||||
-rwxr-xr-x | run.sh | 8 | ||||
-rw-r--r-- | schema.sql | 95 |
6 files changed, 133 insertions, 30 deletions
@@ -2,3 +2,10 @@ This is a small collection of scripts I use to collect information about my energyconsumption. + + + +schema.sql created with: +``` +pg_dump --schema-only --no-owner --no-privileges el +``` @@ -2,8 +2,10 @@ export el_metringPointId=XXX export el_elvia_token=XXX export el_elvia_grid_api_key=XXX -export el_pg_db=XXX -export el_pg_host=XXX +export el_pg_db=el +export el_pg_host=/var/run/postgresql/ +#export el_pg_user=XXX +#export el_pg_pass=XXX export el_tibber_token=XXX @@ -11,3 +13,6 @@ export el_yr_lat=0.0 export el_yr_lon=0.0 export el_entsoe_token=XXX + +export el_neohub_ip=XXX +export el_neohub_port=4242 diff --git a/neohub2pgsql.py b/neohub2pgsql.py new file mode 100755 index 0000000..0c760e5 --- /dev/null +++ b/neohub2pgsql.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3 + +import os +import asyncio +import neohubapi.neohub as neohub + +import psycopg2 + +neohub_ip = os.environ['el_neohub_ip'] +neohub_port = os.environ['el_neohub_port'] +pg_db = os.environ['el_pg_db'] +pg_host = os.environ['el_pg_host'] +pg_user = os.environ['el_pg_user'] +pg_pass = os.environ['el_pg_pass'] +pg_table = "neohub" + +values = [] + +async def run(): + # Legacy connection + hub = neohub.NeoHub(neohub_ip, int(neohub_port)) + # Or, for a websocket connection: + # hub = neohub.Neohub(port=4243, token='xxx-xxxxxxx') + system = await hub.get_system() + hub_data, devices = await hub.get_live_data() + for device in devices['thermostats']: + print(f"Temperature in zone {device.name}: {device}") + values.append((device.time, device.device_id, device.away, device.heat_mode, device.heat_on, device.current_floor_temperature, device.target_temperature, device.temperature)) + +asyncio.run(run()) + +conn = psycopg2.connect(database=pg_db, host=pg_host, user=pg_user, password=pg_pass) +cur = conn.cursor() + +try: + cur.executemany("INSERT INTO " + pg_table + " (time, device_id, away, heat_mode, heat_on, current_floor_temperature, target_temperature, temperature) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)", values) + conn.commit() +except Exception as e: + conn.rollback() + raise e diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7ee1262 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +async-property==0.2.1 +neohubapi==1.0 +psycopg2-binary==2.9.5 +websockets==10.4 @@ -0,0 +1,8 @@ +#!/usr/bin/env zsh +0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}" +0="${${(M)0:#/*}:-$PWD/$0}" + + +source ${0:h}/.env +source ${0:h}/venv/bin/activate +python3 ${0:h}/$1 @@ -2,8 +2,8 @@ -- PostgreSQL database dump -- --- Dumped from database version 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1) --- Dumped by pg_dump version 14.5 (Ubuntu 14.5-0ubuntu0.22.04.1) +-- Dumped from database version 14.6 (Ubuntu 14.6-0ubuntu0.22.04.1) +-- Dumped by pg_dump version 14.6 (Ubuntu 14.6-0ubuntu0.22.04.1) SET statement_timeout = 0; SET lock_timeout = 0; @@ -21,7 +21,7 @@ SET default_tablespace = ''; SET default_table_access_method = heap; -- --- Name: elvia; Type: TABLE; Schema: public; Owner: dennis +-- Name: elvia; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.elvia ( @@ -35,10 +35,8 @@ CREATE TABLE public.elvia ( ); -ALTER TABLE public.elvia OWNER TO dennis; - -- --- Name: elvia_gridtariff; Type: TABLE; Schema: public; Owner: dennis +-- Name: elvia_gridtariff; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.elvia_gridtariff ( @@ -53,10 +51,8 @@ CREATE TABLE public.elvia_gridtariff ( ); -ALTER TABLE public.elvia_gridtariff OWNER TO dennis; - -- --- Name: entsoe; Type: TABLE; Schema: public; Owner: dennis +-- Name: entsoe; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.entsoe ( @@ -69,10 +65,8 @@ CREATE TABLE public.entsoe ( ); -ALTER TABLE public.entsoe OWNER TO dennis; - -- --- Name: nbex; Type: TABLE; Schema: public; Owner: dennis +-- Name: nbex; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.nbex ( @@ -84,10 +78,46 @@ CREATE TABLE public.nbex ( ); -ALTER TABLE public.nbex OWNER TO dennis; +-- +-- Name: neohub; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.neohub ( + id integer NOT NULL, + "timestamp" timestamp with time zone DEFAULT now() NOT NULL, + "time" time without time zone, + device_id smallint, + away boolean, + heat_mode boolean, + heat_on boolean NOT NULL, + current_floor_temperature real, + target_temperature real, + temperature real +); + -- --- Name: tibber_consumption; Type: TABLE; Schema: public; Owner: dennis +-- Name: neohub_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.neohub_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: neohub_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.neohub_id_seq OWNED BY public.neohub.id; + + +-- +-- Name: tibber_consumption; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.tibber_consumption ( @@ -102,10 +132,8 @@ CREATE TABLE public.tibber_consumption ( ); -ALTER TABLE public.tibber_consumption OWNER TO dennis; - -- --- Name: tibber_prices; Type: TABLE; Schema: public; Owner: dennis +-- Name: tibber_prices; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.tibber_prices ( @@ -118,10 +146,8 @@ CREATE TABLE public.tibber_prices ( ); -ALTER TABLE public.tibber_prices OWNER TO dennis; - -- --- Name: yr; Type: TABLE; Schema: public; Owner: dennis +-- Name: yr; Type: TABLE; Schema: public; Owner: - -- CREATE TABLE public.yr ( @@ -137,10 +163,15 @@ CREATE TABLE public.yr ( ); -ALTER TABLE public.yr OWNER TO dennis; +-- +-- Name: neohub id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.neohub ALTER COLUMN id SET DEFAULT nextval('public.neohub_id_seq'::regclass); + -- --- Name: elvia el_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: elvia el_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.elvia @@ -148,7 +179,7 @@ ALTER TABLE ONLY public.elvia -- --- Name: elvia_gridtariff elvia_gridtariff_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: elvia_gridtariff elvia_gridtariff_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.elvia_gridtariff @@ -156,7 +187,7 @@ ALTER TABLE ONLY public.elvia_gridtariff -- --- Name: entsoe entsoe_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: entsoe entsoe_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.entsoe @@ -164,7 +195,7 @@ ALTER TABLE ONLY public.entsoe -- --- Name: nbex nbexchange_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: nbex nbexchange_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.nbex @@ -172,7 +203,15 @@ ALTER TABLE ONLY public.nbex -- --- Name: tibber_consumption tibber_consumption_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: neohub neohub_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.neohub + ADD CONSTRAINT neohub_pkey PRIMARY KEY (id); + + +-- +-- Name: tibber_consumption tibber_consumption_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.tibber_consumption @@ -180,7 +219,7 @@ ALTER TABLE ONLY public.tibber_consumption -- --- Name: tibber_prices tibber_prices_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: tibber_prices tibber_prices_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.tibber_prices @@ -188,7 +227,7 @@ ALTER TABLE ONLY public.tibber_prices -- --- Name: yr yr_pkey; Type: CONSTRAINT; Schema: public; Owner: dennis +-- Name: yr yr_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.yr |