Tweeting my house’s power usage as sparkblocks

Sparkblocks are a text-only version of the sparkline charts, using just unicode symbols. These are a convenient way to represent trends in pure text, quite useful when you want to use SMS, tweets or even the subject line of an email to show a story. Those of you following me on Twitter may have seen recent tweets of mine that look like this:

Home power consumption for the last 7 days: ▆▅▆▇▅▃▁

Edit: I created a dedicated Twitter account for my house, folow it here:

Here is how I did that:

First, I grabbed and installed a Python library for generating unicode text based sparkline bargraphs from lists of numbers. The Readme contains all you need to get the job done in 30 or less seconds.

Next, I registered with PushingBox – a neat service for cloud notifications. I created a simple scenario that would post a tweet with the sparkblocks for me, so that I keep my Python code as simple as possible:


The scenario receives an unique identifier called “DeviceID”, write this down for later use.

I log my power usage to emoncms, storing my kWhd data in feed number 5. I will be fetching week’s worth of data from that feed, converting it to text sparkblock and tweeting it using a simple Python script:

import time
import urllib
import json
from sparkblocks import spark #
import urllib, urllib2
from urllib import quote_plus

API= "**********API***********" # emoncms API key
feedID=5                                # emoncms feed id that contains the kwh/d data
deviceID = "****DeviceID******"   #PushingBox deviceID

sec_per_day = 60 * 60 * 24
end = (int(time.time()))                 #now
start = (end - (7 * sec_per_day))        #7 days ago

data_url = "" + API +"&id=" + str(feedID) + "&start=" + str(start*1000) + "&end=" + str(end*1000) + "&dp="

#urllib's urlencode doesn't take utf8, so use a custom one
def urlencode_utf8(params):
    if hasattr(params, 'items'):
        params = params.items()
    return '&'.join(
        (quote_plus(k.encode('utf8'), safe='/') + '=' + quote_plus(v.encode('utf8'), safe='/')
            for k, v in params))

def pushingbox(key,val):
    url = ''
    values = {'devid' : key, 'msg' : val}
      data = urlencode_utf8(values)
      req = urllib2.Request(url, data)
      sendrequest = urllib2.urlopen(req)
    except Exception, detail:
      print "Error ", detail

# read in the data from emoncms
        sock = urllib.urlopen(data_url)
        data_str =
except Exception, detail:
        print "Error ", detail

data = json.loads(data_str)

sparkval=[]     #A list to hold the sparkline values
data.reverse()  #Reversed order, as we want newest data last in the sparkline
for i in data:
        sparkval.append(float(data[x][1]))  #first parameter is the timestamp, ignoring it
        #print float(data[x][1])


Finally, I set a cron job to run this every Sunday just before midnight.


Neat, right?

Page views: 8115

One thought on “Tweeting my house’s power usage as sparkblocks

  1. Pingback: In-memory SQL analysis of emonCMS feed | Martin's corner on the web

Comments are closed.