Skip to content
Jan Pecinovsky edited this page Oct 10, 2016 · 3 revisions

Getting started with Smappy - The Python API wrapper for Smappee

Overview of steps

  1. Buy and install smappee – the device to measure your gas, water and electrical power consumption
  2. Get the Client Id and Client Secret by e-mail from support@smappee.com
  3. Install Python3
  4. Install the Smappy package with pip3
  5. Run a test Python script
  6. Use Pandas to do data analysis

1. Buy and install Smappee

Once your smappee device is installed and online connected to the Smappee server (currently running on AWS) you can access the data from the server (and not from the gateway) via this REST API. If you want to access the Smappee gateway in your local network directly please check out this

2. Get the Client Id and Client Secret

In order to access the REST API you need 4 things

  • a) your Smappee user name from your Smappee app (iOS or Android)
  • b) your Smappee user password from your Smappee app
  • c) Client Id for identifying the origin of your API calls at the Smappee server
  • d) Client secret for initially getting an access token from the Smappee server via OAuth2

a) and b) are under your own control from the set-up of Smappee. Whereas c) and d) you receive by (unencrypted!) e-mail from support@smappee.com

3. Install Python3

Get your distribution from python.org or use the Anaconda installer. Python3 comes as the standard Python flavour on Ubuntu 16 and onwards.

4. Install Smappy package with pip

sudo pip3 install smappy or sudo pip install smappy depending on your pip installation

5. Run a test Python script

Start Python3 from your shell and enter the following commands line by line. You have to replace client id, client secrect, user name, user password with yours:

$ python3
>>> import smappy
>>> s = smappy.Smappee("client id", "client secret")
>>> s.authenticate("user name", "user password")
>>> service_location_id = s.get_service_locations()['serviceLocations'][0]['serviceLocationId']
>>> print ("id: ", service_location_id)
id:  314

In this case the authorization worked if you get a valid service_location_ID. I have picked the first smappee device in the service location list. get_service_locations returns a dict which contains a list of all possible service locations associated with your Smappee account.

>>> s.get_service_locations()
{'appName': 'MyFirstApp', 'serviceLocations': [{'name': 'Home', 'serviceLocationId': 314}]}

If you have multiple smappee devices you have to use the right index instead of [0].

And now we can get data. Here is how you get data from the last week:

>>> import datetime as dt
>>> end = dt.datetime.utcnow()
>>> start = end - dt.timedelta(weeks=1)
>>> s.get_consumption(service_location_id=service_location_id, start=start, end=end, aggregation=3)
{'consumptions': [{'solar': 0.0, 'consumption': 3970.4, 'alwaysOn': 15131.0, 'timestamp': 1475445600000}, 
{'solar': 0.0, 'consumption': 12245.3, 'alwaysOn': 14976.0, 'timestamp': 1475532000000}, 
{'solar': 0.0, 'consumption': 12717.7, 'alwaysOn': 15288.0, 'timestamp': 1475618400000}, 
{'solar': 0.0, 'consumption': 6538.7, 'alwaysOn': 18818.0, 'timestamp': 1475704800000},
{'solar': 0.0, 'consumption': 10552.6, 'alwaysOn': 20552.0, 'timestamp': 1475791200000}, 
{'solar': 0.0, 'consumption': 7814.7, 'alwaysOn': 21457.0, 'timestamp': 1475877600000}, 
{'solar': 0.0, 'consumption': 7101.0, 'alwaysOn': 17550.0, 'timestamp': 1475964000000}], 'serviceLocationId': 314} 

Now you can start your real project…

6. Use Pandas for your data analysis

You can get your consumption as a Pandas Dataframe, so you can immediately start analysing your data!

>>> df = s.get_consumption_dataframe(service_location_id=service_location_id, start=start, end=end, aggregation=3)
>>> df
                           alwaysOn  consumption  solar
timestamp                                              
2016-10-04 00:00:00+02:00   13153.0       7895.9    0.0
2016-10-05 00:00:00+02:00   13248.0      10286.3    0.0
2016-10-06 00:00:00+02:00   13248.0       6094.5    0.0
2016-10-07 00:00:00+02:00   13248.0      10009.8    0.0
2016-10-08 00:00:00+02:00   13478.0       9107.1    0.0
2016-10-09 00:00:00+02:00   13824.0       6477.5    0.0
2016-10-10 00:00:00+02:00   10965.0       3268.5    0.0
>>> df.consumption.mean()
7591.3714285714286
>>> df.consumption.sum()
53139.599999999999
>>>