Netatmo Healthy Home Coach Bash Script

All kinds of 'OS' scripts
Post Reply
ben53252642
Posts: 504
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Netatmo Healthy Home Coach Bash Script

Post by ben53252642 » Wednesday 31 January 2018 16:58

This script is an alternative to using the built in Domoticz Netatmo plugin, it gets data from the Netatmo API for the Healthy Home Coach and loads it into Domoticz via the JSON API.

I wrote this because I was having problems with the built in Netatmo Domoticz plugin, if you wish to use it here are the steps:

1) Go here and create an App, you can call it anything you like: https://dev.netatmo.com/myaccount/createanapp

2) Login to your Netatmo account and get the "Client ID" and "Client Secret" from your app.

3) Fill out the Domoticz and Healthy Home Coach Configuraiton sections of the script below

4) Create the Domoticz virtual sensor devices mentioned at the bottom of the script, don't forget to set their IDX numbers obtained from the Domoticz devices page

5) Install required packages using terminal: apt-get install jq bc screen


homecoach.sh

Code: Select all

#!/bin/bash
while true; do

# Healthy Home Coach Configuration
username="" #eg emailaddress@example.com
password="" #your netatmo account password
clientid="" #from your Netatmo app you created
clientsecret="" #from your Netatmo app you created
macaddress="" # format: xx:xx:xx:xx:xx:xx

# Domoticz Configuration
domoticzserver="192.168.0.5"
domoticzport="443"
domoticzuser="USERNAME"
domoticzpass="PASSWORD"

# Get an access token
echo "Getting access token..."
accesstoken=$(curl --silent --max-time 60 -d "grant_type=password&username=${username}&password=${password}&client_id=${clientid}&client_secret=${clientsecret}&scope=read_homecoach" --compressed https://api.netatmo.com/oauth2/token | jq ".access_token" | sed 's/["]*//g')

# Get Healthy Home Coach data using the access token and devices mac address
echo "Getting Healthy Home Coach Data..."
data=$(curl --silent --max-time 60 -d "access_token=${accesstoken}&device_id=${macaddress}" --compressed https://api.netatmo.com/api/gethomecoachsdata)

# Put the received json data into easy to use variables
pressure=$(echo "$data" | jq ".body.devices[0].dashboard_data.Pressure")
noise=$(echo "$data" | jq ".body.devices[0].dashboard_data.Noise")
temperature=$(echo "$data" | jq ".body.devices[0].dashboard_data.Temperature")
humidity=$(echo "$data" | jq ".body.devices[0].dashboard_data.Humidity")
co2=$(echo "$data" | jq ".body.devices[0].dashboard_data.CO2")
netatmotimeutc=$(echo "$data" | jq ".body.devices[0].dashboard_data.time_utc")

# Compare the utc time received to the local systems utc time to see if the data received is current (is the Netatmo working ok?)
systemutc=$(date -u +%s)
current=$(echo "scale=0; ($systemutc - $netatmotimeutc) / 60" | bc -l)
if [ "$current" -lt 15 ]; then
echo "Netatmo data is recent, approx $current minutes old, proceeding..."

# Display data in terminal
echo "Pressure: $pressure"
echo "Noise DB: $noise"
echo "Temperature: $temperature"
echo "Humidity: $humidity"
echo "CO2: $co2"

# Load data into Domoticz
# Domoticz Virtual Sensor Type: Temp+Hum+Baro
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1130&nvalue=0&svalue={$temperature};{$humidity};0;{$pressure};0"
# Domoticz Virtual Sensor Type: Air Quality
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1131&nvalue={$co2}"
# Domoticz Virtual Sensor Type: Sound Level
curl --max-time 60 -k -s "https://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=1132&nvalue=0&svalue={$noise}"

else
echo "Netatmo data is more than 15 minutes old or another problem occured (is the Netatmo working?): $current minutes, aborting..."
fi

echo "Waiting 10 minutes before next update..."
sleep 600
done
Example terminal output:
homecoach2.JPG
homecoach2.JPG (82.22 KiB) Viewed 999 times

I start the main script using screen to run it in the background:

Code: Select all

#!/bin/bash
# Netatmo Healthy Home Coach
/usr/bin/screen -S netatmohomecoach -d -m nice -17 ionice -c2 -n6 /path/to/netatmo/homecoach.sh
Notes:
1) The curl commands in the script are set to accept unsigned SSL certificates for sending data to Domoticz
2) You will need to read, understand and accordingly modify this script for your own environment.
3) Netatmo, if you read this please give us local access to our devices. I really dislike having to use your Cloud API to get access to the devices data, we should be able to get it locally straight from the device.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest