From 4a716859b094a7457767301d83ff79a28e5aeac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sat, 17 Aug 2024 23:44:58 +0200 Subject: [PATCH 01/10] combine scripts --- Dockerfile | 2 +- get_price.py | 35 ----------------------- pulse.py | 80 ++++++++++++++++++++++++++++++++++------------------ 3 files changed, 54 insertions(+), 63 deletions(-) delete mode 100644 get_price.py diff --git a/Dockerfile b/Dockerfile index 54c0e01..bc5e0c1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.12-bookworm +FROM python:3.12-slim-bookworm # Labels LABEL MAINTAINER="Jan-Ole Hübner " diff --git a/get_price.py b/get_price.py deleted file mode 100644 index 56d9aca..0000000 --- a/get_price.py +++ /dev/null @@ -1,35 +0,0 @@ -import os -import sys -import tibber -from influxdb_client import InfluxDBClient -from influxdb_client.client.write_api import SYNCHRONOUS -from DataPoints import Price -import logging - -TOKEN = os.getenv('TOKEN', '') -TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') -URL = os.getenv('URL', "") -BUCKET = os.getenv('BUCKET', "tibber") -ORG = os.getenv('ORG', "Default") - -# Logging configuration -logger = logging.getLogger("TibberInflux") -formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s') -ch = logging.StreamHandler(sys.stdout) -ch.setFormatter(formatter) -logger.addHandler(ch) -logger.setLevel(logging.INFO) - -__version__ = "v0.3.0" -logger.info(__version__) - -client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) -write_api = client.write_api(write_options=SYNCHRONOUS) -query_api = client.query_api() - -account = tibber.Account(TIBBERTOKEN) -home = account.homes[0] -p = Price(home.current_subscription.price_info.current).get_datapoint() -write_api.write(record=p, bucket=BUCKET) -logger.info(vars(p)) \ No newline at end of file diff --git a/pulse.py b/pulse.py index 338a4a1..050444d 100644 --- a/pulse.py +++ b/pulse.py @@ -2,13 +2,12 @@ import asyncio import os import signal import sys -import time +import logging import tibber from influxdb_client import InfluxDBClient from influxdb_client.client.write_api import SYNCHRONOUS -from DataPoints import Pulse -import logging +from DataPoints import Pulse, Price TOKEN = os.getenv('TOKEN', '') TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') @@ -18,25 +17,24 @@ ORG = os.getenv('ORG', "Default") # Logging configuration logger = logging.getLogger("TibberInflux") -formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') ch = logging.StreamHandler(sys.stdout) ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.3.4" +__version__ = "v0.4.0" logger.info(__version__) - -logger.info("connecting to db...") +# Initialize InfluxDB client client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) write_api = client.write_api(write_options=SYNCHRONOUS) query_api = client.query_api() -logger.info("connecting to tibber...") + +# Initialize Tibber account account = tibber.Account(TIBBERTOKEN) home = account.homes[0] -logger.info("starting subscription...") + def _incoming(data): try: @@ -47,32 +45,60 @@ def _incoming(data): logger.exception("Error in _incoming():") raise e + def timeout_handler(signum, frame): raise TypeError("Timeout occurred") -@home.event("live_measurement") + async def show_current_power(data): + signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(8) _incoming(data) -def stop(home): - return False -try: +async def periodic_task(): + while True: + try: + # Fetch price info and write to InfluxDB + p = Price(home.current_subscription.price_info.current).get_datapoint() + write_api.write(record=p, bucket=BUCKET) + logger.info(vars(p)) + except Exception as e: + logger.exception("Error in periodic_task():") + # Wait for the next run (e.g., every 60 seconds) + await asyncio.sleep(3) + + +async def main(): + loop = asyncio.get_event_loop() signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(15) - home.start_live_feed(user_agent="pulse.py/0.3.4",exit_condition=stop(home),retries=2,retry_interval=3.0) -except TypeError: - logger.exception("Timeout occurred while executing start_live_feed()") - client.close() - exit(1) -except Exception: - logger.exception("Error in start_live_feed()") - client.close() - exit(41) - - - - + + try: + # Start the live feed in a separate task + home_feed_task = loop.create_task(home.start_live_feed( + user_agent="pulse.py/0.4.0", + exit_condition=lambda: False, + retries=2, + retry_interval=3.0, + callback=show_current_power + )) + + # Start the periodic task + periodic_task_task = loop.create_task(periodic_task()) + + # Wait for both tasks to complete + await asyncio.gather(home_feed_task, periodic_task_task) + + except TypeError: + logger.exception("Timeout occurred while executing start_live_feed()") + client.close() + sys.exit(1) + except Exception: + logger.exception("Error in start_live_feed()") + client.close() + sys.exit(41) +if __name__ == "__main__": + asyncio.run(main()) From fdad6c3d7261badc8c27d2a49d3c10308e36a28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:06:38 +0200 Subject: [PATCH 02/10] x vcbxfdfsfgrfWFEFF --- Dockerfile | 2 +- delay_price.sh | 6 ------ start.sh | 11 +++++++++++ 3 files changed, 12 insertions(+), 7 deletions(-) delete mode 100644 delay_price.sh diff --git a/Dockerfile b/Dockerfile index dd424f4..297617e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,6 @@ RUN crontab /etc/cron.d/get_price RUN touch /var/log/cron.log -RUN chmod 755 /app/pulse.py /app/get_price.py /app/start.sh /app/delay_price.sh +RUN chmod 755 /app/pulse.py /app/get_price.py /app/start.sh CMD ["/app/start.sh"] diff --git a/delay_price.sh b/delay_price.sh deleted file mode 100644 index 2515c71..0000000 --- a/delay_price.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -source /app/env -printenv -sleep 2 - -python3 /app/get_price.py >> /var/log/cron.log 2>&1 \ No newline at end of file diff --git a/start.sh b/start.sh index 6632127..9df20c4 100644 --- a/start.sh +++ b/start.sh @@ -1,11 +1,22 @@ #!/bin/bash + +# Print environment variables to a file printenv > /app/env + # Start the cron service service cron start # Run your main application script (pulse.py) in the background python3 /app/pulse.py & +# Run the other script periodically in the background +( + while true; do + python3 /app/get_price.py >> /var/log/other_script.log 2>&1 + sleep 3 # Sleep for 60 seconds (1 minute) + done +) & + # Keep the container running and output both cron logs and pulse.py output tail -f /var/log/cron.log & wait -n From 043170481af6843ac9a6eb346895b4084f17cf28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:08:00 +0200 Subject: [PATCH 03/10] NMBFYGTGG --- Dockerfile | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index 297617e..6c3aebe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,6 @@ FROM python:3.12-slim-bookworm WORKDIR /app LABEL MAINTAINER="Jan-Ole Hübner " -RUN apt-get update && apt-get install -y cron - ADD requirements.txt /app RUN pip3 install -r /app/requirements.txt @@ -11,15 +9,6 @@ ENV PYTHONIOENCODING=utf-8 ADD . /app - -RUN echo "SHELL=/bin/bash\nBASH_ENV=/app/env\n* * * * * root /app/delay_price.sh >> /var/log/cron.log 2>&1" > /etc/cron.d/get_price - -RUN chmod 0644 /etc/cron.d/get_price - -RUN crontab /etc/cron.d/get_price - -RUN touch /var/log/cron.log - RUN chmod 755 /app/pulse.py /app/get_price.py /app/start.sh CMD ["/app/start.sh"] From 83a23b3c1bdb97e104004ad2e3a0eef93176a722 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:10:44 +0200 Subject: [PATCH 04/10] aaaaaaarghhhhhhhh --- get_price.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 get_price.py diff --git a/get_price.py b/get_price.py new file mode 100644 index 0000000..56d9aca --- /dev/null +++ b/get_price.py @@ -0,0 +1,35 @@ +import os +import sys +import tibber +from influxdb_client import InfluxDBClient +from influxdb_client.client.write_api import SYNCHRONOUS +from DataPoints import Price +import logging + +TOKEN = os.getenv('TOKEN', '') +TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') +URL = os.getenv('URL', "") +BUCKET = os.getenv('BUCKET', "tibber") +ORG = os.getenv('ORG', "Default") + +# Logging configuration +logger = logging.getLogger("TibberInflux") +formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +ch = logging.StreamHandler(sys.stdout) +ch.setFormatter(formatter) +logger.addHandler(ch) +logger.setLevel(logging.INFO) + +__version__ = "v0.3.0" +logger.info(__version__) + +client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) +write_api = client.write_api(write_options=SYNCHRONOUS) +query_api = client.query_api() + +account = tibber.Account(TIBBERTOKEN) +home = account.homes[0] +p = Price(home.current_subscription.price_info.current).get_datapoint() +write_api.write(record=p, bucket=BUCKET) +logger.info(vars(p)) \ No newline at end of file From d5fce90b8da5d7de78c86556bf59e85bd0614a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:13:59 +0200 Subject: [PATCH 05/10] aaaaaaarghhhhhhhh --- start.sh | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/start.sh b/start.sh index 9df20c4..de121f0 100644 --- a/start.sh +++ b/start.sh @@ -1,12 +1,5 @@ #!/bin/bash -# Print environment variables to a file -printenv > /app/env - -# Start the cron service -service cron start - -# Run your main application script (pulse.py) in the background python3 /app/pulse.py & # Run the other script periodically in the background @@ -17,6 +10,5 @@ python3 /app/pulse.py & done ) & -# Keep the container running and output both cron logs and pulse.py output -tail -f /var/log/cron.log & + wait -n From 877cda964074598df220f36fa2053bdaf7d679e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:16:16 +0200 Subject: [PATCH 06/10] aaaaaaarghhhhhhhh --- pulse.py | 161 ++++++++++++++++++++++++++----------------------------- 1 file changed, 77 insertions(+), 84 deletions(-) diff --git a/pulse.py b/pulse.py index 050444d..6e946cd 100644 --- a/pulse.py +++ b/pulse.py @@ -1,59 +1,3 @@ -import asyncio -import os -import signal -import sys -import logging - -import tibber -from influxdb_client import InfluxDBClient -from influxdb_client.client.write_api import SYNCHRONOUS -from DataPoints import Pulse, Price - -TOKEN = os.getenv('TOKEN', '') -TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') -URL = os.getenv('URL', "") -BUCKET = os.getenv('BUCKET', "tibber") -ORG = os.getenv('ORG', "Default") - -# Logging configuration -logger = logging.getLogger("TibberInflux") -formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') -ch = logging.StreamHandler(sys.stdout) -ch.setFormatter(formatter) -logger.addHandler(ch) -logger.setLevel(logging.INFO) - -__version__ = "v0.4.0" -logger.info(__version__) - -# Initialize InfluxDB client -client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) -write_api = client.write_api(write_options=SYNCHRONOUS) -query_api = client.query_api() - -# Initialize Tibber account -account = tibber.Account(TIBBERTOKEN) -home = account.homes[0] - - -def _incoming(data): - try: - p = Pulse(data).get_datapoint() - write_api.write(record=p, bucket=BUCKET) - logger.info(p) - except Exception as e: - logger.exception("Error in _incoming():") - raise e - - -def timeout_handler(signum, frame): - raise TypeError("Timeout occurred") - - -async def show_current_power(data): - signal.signal(signal.SIGALRM, timeout_handler) - signal.alarm(8) - _incoming(data) async def periodic_task(): @@ -69,36 +13,85 @@ async def periodic_task(): await asyncio.sleep(3) -async def main(): - loop = asyncio.get_event_loop() +import asyncio +import os +import signal +import sys + +import tibber +from influxdb_client import InfluxDBClient +from influxdb_client.client.write_api import SYNCHRONOUS +from DataPoints import Pulse +import logging + +TOKEN = os.getenv('TOKEN', '') +TIBBERTOKEN = os.getenv('TIBBERTOKEN', '') +URL = os.getenv('URL', "") +BUCKET = os.getenv('BUCKET', "tibber") +ORG = os.getenv('ORG', "Default") + +# Logging configuration +logger = logging.getLogger("TibberInflux") +formatter = logging.Formatter( + '%(asctime)s - %(name)s - %(levelname)s - %(message)s') +ch = logging.StreamHandler(sys.stdout) +ch.setFormatter(formatter) +logger.addHandler(ch) +logger.setLevel(logging.INFO) + +__version__ = "v0.3.4" +logger.info(__version__) +loop = asyncio.get_event_loop() + +logger.info("connecting to db...") +client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) +write_api = client.write_api(write_options=SYNCHRONOUS) +query_api = client.query_api() +logger.info("connecting to tibber...") +account = tibber.Account(TIBBERTOKEN) +home = account.homes[0] +logger.info("starting subscription...") + +def _incoming(data): + try: + p = Pulse(data).get_datapoint() + write_api.write(record=p, bucket=BUCKET) + logger.info(p) + except Exception as e: + logger.exception("Error in _incoming():") + raise e + +def timeout_handler(signum, frame): + raise TypeError("Timeout occurred") + +@home.event("live_measurement") +async def show_current_power(data): + signal.alarm(8) + _incoming(data) + +def stop(home): + return False + +try: signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(15) + home.start_live_feed(user_agent="pulse.py/0.3.4",exit_condition=stop(home),retries=2,retry_interval=3.0) + periodic_task_task = loop.create_task(periodic_task()) + + # Wait for both tasks to complete +except TypeError: + logger.exception("Timeout occurred while executing start_live_feed()") + client.close() + exit(1) +except Exception: + logger.exception("Error in start_live_feed()") + client.close() + exit(41) + + + + + - try: - # Start the live feed in a separate task - home_feed_task = loop.create_task(home.start_live_feed( - user_agent="pulse.py/0.4.0", - exit_condition=lambda: False, - retries=2, - retry_interval=3.0, - callback=show_current_power - )) - - # Start the periodic task - periodic_task_task = loop.create_task(periodic_task()) - - # Wait for both tasks to complete - await asyncio.gather(home_feed_task, periodic_task_task) - - except TypeError: - logger.exception("Timeout occurred while executing start_live_feed()") - client.close() - sys.exit(1) - except Exception: - logger.exception("Error in start_live_feed()") - client.close() - sys.exit(41) -if __name__ == "__main__": - asyncio.run(main()) From 9fa758157438deacbb365102aa819656065db96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 00:21:28 +0200 Subject: [PATCH 07/10] aaaaaaarghhhhhhhh --- pulse.py | 23 ++--------------------- start.sh | 4 ++-- 2 files changed, 4 insertions(+), 23 deletions(-) diff --git a/pulse.py b/pulse.py index 6e946cd..338a4a1 100644 --- a/pulse.py +++ b/pulse.py @@ -1,22 +1,8 @@ - - -async def periodic_task(): - while True: - try: - # Fetch price info and write to InfluxDB - p = Price(home.current_subscription.price_info.current).get_datapoint() - write_api.write(record=p, bucket=BUCKET) - logger.info(vars(p)) - except Exception as e: - logger.exception("Error in periodic_task():") - # Wait for the next run (e.g., every 60 seconds) - await asyncio.sleep(3) - - import asyncio import os import signal import sys +import time import tibber from influxdb_client import InfluxDBClient @@ -41,7 +27,7 @@ logger.setLevel(logging.INFO) __version__ = "v0.3.4" logger.info(__version__) -loop = asyncio.get_event_loop() + logger.info("connecting to db...") client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) @@ -76,9 +62,6 @@ try: signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(15) home.start_live_feed(user_agent="pulse.py/0.3.4",exit_condition=stop(home),retries=2,retry_interval=3.0) - periodic_task_task = loop.create_task(periodic_task()) - - # Wait for both tasks to complete except TypeError: logger.exception("Timeout occurred while executing start_live_feed()") client.close() @@ -93,5 +76,3 @@ except Exception: - - diff --git a/start.sh b/start.sh index de121f0..4a78868 100644 --- a/start.sh +++ b/start.sh @@ -5,8 +5,8 @@ python3 /app/pulse.py & # Run the other script periodically in the background ( while true; do - python3 /app/get_price.py >> /var/log/other_script.log 2>&1 - sleep 3 # Sleep for 60 seconds (1 minute) + python3 /app/get_price.py + sleep 900 done ) & From 9d3afcd801f96cc422168270af4e3e37ac7ae2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Sun, 18 Aug 2024 01:18:19 +0200 Subject: [PATCH 08/10] change timing --- get_price.py | 2 +- pulse.py | 2 +- start.sh | 10 ++++++---- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/get_price.py b/get_price.py index 56d9aca..a41a2c6 100644 --- a/get_price.py +++ b/get_price.py @@ -21,7 +21,7 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.3.0" +__version__ = "v0.4.0" logger.info(__version__) client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) diff --git a/pulse.py b/pulse.py index 338a4a1..cd08f9f 100644 --- a/pulse.py +++ b/pulse.py @@ -25,7 +25,7 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.3.4" +__version__ = "v0.4.0" logger.info(__version__) diff --git a/start.sh b/start.sh index 4a78868..1ac5d74 100644 --- a/start.sh +++ b/start.sh @@ -2,13 +2,15 @@ python3 /app/pulse.py & -# Run the other script periodically in the background +# Run the other script at 0, 15, 30, 45, and 60 minutes ( while true; do - python3 /app/get_price.py - sleep 900 + current_minute=$(date +'%M') + if [[ "$current_minute" == "00" || "$current_minute" == "15" || "$current_minute" == "30" || "$current_minute" == "45" || "$current_minute" == "00" ]]; then + python3 /app/get_price.py + fi + sleep 60 # Check every minute done ) & - wait -n From b13d3e685748d742b1cb2e5f33f3e839967639f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Wed, 11 Dec 2024 18:31:24 +0100 Subject: [PATCH 09/10] change retries --- get_price.py | 2 +- pulse.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 get_price.py mode change 100644 => 100755 pulse.py diff --git a/get_price.py b/get_price.py old mode 100644 new mode 100755 index a41a2c6..5fa5fe8 --- a/get_price.py +++ b/get_price.py @@ -21,7 +21,7 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.4.0" +__version__ = "v0.4.2" logger.info(__version__) client = InfluxDBClient(url=URL, token=TOKEN, org=ORG) diff --git a/pulse.py b/pulse.py old mode 100644 new mode 100755 index cd08f9f..061945e --- a/pulse.py +++ b/pulse.py @@ -25,7 +25,7 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.4.0" +__version__ = "v0.4.2" logger.info(__version__) @@ -61,7 +61,7 @@ def stop(home): try: signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(15) - home.start_live_feed(user_agent="pulse.py/0.3.4",exit_condition=stop(home),retries=2,retry_interval=3.0) + home.start_live_feed(user_agent="pulse.py/0.4.2",exit_condition=stop(home),retries=4,retry_interval=5.0) except TypeError: logger.exception("Timeout occurred while executing start_live_feed()") client.close() From 336eefb963b87d412a1777d36be9e04dd6f71b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Ole=20Hu=CC=88bner?= Date: Thu, 19 Dec 2024 01:48:58 +0100 Subject: [PATCH 10/10] acc cost filter --- DataPoints.py | 2 +- pulse.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) mode change 100644 => 100755 DataPoints.py diff --git a/DataPoints.py b/DataPoints.py old mode 100644 new mode 100755 index 2431c82..6ce07b6 --- a/DataPoints.py +++ b/DataPoints.py @@ -14,7 +14,7 @@ class Pulse: "power": float(power), "lastMeterConsumption": float(lastMeterConsumption) } - if cost is not None: + if cost is not None and float(cost) > 0: fields.update({'accumulatedCost':cost}) self.datapoint = {"fields": fields, "tags": tags, diff --git a/pulse.py b/pulse.py index 061945e..97c18f7 100755 --- a/pulse.py +++ b/pulse.py @@ -25,7 +25,7 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.INFO) -__version__ = "v0.4.2" +__version__ = "v0.4.3" logger.info(__version__) @@ -61,7 +61,7 @@ def stop(home): try: signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(15) - home.start_live_feed(user_agent="pulse.py/0.4.2",exit_condition=stop(home),retries=4,retry_interval=5.0) + home.start_live_feed(user_agent="pulse.py/0.4.3",exit_condition=stop(home),retries=4,retry_interval=5.0) except TypeError: logger.exception("Timeout occurred while executing start_live_feed()") client.close()