getting DS18b20 working with cacti on Raspberry Pi

check its seeing your thermometers
cat /sys/bus/w1/devices/

thermometer on my little breadboard happens to be

read its temp via
cat /sys/bus/w1/devices/28-000006c7bfb5/w1_slave
d7 01 4b 46 7f ff 09 10 55 : crc=55 YES
d7 01 4b 46 7f ff 09 10 55 t=29437


# 28-000006c7bfb5

import sys

# Set our constant for number of retries

def process_string(text):
# Split the text with new lines (\n) and select the second line.
secondline = text.split(“\n”)[1]
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0).
temperaturedata = secondline.split(” “)[9]
# The first two characters are “t=”, so get rid of those and convert the temperature from a string to a number.
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
return temperature

def tempature_valid(text):
return “YES” in text

# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before.
tfile = open(“/sys/bus/w1/devices/28-000006c7bfb5/w1_slave”)

# Read all of the text in the file, keep retrying until the CRC is valid
text =
count = 0

while(not tempature_valid(text) and count < NUM_RETRIES):
text =
count = count + 1

# If we’ve tried to read the sensor more than 3 times and failed, gracefully exit
if(count == NUM_RETRIES):

# Close the file now that the text has been read.

# Process the string and print out the tempature
tempature = process_string(text)

# change -0.0 for error correction for each sensor once true values are known
# only print the tempature if its not 85
if not tempature == 85:
print tempature -0.0

for cacti
python /home/pi/

shamelessly stolen from
alter it to suit ds18b20

The steps in Cacti follow. In all causes where I have not specified a value, use the default.

  1. Create the Data Input Method
    1. “Collection Methods” -> “Data Input Methods” -> “Add”
    2. Name: TEMPer USB deg C
    3. Input Type: Script/Command
    4. Input String: /usr/local/bin/temper-mon
    5. Press “Create”
    6. “Output Fields” -> “Add”
    7. Field [Output]: temp_celsius
    8. Friendly Name: Temperature in degrees Celsius
  2. Create the Data Template
    1. “Templates” -> “Data Templates” -> “Add”
    2. Data Template Name: Local Temperature
    3. Date Source / Name: |host_description| – Temperature
    4. Data Source / Data Input Method: TEMPer USB deg C
    5. Data Source Item / Internal Data Source Name: temp_c
    6. Press “Create”
    7. “Output Field” will now say “temp_celsius – Temperature in degrees Celsius”. Press “Save”.
  3. Create the Graph Template
      1. “Templates” -> “Graph Templates” -> “Add”
      2. Template Name: Local Temperature
      3. Title: |host_description| – Temperature
      4. Lower Limit: 20 (optional, I find this gives useful graph heights)
      5. Unit Exponent Value: 0
      6. Vertical Label: degrees Celsius
      7. Press “Create”
      8. Now create the line on the graph.
        1. “Graph Template Items” -> “Add”
        2. Data Source: Local Temperature – (temp_c)
        3. Color: 0000FF
        4. Graph Item Type: LINE1
        5. GPRINT Type: Exact Numbers (you may prefer Load Average, if available)
        6. Text Format: Temperature
        7. Press “Create”
      9. Finally, create the four items in the graph legend.
        1. “Graph Template Items” -> “Add”
        2. Data Source: Local Temperature – (temp_c)
        3. Graph Item Type: GPRINT
        4. Consolidation Function: LAST
        5. GPRINT Type: Exact Numbers
        6. Text Format: “Current:”
        7. Press “Create”
      10. Repeat this last step 3 more times, replacing the Consolidation Function and Text Format with AVERAGE, MIN, and MAX. On the last item, check Insert a Hard Return
      11. Your Graph Template should now look like:

    Graph Template

    1. Press “Save”

5. Create a Cacti graph

We are finally ready to create a graph.

  1. “Management” -> “Devices” -> Select your Cacti server
  2. Click “Create Graphs for this Host”
  3. “Graph Templates” -> “Create:” -> select “Local Temperature” from pull-down
  4. Press “Create”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s