Page 1 of 1

Create rolling CSV file with dzVents

Posted: Wednesday 08 January 2020 19:44
by MikeF
I currently create rolling 24-hour graphs of Domoticz data (temperature, power) using Google Charts with a csv file created from a python script.

Image

The python script is very clunky - it runs every 10 minutes (cron), reading data from Domoticz (using the JSON API) for 6 devices, uses the python pop command to remove the oldest row from the file, and then creates a new row (at the end of the file) with the latest data. Here's a snippet of the code:
Spoiler: show

Code: Select all

# get temperature data

with open(temps_file,'r') as f:
 	n = len(f.readlines())

if n > 144: # readings every 10 mins. = 144 / day + header
	with open(temps_file,'rb') as f:
		reader = list(csv.reader(f,delimiter = ","))
		reader.pop(1)  # removes the oldest row, after the header
		with open("temp.csv",'wb') as f1:
			writer = csv.writer(f1,delimiter = ",")
			for row in reader:
				writer.writerow(row)
	
	shutil.move("temp.csv",temps_file)

with open(temps_file, 'a') as output:
	outfile = csv.writer(output, delimiter=",")
	
	now = datetime.datetime.now().strftime('%H:%M')
	outfile.writerow([now, inside, target, outside, lounge, greenhouse, heating])
	
Is there a better / more elegant way to do this using dzVents?

Re: Create rolling CSV file with dzVents

Posted: Wednesday 08 January 2020 22:01
by waaren
MikeF wrote: Wednesday 08 January 2020 19:44 I currently create rolling 24-hour graphs of Domoticz data (temperature, power) using Google Charts with a csv file created from a python script.
Is there a better / more elegant way to do this using dzVents?
"Elegant" will always be a matter of taste. But I can try to build something in dzVents if you show the required csv file with some information what type of device is reflected in the row / columns.

Re: Create rolling CSV file with dzVents

Posted: Wednesday 08 January 2020 23:38
by MikeF
Many thanks, waaren.

(Re 'elegant': I just thought my python script was inefficient and clunky, and that there might be a neater way in dzVents.)

Here's an extract from the temperatures csv file [annotated with comments - not in actual file]:

Code: Select all

now, inside, target, outside, lounge, greenhouse, heating  [header row]
22:10,21.2,7.0,12.7,21.3,11.8,0   [oldest data: 24 hours minus 10 mins. ago]
22:20,21.1,7.0,12.7,21.3,11.8,0   [next data: 10 mins. later]
...
22:00,21.1,7.0,9.4,22.0,8.0,0     [latest data]
'now' is time; 'inside', 'target', 'outside', 'lounge', 'greenhouse' are all temperatures from Domoticz temperature devices.
'heating' is a dummy variable set in the script, with a value of 5 if heating is on (taken from a dummy switch) and 0 if off
- so that it appears on the graph as the shaded series.

There are 144 data rows (every 10 mins. for 24 hours), dropping the oldest row and adding the latest row to give the rolling 24 hour graph.

(I created what has turned out as a rather complex piece of javascript, which reads the csv file and calls Google Chart functions. This runs on an nginx webserver on my Domoticz server, called every 10 minutes by cron.)