I use this script to read parameters from a boiler's data bus every 10 seconds and write them to a csv file:
from subprocess import check_output, Popen, PIPE
import csv
from datetime import datetime
import threading
bai = ['/usr/bin/ebusctl', 'read', '-c', 'bai', '-f']
f39 = ['/usr/bin/ebusctl', 'read', '-c', 'f39', '-f']
header = ['Date','Time','Flow','Return','Flame','Modulation','Flow Temp Desired','Energy CH','Energy HW','Room Temp']
with open('/media/pi/boiler_log.csv', 'w', newline = '') as f:
writer = csv.writer(f)
writer.writerow(header)
def read_ebus():
threading.Timer(10.0, read_ebus).start()
date = datetime.today().strftime('%Y-%m-%d')
time = datetime.today().strftime('%H:%M:%S')
flow_temp = float(check_output(bai+['FlowTemp', 'temp']))
return_temp = float(check_output(bai+['ReturnTemp', 'temp']))
flame = Popen([*bai, 'Flame'], stdout=PIPE, stderr=PIPE).communicate()[0].decode().strip()
modulation = float(check_output(bai+['ModulationTempDesired']))
flow_temp_desired = float(check_output(bai+['FlowTempDesired']))
energy_ch = int(check_output(bai+['PrEnergySumHc1']))
energy_hw = int(check_output(bai+['PrEnergySumHwc1']))
room_temp = float(check_output(f39+['RoomTemp', 'temp']))
data = [date,time,flow_temp,return_temp,flame,modulation,flow_temp_desired,energy_ch,energy_hw,room_temp]
with open('/media/pi/boiler_log.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
read_ebus()
The script runs continuously creating an ever larger log file, but I would like instead to create a new file every 24 hours with the date in the filename.
How can I do this please?
Mike