0

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

Mike
  • 3
  • 3

1 Answers1

0

Wouldn't it work to just add date as a variable in the filename?

with open('/media/pi/boiler_log_' + date + '.csv', 'a', newline='') as f:
Erik
  • 11
  • 2
  • Thanks for the suggestion Erik. However the log file is opened near the top of the script to create the header. Shouldn't there be a test to see if the date has changed before creating the file? – Mike Apr 25 '23 at 13:42
  • @Mike In that case, maybe you could have an if-check with os.path.isfile that adds the headers at the beginning if the file does not already exist? – Erik Apr 25 '23 at 14:37