Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config for external temperature source #314

Closed
3 tasks
nicknicknickos opened this issue Mar 18, 2018 · 2 comments
Closed
3 tasks

Config for external temperature source #314

nicknicknickos opened this issue Mar 18, 2018 · 2 comments
Labels

Comments

@nicknicknickos
Copy link

nicknicknickos commented Mar 18, 2018

See below for a working setup to pipe in the temperature for a Phillips Hue motion sensor into the temperatureFilePath that was introduced in v3.0.5. This is written from the perspective of a Raspberry Pi user, but it should work on any Unix based system

Prerequisites:

1 - Get your API data:
Register for an API with Hue using the above link. Then, you'll need to find the specific sensor that you want to get data from. This will look something like http://<HUE_BRIDGE_IP_ADDRESS>/api/<USER_ID>/sensors.
If you have multiple sensors, you'll need to find which one is the right one from the JSON response (try looking at the name of the sensor vs the name in the hue app). Once you have found it, add the number to the end of your URL
(like this http://<HUE_BRIDGE_IP_ADDRESS>/api/<USER_ID>/sensors/**5**.
If you paste this URL in your browser, you will get the response for one motion sensor only, which is what we want.

2 - Get your system ready
Install the 'requests' package for python on the Pi (or whatever machine you're using) with apt-get install python-requests

3 - Edit this python script with your Hue API data
Replace 'http://HUE_BRIDGE_IP_ADDRESS/api/USER_ID/sensors/NUMBER' with your required information

import requests
import json
response = requests.get('http://HUE_BRIDGE_IP_ADDRESS/api/USER_ID/sensors/NUMBER')
json_data = json.loads(response.text)
temp = int(json_data['state']['temperature']) /100
f = open( '/var/homebridge/temp', 'w' )
f.write(str(temp))
f.close()

Save this file as 'temp.py'. This script looks at the data from the API call, parses the rest of the JSON out to have just the temperature data (which is a 4 digit number, 2476 = 24.76C degrees), divides the 4 digit number by 100 to get a 2 digit number, then adds it to a file called 'temp' which lives /var/homebridge (this should create the file called 'temp' if it does not already exist).

Unfortunately this does not round the numbers very well. For example, if the response from the API is 2499 (24.99 degrees C), then the 2 digit number will be '24', not '25'. This can be fixed with 'round' or 'float' in the python, but I haven't done that yet (maybe v2).

In terminal, navigate to where temp.py is located (e.g. cd /var/homebridge, then run chmod a+x temp.py.

4 - Set up a cronjob to make the python script run every minute
On your Pi run 'crontab -e'. You may see a few options for where you want cron to run from, I don't think it maters which one you pick. I set this script to run every minute using * * * * * /var/homebridge/temp.py. You can use this handy website to find how to use cron and what options to use for different times. Using '* * * * *' means it will run every minute.

5 - Edit you config.json file
In your config.json file, the path for 'temperatureFilePath' should be "/var/homebridge/temp".

That's it! You should now have your temperature value piping into your Air Conditioner. Comment if you have any troubles and I'll do my best to help. Thanks to @lprhodes for the plugin!

@lprhodes lprhodes changed the title (Solution provided) - Config for external temperature source (issue 244) Config for external temperature source Mar 20, 2018
@spaghetti-
Copy link

spaghetti- commented Aug 6, 2019

If you want to get rid of python you can just install jq and create a small shell script that does the same:

#!/bin/sh
/usr/bin/curl -s \
  http://$HOMEBRIDGE_IP/api/$HOMEBRIDGE_USERNAME/sensors/$TEMP_SENSOR_NUMBER | \
  /usr/bin/jq '.state.temperature / 100' | \
  tee /tmp/bedroom-temperature

and put it in crontab (crontab -e) then read from that file.

@Cylus7
Copy link

Cylus7 commented Mar 25, 2020

Thanks for your post- I have got this working.

I altered the python script to show a the temperature with a figure behind the decimal point (temp = round(temp, 1) inserted above 'f = open...').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants