Skip to content

Commit

Permalink
Make temperature units configurable in settings (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
GavinLucas authored Nov 12, 2024
1 parent f8b0e60 commit 1c2431e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ https://github.com/GavinLucas/HueToInflux

Script to take data from a Hue Bridge and post it to InfluxDB in order to view the data in Grafana.

Currently, it collects presence, temperature and light readings from Hue Motion Sensors and the brightness
of lights, but could be modified to collect other data from the Hue Bridge.
Currently, it collects occupancy, temperature and light readings from Hue Motion Sensors, on/off state
of Smart Plugs and the brightness of lights, but could be modified to collect other data from the Hue Bridge.

- Temperature data uses the units specified in the settings file
- Light level is in lux
- Occupancy (movement) is output as boolean 0 or 1 (1 for movement detected)
- Smart Plugs state is also output as boolean 0 or 1 (1 for on)
- Brightness of dimmable lights is output as a percentage

To create a username and password for the Hue Bridge, follow the instructions
here: https://developers.meethue.com/develop/get-started-2/
Expand All @@ -18,6 +24,8 @@ To run the script:
by other users
- Fill in the values for your Hue Bridge and InfluxDB
- Set the 'interval' to the number of seconds between each data collection
- Set "temperature_units" to "C" (Celsius), "F" (Fahrenheit), or "K" (Kelvin) to convert the temperature
to the desired units
- The 'sensors' section is for remapping the names of sensors if you're not happy with the names in the
Hue settings. This is optional
- Install the requirements with `pip install -r requirements.txt`
Expand Down
17 changes: 12 additions & 5 deletions huetoinflux.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ def device_name_to_name(device_name):
:return: name
:rtype: str
"""
# if "sensors" in settings and settings["sensors"].get(device_name):
# name = settings["sensors"].get(device_name)
# else:
# name = device_name
name = settings["sensors"].get(device_name, device_name) if "sensors" in settings else device_name
return name.replace(" ", "_")

Expand All @@ -87,14 +83,25 @@ def parse_data():
for device in hue_data["sensors"].values():
name = device_name_to_name(device["name"])
if device["type"] == "ZLLTemperature":
data[name] = round(device["state"]["temperature"] / 100, 2)
# convert temperature to the desired units
celsius = device["state"]["temperature"] / 100
if settings.get("temperature_units") == "F":
data[name] = round((celsius * 1.8) + 32, 2)
elif settings.get("temperature_units") == "K":
data[name] = round(celsius + 273.15, 2)
else:
data[name] = round(celsius, 2)
elif device["type"] == "ZLLLightLevel":
# convert light level to lux
data[name] = round(float(10 ** ((device["state"]["lightlevel"] - 1) / 10000)), 2)
elif device["type"] == "ZLLPresence":
# convert presence to boolean 0 or 1
data[name] = int(1 if device["state"]["presence"] else 0)

for device in hue_data["lights"].values():
name = device_name_to_name(device["name"])
# convert brightness to percentage if the light is dimmable (has a "bri" attribute)
# otherwise boolean 0 or 1 to cover smart plugs which are also listed as lights
data[name] = int(device["state"].get("bri", 2.54) / 2.54) if device["state"]["on"] else 0

return data
Expand Down
1 change: 1 addition & 0 deletions settings.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"timeout": 5
},
"interval": 300,
"temperature_units": "C",
"sensors": {
"Hue ambient light sensor 1": "Room1_Light_Sensor",
"Hue ambient light sensor 2": "Room2_Light_Sensor",
Expand Down

0 comments on commit 1c2431e

Please sign in to comment.