Page 1 of 1

How to update rrd database in python

Posted: Mon Dec 21, 2015 3:06 pm
by Obel
I am new to the programing and I am using already created scripts, I am trying to update my rrdtool database in python. I have manage to create below code which don’t come back to me with any errors but when I am trying to generate a graph it don’t contain any data.
#!/usr/bin/python

#modules
import sys

import os

import time

import rrdtool

import Adafruit_DHT as dht

#assign data
h,t = dht.read_retry(dht.DHT22, 22)

#display data
print 'Temp={0:0.1f}*C'.format(t, h)
print 'Humidity={1:0.1f}%'.format(t,h)

#update database
data = "N:h:t"
ret = rrdtool.update("%s/humidity.rrd" % (os.path.dirname(os.path.abspath(__file__))),data)

if ret:
print rrdtool.error()
time.sleep(300)
Below my data base specification:
#! /bin/bash
rrdtool create humidity.rrd \
--start "01/01/2015" \
--step 300 \
DS:th_dht22:GAUGE:1200:-40:100 \
DS:hm_dht22:GAUGE:1200:-40:100 \
RRA:AVERAGE:0.5:1:288 \
RRA:AVERAGE:0.5:6:336 \
RRA:AVERAGE:0.5:24:372 \
RRA:AVERAGE:0.5:144:732 \
RRA:MIN:0.5:1:288 \
RRA:MIN:0.5:6:336 \
RRA:MIN:0.5:24:372 \
RRA:MIN:0.5:144:732 \
RRA:MAX:0.5:1:288 \
RRA:MAX:0.5:6:336 \
RRA:MAX:0.5:24:372 \
RRA:MAX:0.5:144:732 \

Re: How to update rrd database in python

Posted: Mon Dec 21, 2015 3:26 pm
by danjperron
Check my step by step method .

I do have in my python script code to update the rddtool with Highcharts

viewtopic.php?p=672576#p672576

Re: How to update rrd database in python

Posted: Wed Jan 13, 2016 1:09 pm
by Obel
yea, based on your script i have minimaze my and it worked.

Again thank you

Code: Select all

#!/usr/bin/python

#laduje moduly
import sys

import os

import time

import rrdtool

import subprocess

import Adafruit_DHT as dht

#przypisuje
h,t = dht.read_retry(dht.DHT22, 22)

#display data
print 'Temp={0:0.1f}*C'.format(t, h)
print 'Humidity={1:0.1f}%'.format(t,h)

#RRD TOOL UPDATE

def Validate(value):
  if value == None:
    return ":U"
  else:
    return ":{}".format(value)

#create text string to insert data

rdata = "N" + Validate(h)

#now let insert it into the rddtool data

fileRrdtool = "/home/pi/Hum/humidity.rrd"

subprocess.Popen(["/usr/bin/rrdtool","update",fileRrdtool,rdata])

Re: How to update rrd database in python

Posted: Thu Apr 21, 2016 8:21 am
by sufenta
I hope somone is able to help me. When i use your code i get an error:

Temp=20.0*C
Humidity=21.0%
ERROR: /home/pi/Test/test/humidity.rrd: expected 2 data source readings (got 1) from N

any ideas?

Thanks

Re: How to update rrd database in python

Posted: Sun Apr 24, 2016 8:12 am
by sufenta
no one an idea?

Re: How to update rrd database in python

Posted: Sun Apr 24, 2016 8:48 am
by tom.slick
sufenta wrote:I hope somone is able to help me. When i use your code i get an error:

Temp=20.0*C
Humidity=21.0%
ERROR: /home/pi/Test/test/humidity.rrd: expected 2 data source readings (got 1) from N

any ideas?

Thanks
expected 2 data source readings (got 1)
You only pass the value h. What happend to t?

original

Code: Select all

data = "N:h:t"
now

Code: Select all

rdata = "N" + Validate(h)

Re: How to update rrd database in python

Posted: Sun Apr 24, 2016 12:31 pm
by danjperron
The validate function is there to check if the sensor return something
if not it will return ':U' => unknow

Code: Select all

ef Validate(value):
  if value == None:
    return ":U"
  else:
    return ":{}".format(value)
And yes if your rrdtool needs 3 inputs you should send all your inputs.

Code: Select all

#create text string to insert data

rdata = "N" + Validate(cpuTemp) + Validate(sensorData[0]) + Validate(sensorData[1])

#now let insert it into the rddtool data

fileRrdtool = "/home/pi/temperatures.rrd"

subprocess.Popen(["/usr/bin/rrdtool","update",fileRrdtool,rdata])

If you add some input data at your rrd database you could check using the info command

ex:

Code: Select all

pi@Pi2 ~ $ rrdtool info temperatures.rrd 
filename = "temperatures.rrd"
rrd_version = "0003"
step = 300
last_update = 1420088400
header_size = 5096
ds[th_cpu].index = 0
ds[th_cpu].type = "GAUGE"
ds[th_cpu].minimal_heartbeat = 1200
ds[th_cpu].min = -4,0000000000e+01
ds[th_cpu].max = 1,0000000000e+02
ds[th_cpu].last_ds = "U"
ds[th_cpu].value = 0,0000000000e+00
ds[th_cpu].unknown_sec = 0
ds[th_dht22].index = 1
ds[th_dht22].type = "GAUGE"
ds[th_dht22].minimal_heartbeat = 1200
ds[th_dht22].min = -4,0000000000e+01
ds[th_dht22].max = 1,0000000000e+02
ds[th_dht22].last_ds = "U"
ds[th_dht22].value = 0,0000000000e+00
ds[th_dht22].unknown_sec = 0
ds[hm_dht22].index = 2
ds[hm_dht22].type = "GAUGE"
ds[hm_dht22].minimal_heartbeat = 1200
ds[hm_dht22].min = -4,0000000000e+01
ds[hm_dht22].max = 1,0000000000e+02
ds[hm_dht22].last_ds = "U"
ds[hm_dht22].value = 0,0000000000e+00
ds[hm_dht22].unknown_sec = 0
On this example I do have 3 inputs. Cpu temperature, DHT22 Temperature and DHT22 humidity.


Maybe it is a good thing to read the rrdtool doc.

Daniel