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!":
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
Enregistrer un commentaire