Accéder au contenu principal

Get Foobot measurements in Home Assistant using RESTful API

There is no Foobot module yet available in Home Assistant. Nevertheless, Foobot is offering a RESTful API which you can easily access, and which is configurable with the RESTful sensor component in Home Assistant.

The first thing you need to do, is to request a Foobot API key if you don't have one. You can do this here: https://api.foobot.io/apidoc/index.html

On the same API page, you can get your device uuid, by using the Identity API. Just enter your user name, and click "Try it out!":


You will be able to read your uuid form the JSON response body.
You now have all the information necessary to retrieve Foobot sensor values for a specific period of time.

The API allows to get two types of data points: data points read in the last N seconds, or between two specific date and times. As in Home Assistant we would like to read the latest information given by the Foobot, we will use the first one.

Using the Data Point API, we are now able to generate all the information we need for Home Assistant. Just enter your uuid, put 600 seconds for the period (Foobot is doing a reading every 5 minutes, so you should get one or two readings as a result), and 0 as "averageBy", as we don't need to average the results. Click on "Try it out!":


The interesting result here is the generated curl command:

curl -X GET --header "Accept: text/csv;charset=UTF-8" --header "X-API-KEY-TOKEN: your-key-here" "https://api.foobot.io/v2/device/your-uuid-here/datapoint/600/last/0/"

The url (https://api.foobot.io/v2/device/your-uuid-here/datapoint/600/last/0/) is the one you will need to configure the RESTful component in Home Assistant, so save it for later. Remove the --header parameter which is related to the text output (we want JSON, not plain text), and execute the command in a terminal window:

curl -X GET --header "X-API-KEY-TOKEN: your-key-here" "https://api.foobot.io/v2/device/your-uuid-here/datapoint/600/last/0/"

You will get a JSON result, which should look like this:

{"uuid":"your-uuid-here","start":1515866899,"end":1515867210,"sensors":["time","pm","tmp","hum","co2","voc","allpollu"],"units":["s","ugm3","C","pc","ppm","ppb","%"],"datapoints":[[1515866899,16.600006,21.919,42.835,1294,357,46.350006],[1515867210,16.140015,21.926,42.813,1310,362,46.30668]]}

In this result, we get 2 data points which have been measured over the last 10 minutes. Whatever the number of data points returned, we are actually interested in the last result, which can be queried by:
datapoints[-1]

"-1" means here the last record of the datapoints list. You can test and query JSON here: http://jmespath.org/

We can get access to the following values:
"time": seconds since epoch
"pm": "particulate matter" in micrograms per cubic meter of air
"tmp: temperature
"hum": humidity
"co2": co2 in parts per million
"voc": "volatile organic compounds" in parts per billion
"allpollu": overall pollution pourcentage value

So, let's say you want to get the "voc "value, you will have to refer to:
datapoints[-1][5]

Let's go back to Home Assistant configuration. In order to configure the RESTful sensor, you have to add the following section in your "configuration.yaml":

sensor:
- platform: rest
  resource: https://api.foobot.io/v2/device/your-uuid-here/datapoint/600/last/0/
  value_template: '{{ value_json.datapoints[-1][5] }}'
  name: "foobot_voc"
  unit_of_measurement: "ppb"
  scan_interval: 450
  headers:
    X-API-KEY-TOKEN: !secret foobot_api_key

A good practice is to add your API key in "secrets.yaml", so it doesn't appear in clear text in "configuration.yaml". Add the following line in "secrets.yaml":
foobot_api_key: your-api-key-here

The scan interval is defined to 450 seconds, as the max number of queries to the Foobot API is 200 per day, this should be ok.

Save your config files, restart your Home Assistant server, and that's it. You should now have a new state named sensor.foobot_voc, that you can use in your Home Assistant dashboards.


Commentaires

Posts les plus consultés de ce blog

A simple temperature and humidity MQTT sensor for Home Assistant, based on Arduino MKR1000

This article describes a way to create and manage multiple very simple temperature and humidity sensors based on Arduino MKR1000 , and being able to send their information to Home Assistant . The Arduino MKR1000 is a pretty cool device, which has an integrated Wifi chip, optimized for low power consumption. This post is not addressing the power aspect, but keep in mind that a possible enhancement of the presented configuration could be to leverage the low power and sleep mode possibilities, in order to run the sensor on battery, instead of having it constantly plugged in. Without taking that into consideration, my requirements were the following: - To be able to plug in a sensor in each room of my home, connected to my Wifi network; - To read humidity and temperature every few minutes; - To collect all data on a pre-configured Home Assistant server; - To be able to re-connect automatically, whenever an Internet, wifi or power outage occurs (and in my place, you can expect all t...

10 Myths about Google Docs

When discussing around me about Google Docs in the corporate world, I'm very often surprised to see the difference between the people who use it -and generally love it-, and the people who have never used it -and have tons of reasons for not doing so-. Here are the 10 justifications I'm earing the most: 1. Google Spreadsheets do not support complex data analysis: wrong, the function list contains hundreds of functions for Math, Date, Financial, Statistical, and Engineering Analysis. The database functions even allow Google Spreadsheets to query and process data from other databases. It can be fully customized using Google Apps Scripts, based on Javascript. You can create charts in a spreadsheet and publish them on an intranet webpage, so they get updated every time you update the data, in real time. 2. You can’t send a Google Document easily as an attachment in another format: wrong, you can export a document by selecting “Email as attachment...” and select the document...

Introduction of an IT Project Management Methodology in a fast-growing company

This article from the PMI Knowledge Shelf explains the rationale behind the creation of an in-house, customized project management methodology, iMAP, at a rapidly growing pharmaceutical company. The authors also describe the priorities that were selected to start with its implementation: a solid project initiation, a clear project life cycle, a special attention to risk management, a first step in reporting standardization, and specific attention to the activities necessary for the transition to operations. They provide an overview of their approach to IT project governance and how a defined project management methodology is the key for its success. https://docs.google.com/file/d/0B40T8ZW4GEUJNmYxMTJhY2YtODY3Mi00NWEwLWExZDMtMmNlMGNiZTUyZWEw/edit