sorry, die geschweiften Klammern habe ich nicht gesehen, aber jetzt wo Du es sagst, habe ich es probiert und es funktioniert auch, habe jetzt Deine Version gelassen.
Gruss und Danke
gwaag
sorry, die geschweiften Klammern habe ich nicht gesehen, aber jetzt wo Du es sagst, habe ich es probiert und es funktioniert auch, habe jetzt Deine Version gelassen.
Gruss und Danke
gwaag
Hier wie gewünscht.
Danke.
pi@solaranzeige:~/th $ /home/pi/th/WP-Steuerung.py
Einspeisung/Bezug: 105.0 W
Wohnzimmer/Schnitt: 22.8°
Aussentemperatur: 14.0°
Kalenderwoche: 19
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.7/logging/__init__.py", line 1034, in emit
msg = self.format(record)
File "/usr/lib/python3.7/logging/__init__.py", line 880, in format
return fmt.format(record)
File "/usr/lib/python3.7/logging/__init__.py", line 619, in format
record.message = record.getMessage()
File "/usr/lib/python3.7/logging/__init__.py", line 380, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
File "/home/pi/th/WP-Steuerung.py", line 163, in <module>
logging.info("Heizung Aus KW: ", s) # Logeintrag
Message: 'Heizung Aus KW: '
Arguments: ('19',)
Display More
#!/usr/bin/env python3
import json
import requests
import logging
import locale
import datetime
from datetime import datetime
import http.client
import time
from time import sleep
from time import strftime
from influxdb import InfluxDBClient
while True:
sleep(60)
#Logging
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
logging.basicConfig(filename='/home/pi/th/sgr.log', format='%(asctime)s %(message)s', datefmt="Woche %V %a %d.%m.%Y %H:%M",
level=logging.INFO)
# Zeitfenster
now = datetime.now()
Now = now.hour * 60 + now.minute
tw1 = (7*60+0) <= Now < (18*60+0) # ein
# tw2 = (22*60+0) <= Now < (22*60+2) # aus
# tw3 = (20*60+5) <= Now < (22*60+2) # ein
# tw4 = (22*60+0) <= Now < (22*60+30) # aus
tw5 = (22*60+30) <= Now < (22*60+32) # ein Nachtabsenkung
tw6 = (6*60+55) <= Now < (6*60+57) # aus Nachtabsenkung
# sperrzeit = (11*60+15) <= Now < (12*60+10) # EVU-sperrzeit
# Periode Winter oder Sommer
monat = datetime.now().month
sommer = monat > 4 and monat <= 9
winter = monat <= 4 or monat >= 9
# Enspeisung WR von Influx auslesen
client = InfluxDBClient(host='192.168.0.29', port=8086) # ip oder localhost
client.switch_database('zaehler')
results = client.query('SELECT "Gesamtleistung" FROM "AC" GROUP BY * ORDER BY DESC LIMIT 1')
points = results.get_points
point = 0
for point in points():
einspeisung = point['Gesamtleistung']
print ((f"Einspeisung/Bezug: {round (einspeisung, 1)} W"))
logging.info("Einspeisung/Bezug: %.1f W", einspeisung) # Logeintrag
# Wohnzimmer Raumtemperatur auslesen (Wohnzimmer west und ost von Controme)
response = requests.get("http://192.168.0.23/get/json/v1/1/temps/")
obj = response.json()
for raum in obj[0]["raeume"]:
if raum["name"] == "Wohnen west":
if raum["sensoren"][1]["wert"]:
raum_temperatur1 = raum["sensoren"][1]["wert"]
for raum in obj[0]["raeume"]:
if raum["name"] == "Wohnen ost":
if raum["sensoren"][1]["wert"]:
raum_temperatur2 = raum["sensoren"][1]["wert"]
durchschnitt = (raum_temperatur1 + raum_temperatur2) / 2 # + 0.2
print ((f"Wohnzimmer/Schnitt: {round (durchschnitt, 1)}°"))
logging.info("Wohnzimmer/Schnitt: %.1f°", durchschnitt) # Logeintrag
zu_kalt = durchschnitt < 21.0
zu_warm = durchschnitt > 22.0
viel_zuwarm = durchschnitt > 22.8
# Wetter Aussentemperatur und Luftfeuchte auslesen
settings = {
'api_key':'550038810b2df71231e4d0e2529c2de3',
'zip_code':'8212',
'country_code':'ch',
'temp_unit':'metric'} #unit can be metric, imperial, or kelvin
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?appid={0}&zip={1},{2}&units={3}"
final_url = BASE_URL.format(settings["api_key"],settings["zip_code"],settings["country_code"],settings["temp_unit"])
weather_data = requests.get(final_url).json()
outside_temp = weather_data['main']['temp']
print((f"Aussentemperatur: {round (outside_temp, 1)}°"))
logging.info(f"Aussentemperatur: %.1f°", outside_temp) # Logeintrag
# humidity = weather_data['main']['humidity']
# print((f"Luftfeuchte: {round (humidity, 1)}%"))
unter_0 = outside_temp <= 0
# HEIZUNG ANHEBUNG EIN wenn PV Ueberschuss oder unter 0 oder zu kalt
# SG-Ready Scenario 1 1 ON / ON Leistung Max bit1 IP .150 / bit2 IP .151
if winter and tw1 and einspeisung <= -1500 and not zu_warm or tw1 and unter_0 or tw1 and zu_kalt: # 7.00-18.00h > 22.0 < 21.0
conn = http.client.HTTPConnection("[192.168.0.151]")
conn.request("GET", "/relay/0?turn=on") # Closed
sleep(0.1)
conn = http.client.HTTPConnection("[192.168.0.150]")
conn.request("GET", "/relay/0?turn=on") # Closed
logging.info('***********SG-Ready +++') # Logeintrag
sleep(3600) # 1h eingeschaltet
logging.info('*******SG-Ready sleep+++') # Logeintrag
# HEIZUNG ANHEBUNG AUS wenn zu warm
# SG-Ready Aus Scenario 0 0 OFF / OFF Normalbetrieb bit1 IP .150 / bit2 IP .151
if winter and zu_warm and not viel_zuwarm and tw1 and not tw5: # 7.00h-18.00h > 22.0° > 22.8° 22.00h-22.30h
conn = http.client.HTTPConnection("[192.168.0.150]")
conn.request("GET", "/relay/0?turn=off") # Open
sleep(0.1)
conn = http.client.HTTPConnection("[192.168.0.151]")
conn.request("GET", "/relay/0?turn=off") # Open
logging.info('***********SG-Ready norm') # Logeintrag
sleep(1800) # 1/2h ausgeschaltet
logging.info('*****SG-Ready sleep-norm') # Logeintrag
# HEIZUNG Nachtabsenkung EIN/AUS
# SG-Ready Scenario 0 1 OFF / ON Leistung Min
if tw5 or viel_zuwarm: # 22.30h-22.32h > 22.8°
conn = http.client.HTTPConnection("[192.168.0.151]")
conn.request("GET", "/relay/0?turn=on") # Closed
sleep(0.1)
conn = http.client.HTTPConnection("[192.168.0.150]")
conn.request("GET", "/relay/0?turn=off") # Open
logging.info('*********SG-Ready Nachtabsenkung ein') # Logeintrag
if tw6: # 7.00h
conn = http.client.HTTPConnection("[192.168.0.150]")
conn.request("GET", "/relay/0?turn=off") # Open
sleep(0.1)
conn = http.client.HTTPConnection("[192.168.0.151]")
conn.request("GET", "/relay/0?turn=off") # Open
logging.info('*********SG-Ready Nachtabsenkung aus') # Logeintrag
s = strftime("%W") # Kalenderwoche
if s >= str(18):
print("Kalenderwoche: ", s)
logging.info("Heizung Aus KW: ", s) # Logeintrag
Display More
Hallo,
mein logeintrag gibt folgende Fehlermeldung aus:
--- Logging error ---
Traceback (most recent call last):
File "/usr/lib/python3.7/logging/__init__.py", line 1034, in emit
msg = self.format(record)
File "/usr/lib/python3.7/logging/__init__.py", line 880, in format
return fmt.format(record)
File "/usr/lib/python3.7/logging/__init__.py", line 619, in format
record.message = record.getMessage()
File "/usr/lib/python3.7/logging/__init__.py", line 380, in getMessage
msg = msg % self.args
TypeError: not all arguments converted during string formatting
Call stack:
File "/home/pi/th/WP-Steuerung.py", line 163, in <module>
logging.info("Heizung Aus KW: ", s) # Logeintrag
Message: 'Heizung Aus KW: '
Arguments: ('19',)
^X^CTraceback (most recent call last):
File "/home/pi/th/WP-Steuerung.py", line 17, in <module>
sleep(60)
KeyboardInterrupt
Der print eintrag funktioniert, Heizung Aus KW 19
s = strftime("%W") # Kalenderwoche
if s >= str(18):
print("Kalenderwoche: ", s)
logging.info("Heizung Aus, KW: ", s) # Logeintrag
Andere logging.info im selben scrip funktionieren mit gleicher schreibweise.
Gruss und Danke
gwaag
So, jetzt gehts.
Habe die Schleife raus, und alles wieder richtig eingerückt, und starte jetzt das script mit crontab -e, jede Minute.(später wird das noch in Timer Unit geändert)
Log zeigt jetzt Eintrag pro Ereignis nur noch 1x pro minute.
Danke an alle.
Gruss
gwaag
Macht immer noch zu viele Einträge.
#!/usr/bin/env python3
import logging
import locale
import datetime
from datetime import datetime
import http.client
from influxdb import InfluxDBClient
from socket import socket, AF_INET, SOCK_DGRAM
import time
from time import sleep
# Logging
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
logging.basicConfig(filename='/home/pi/th/licht.log', format='%(asctime)s %(message)s', datefmt="Woche %V %a %d.%m.%Y %H:%M",
level=logging.INFO)
while True:
sleep(60)
# Zeitfenster
now = datetime.now()
Now = now.hour * 60 + now.minute
Display More
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -41.5 W
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:02 Einspeisung: -81.4 W
Woche 12 Fr 24.03.2023 13:02 gelbw
Woche 12 Fr 24.03.2023 13:03 Einspeisung: -81.4 W
Woche 12 Fr 24.03.2023 13:03 Einspeisung: -81.4 W
Woche 12 Fr 24.03.2023 13:03 gelbw
Woche 12 Fr 24.03.2023 13:03 Einspeisung: -81.4 W
Display More
Danke Dennis89.
ist gemacht, beobachte weiter und melde mich spaeter wieder.
Ja das hatten wir schon mal. Habe aber vor langer Zeit die Logs separat, pro script gemacht.
Hier die beiden System-Units ( Mit systemctl status, laufen beide.)
[Unit]
Description=PV_Licht
After=multi-user.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/th/PV_Licht.py
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
Display More
[Unit]
Description=WP-Steuerung
After=multi-user.target
Wants=network-online.target
StartLimitIntervalSec=300
StartLimitBurst=5
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/pi/th/WP-Steuerung.py
Restart=on-failure
RestartSec=1s
[Install]
WantedBy=multi-user.target
Display More
Crontab-Einträge gibt es nicht.
" Konfiguriere deine Logeinträge einmal vor der Schleife und nicht in jedem Durchgang."
Das verstehe ich nicht, wie muss ich das machen?
Danke
gwaag
Habe jetzt die script angepasst.
Script WP-Steuerung is jetzt eigentlich ok, Script PV-Licht ist etwas besser geworden, logged aber immer noch das Gleiche 5-6x obwohl der Eintrag alle 60 sec nur 1x erfolgen sollte .
Habe mal die Files angehaengt, ev habe ich ja noch was nicht richtig verstanden und es ist immer noch etwas falsch.
Danke und Gruss
gwaag
stelle hier mal das gesamte script ein.
Bitte nicht steinigen.
Hallo,
glaube vor einigen Monaten schon mal gefragt zu haben, weiss aber leider nicht mehr wo der Beitrag zu finden ist.
Problem:
Ich habe ein Python script, das lauft wunderbar, aber in die .log Datei schreibt es pro sec. bis zu 14x, obwohl das script eigentlich nur alle 60 sec. abgearbeitet wird.
Auszug aus .log
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:13 gelbw
Woche 12 Do 23.03.2023 16:44:22 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:22 Einspeisung: -220.8 W
Woche 12 Do 23.03.2023 16:44:22 gelbw
Woche 12 Do 23.03.2023 16:44:22 gelbw
Display More
#!/usr/bin/env python3
import logging
import locale
import datetime
import http.client
from influxdb import InfluxDBClient
from socket import socket, AF_INET, SOCK_DGRAM
import time
from time import sleep
while True:
sleep(60)
# Logging
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
logging.basicConfig(filename='/home/pi/th/licht.log', format='%(asctime)s %(message)s', datefmt="Woche %V %a %d.%m.%Y %H:%M:%S",
level=logging.INFO)
# Beispiel eines logs:
if winter and einspeisung >= -1200 and einspeisung < -0 and Ledw_ein:
send_command(ZONE1_ON, IP, PORT)
sleep(0.1)
send_command(Gelb, IP, PORT)
send_command(ZONE1_Dimm, IP, PORT)
logging.info('gelbw') # Logeintrag
# Einspeisung Influx auslesen
client = InfluxDBClient(host='192.168.0.29', port=8086) # ip oder localhost
client.switch_database('zaehler')
results = client.query('SELECT "Gesamtleistung" FROM "AC" GROUP BY* ORDER BY DESC LIMIT 1')
points = results.get_points
point = 0
for point in points():
shelly_eks = point['Gesamtleistung']
einspeisung = shelly_eks
print ((f"Einspeisung: {round (einspeisung, 1)} W"))
logging.info("Einspeisung: %.1f W", einspeisung) # Logeintrag
Display More
Ist da Grundsäzlich was falsch?
Warum loged es pro durchgang nicht nur 1x?
Das script wird mit systemd gestartet und läuft zuverlässig.
Gruss und Danke
gwaag
Noch eine Frage.
Wenn ich mehrere http Abfragen habe muss dann payload auf payload 1/2/3 geändert werden ?
# TEST
if ausschalten:
payload = {'SetHeatpump': '0'} # payload1 ??
response = requests.get('http://192.168.0.200/command', params=payload) # palload1 ??
print(response.url)
print(response)
print(response.text)
logging.info('WP aus')
if einschalten:
payload = {'SetHeatpump': '1'} # payload2 ??
response = requests.get('http://192.168.0.200/command', params=payload) # payload2 ??
print(response.url)
print(response)
print(response.text)
logging.info('WP ein')
Display More
Hallo,
wenn ich im Browser http://192.168.0.200/command?SetHeatpump=1 eingebe, funktioniert es, schaltet ein /aus
wenn ich im Python scrip das mache geht es nicht:
#!/usr/bin/env python3
import requests
import logging
import locale
import http.client
if irgendwas:
conn = http.client.HTTPConnection("[192.168.0.200]")
conn.request("GET", "command?SetHeatpump=0")
r1 = conn.getresponse()
print (r1.status, r1.reason)
logging.info('WP aus')
if irgendwasanderes:
conn = http.client.HTTPConnection("[192.168.0.200]")
conn.request("GET", "command?SetHeatpump=1")
r1 = conn.getresponse()
print (r1.status, r1.reason)
logging.info('WP ein')
Display More
Traceback (most recent call last):
File "/home/pi/th/WP-Steuerung_test.py", line 102, in <module>
r1 = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1352, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 310, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 279, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
Was mache ich falsch?
Danke für die schnelle Rückmeldungen , ohne Temp funktioniert es, Danke