Giter Club home page Giter Club logo

netatmo2mqtt's Introduction

netatmo2mqtt

Get the measures from your NetAtmo thermostat and send it to your MQTT broker

Why yet another tool around NetAtmo API

I tried many other open source tools on github but I did not find a perfect fit for me.

The main reason is security : most of the tools I reviewed are still asking for client_id, client_secret (perfectly normal) but also for your username / password and that's not acceptable for me. It's also forbidden by NetAtmo guidelines.

The other reason is that the thermostat is synchronized only every hour so simply using getthermostatsdata was not enough for me (make a graph with a measure every hour is not very precise). So I finally used getmeasure in addition to provide all the missing detail with a measure every 10 minutes (like on the website).

Update 2019-01-27 : I replaced getthermostatsdata (which is being deprecated by Netatmo) by homesdata.

So I build mine :(.

Usage

Prerequisite

You simply need Python3 (never tested with Python2.7) and the only dependencies are requests (to access the api) and paho-mqtt (for MQTT broker interaction) so this line should be enough :

pip3 install paho-mqtt requests

Getting your refresh token

First you'll have to create your app in the NetAtmo website and then you can use curl on a server you trust to get your refresh token :

 curl -d 'grant_type=password&client_id=<CLIENT_ID>&client_secret=<CLIENT_SECRET>&username=<USERNAME>&password=<PASSWORD>&scope=read_thermostat' 'https://api.netatmo.net/oauth2/token'

Make sure to add a leading space to this command to avoid keeping this line in your shell history.

About the scope : for this program you only need to be allowed to read thermostat values, you can read the API documentation if you need anything else.

Using the script

Easy, first try a dry-run command :

./netatmo2MQTT.py -c '<CLIENT_ID>' -a '<CLIENT_SECRET>' -r '<REFRESH_TOKEN>' -n -v

and then a real command to add to your crontab :

./netatmo2MQTT.py -c '<CLIENT_ID>' -a '<CLIENT_SECRET>' -r '<REFRESH_TOKEN>'

The secrets can also be set with environment variables, see the help for more detail.

Help

seb@minus ~/src/netatmo2mqtt (git)-[master] # ./netatmo2MQTT.py --help
usage: netatmo2MQTT.py [-h] -a NACLIENTSECRET -c NACLIENTID -r NAREFRESHTOKEN -l LATESTREADINGURL -x LATESTREADINGREGEX [-m HOST] [-n] [-o PREVIOUSFILENAME] [-u UPDATEDREFRESHFILENAME] [-s TOPIC] [-t TOPIC]
                       [-T TOPIC] [-v]

Read current temperature and setpoint from NetAtmo API and send them to a MQTT broker.

options:
  -h, --help            show this help message and exit
  -a NACLIENTSECRET, --client-secret NACLIENTSECRET
                        NetAtmo Client Secret / Can also be read from NETATMO_CLIENT_SECRET env var. (default: None)
  -c NACLIENTID, --client-id NACLIENTID
                        NetAtmo Client ID / Can also be read from NETATMO_CLIENT_ID en var. (default: None)
  -r NAREFRESHTOKEN, --refresh-token NAREFRESHTOKEN
                        NetAtmo Refresh Token / Can also be read from NETATMO_REFRESH_TOKEN en var. (default: None)
  -l LATESTREADINGURL, --latest LATESTREADINGURL
                        Url with latest reading timestamp already stored. (default: None)
  -x LATESTREADINGREGEX, --regex LATESTREADINGREGEX
                        Regular expression to get latest reading time from url. (default: None)
  -m HOST, --mqtt-host HOST
                        Specify the MQTT host to connect to. (default: 127.0.0.1)
  -n, --dry-run         No data will be sent to the MQTT broker. (default: False)
  -o PREVIOUSFILENAME, --last-time PREVIOUSFILENAME
                        The file where the last timestamp coming from NetAtmo API will be saved (default: /tmp/netatmo_last)
  -u UPDATEDREFRESHFILENAME, --updated-refresh UPDATEDREFRESHFILENAME
                        The file where the last refresh token coming from NetAtmo API will be saved (default: /tmp/netatmo_last_refresh)
  -s TOPIC, --topic-setpoint TOPIC
                        The MQTT topic on which to publish the message with the current setpoint temperature (if it was a success) (default: sensor/setpoint)
  -t TOPIC, --topic TOPIC
                        The MQTT topic on which to publish the message (if it was a success). (default: sensor/mainroom)
  -T TOPIC, --topic-error TOPIC
                        The MQTT topic on which to publish the message (if it wasn't a success). (default: error/sensor/mainroom)
  -v, --verbose         Enable debug messages. (default: False)

Other things to know

I personaly use cron to start this program so as I want to keep the latest timestamp received from the API, I store it by default in /tmp/netatmo_last (you can change it through a command line parameter. You can also initialize it with an URL (check the argument -l).

Docker

I added a sample Dockerfile, I personaly use it with a docker-compose.yml like this one :

version: '3'

services:
  netatmo2mqtt:
    build: https://github.com/seblucas/netatmo2mqtt.git
    image: netatmo-python3-cron:latest
    restart: always
    environment:
      NETATMO_CLIENT_SECRET: XXX
      NETATMO_CLIENT_ID: XXX
      NETATMO_REFRESH_TOKEN: XXX
      CRON_STRINGS: "46 * * * * netatmo2MQTT.py -m mosquitto -t sensor/netatmo"
      CRON_LOG_LEVEL: 8

Limits

  • This program only handles Thermostat for now (PR welcome for other sensors)
  • Won't work if you have more than one thermostat (again PR welcome)

License

This program is licenced with GNU GENERAL PUBLIC LICENSE version 3 by Free Software Foundation, Inc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.