import os import ssl import smtplib import tempfile import time import urllib.request from datetime import datetime, timedelta from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from influxdb import InfluxDBClient import requests # Configurations strFrom = 'pv-tannenstr@web.de' strTo = ["tobias.siegel@outlook.com", "wsiegel@web.de"] bot_token = 'YOUR_TELEGRAM_BOT_TOKEN' chat_id = 'YOUR_TELEGRAM_CHAT_ID' def get_boot_time(): with open('/proc/uptime', 'r') as f: uptime_seconds = int(float(f.readline().split()[0])) boot_time = datetime.now() - timedelta(seconds=uptime_seconds) + timedelta(hours=1) return boot_time def get_kwh(beginning, end): client = InfluxDBClient(host='127.0.0.1', port=8086, database='solar') result = client.query("SELECT sum(\"eingespeiste_leistung\") *0.000002777777777 FROM \"Tannenstrasse\" WHERE time >= {}ms and time <= {}ms".format(beginning, end)) if 'series' in result.raw: return round(result.raw['series'][0]['values'][0][1], 1) else: return "0 - Keine Werte" def create_email(kwh): msgRoot = MIMEMultipart('related') msgRoot['Subject'] = f'{datetime.today().strftime("%Y-%m-%d")} - {kwh}kWh - Solaranlage' msgRoot['From'] = strFrom msgRoot['To'] = ",".join(strTo) msgRoot.preamble = 'This is a multi-part message in MIME format.' msgAlternative = MIMEMultipart('alternative') msgRoot.attach(msgAlternative) msgText = MIMEText('This is the alternative plain text message.') msgAlternative.attach(msgText) bt = get_boot_time() bt_str = bt.strftime("%d.%m.%Y, %H.%M") msgText = MIMEText(f'

Letzter Start: {bt_str} Uhr

', 'html') msgAlternative.attach(msgText) with open(tmpdir.name + '/leistung.png', 'rb') as fp: msgImage = MIMEImage(fp.read()) msgImage.add_header('Content-ID', '') msgRoot.attach(msgImage) return msgRoot def send_email(msgRoot): context = ssl.create_default_context() with smtplib.SMTP('smtp.web.de', 587) as server: server.ehlo() server.starttls(context=context) server.ehlo() server.login(strFrom, 'PV-Tannenstr') server.sendmail(strFrom, strTo, msgRoot.as_string()) def send_telegram(bot_token, chat_id, kwh): bot_url = f'https://api.telegram.org/bot{bot_token}/sendPhoto' bt = get_boot_time() bt_str = bt.strftime("%d.%m.%Y, %H.%M") caption = f'{datetime.today().strftime("%Y-%m-%d")} - {kwh}kWh - Solaranlage\nLetzter Start: {bt_str} Uhr' with open(tmpdir.name + '/leistung.png', 'rb') as img: response = requests.post( bot_url, data={'chat_id': chat_id, 'caption': caption}, files={'photo': img} ) return response if __name__ == "__main__": tmpdir = tempfile.TemporaryDirectory() beginning = str((1000 * int(time.time())) - (17 * 3600000)) end = str(1000 * int(time.time())) urllib.request.urlretrieve(f"http://localhost:3000/render/d-solo/-wCxs6mgk/solaranlage?orgId=1&refresh=10s&panelId=2&from={beginning}&to={end}&width=600&height=300&tz=Europe%2FBerlin", f"{tmpdir.name}/leistung.png") kwh = get_kwh(beginning, end) email_msg = create_email(kwh) send_email(email_msg) send_telegram(bot_token, chat_id, kwh) tmpdir.cleanup()