From 45ea6ea654702152a716549a2f8ecce42e6ead3d Mon Sep 17 00:00:00 2001 From: Gavin Lucas Date: Tue, 12 Nov 2024 23:56:05 +0000 Subject: [PATCH] Make temperature units configurable in settings --- README.md | 12 ++++++++++-- huetoinflux.py | 17 ++++++++++++----- settings.json.example | 1 + 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d262e7e..cf18f70 100644 --- a/README.md +++ b/README.md @@ -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/ @@ -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` diff --git a/huetoinflux.py b/huetoinflux.py index ca928ba..3d2d379 100644 --- a/huetoinflux.py +++ b/huetoinflux.py @@ -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(" ", "_") @@ -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 diff --git a/settings.json.example b/settings.json.example index 6fa2127..ef5fe40 100644 --- a/settings.json.example +++ b/settings.json.example @@ -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",