{"id":13551250,"url":"https://github.com/alin23/lunarsensor","last_synced_at":"2025-07-03T02:34:49.351Z","repository":{"id":37703880,"uuid":"465285742","full_name":"alin23/lunarsensor","owner":"alin23","description":"Server that mimics a Lunar ambient light sensor, with support for multiple lux data sources","archived":false,"fork":false,"pushed_at":"2025-05-12T18:54:41.000Z","size":51,"stargazers_count":39,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-06-09T05:08:42.932Z","etag":null,"topics":["iot","monitor"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alin23.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2022-03-02T11:55:02.000Z","updated_at":"2025-05-29T14:54:32.000Z","dependencies_parsed_at":"2023-11-24T16:26:28.115Z","dependency_job_id":"c689765e-e1bc-4830-863b-16475adb5ed9","html_url":"https://github.com/alin23/lunarsensor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/alin23/lunarsensor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alin23%2Flunarsensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alin23%2Flunarsensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alin23%2Flunarsensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alin23%2Flunarsensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alin23","download_url":"https://codeload.github.com/alin23/lunarsensor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alin23%2Flunarsensor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263247888,"owners_count":23436934,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["iot","monitor"],"created_at":"2024-08-01T12:01:44.998Z","updated_at":"2025-07-03T02:34:49.307Z","avatar_url":"https://github.com/alin23.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://lunar.fyi/\"\u003e\u003cimg width=\"128\" height=\"128\" src=\"https://static.lunar.fyi/svg/lunar.svg\"\u003e\u003c/a\u003e\n  \u003ch1 align=\"center\"\u003e\u003ccode\u003eLunar Sensor\u003c/code\u003e\u003c/h1\u003e\n  \u003ch4 align=\"center\"\u003eCreate a custom Ambient Light Sensor on any device\u003c/h4\u003e\n\u003c/p\u003e\n\n---\n\nThis is a server that implements a custom Ambient Light Sensor for adapting monitor brightness with the [Lunar macOS app](https://lunar.fyi/).\n\n### Requirements\n\n* Python 3.6+\n\n### Running the server\n\n```sh\n# Installs dependencies and runs the server\nmake\n\n# Runs the server without installing dependencies\nmake run\n\n# If IPv6 is not available use HOST\nmake run HOST=0.0.0.0\n\n# Listen on another port using the PORT variable\nmake run PORT=8080\n```\n\n---\n\n### Implementing light sensor reading\n\nThe file `lunarsensor.py` contains a server that reads [lux](https://en.wikipedia.org/wiki/Lux) values using the [`read_lux()`](lunarsensor.py#L53-L57) function at the bottom of the file.\n\nYour actual sensor reading logic should be written in that function.\n\n#### Testing the server\n\n* Check if one-shot lux reading works\n\n```sh\n❯ curl lunarsensor.local/sensor/ambient_light\n\n{\"id\":\"sensor-ambient_light\", \"state\":\"0 lx\", \"value\":0.000000}\n```\n\n* Check if the EventSource is sending lux values every 2 seconds\n\n```sh\n❯ curl -N lunarsensor.local/events\n\nevent: state\ndata: {\"id\": \"sensor-ambient_light\", \"state\": \"400.0 lx\", \"value\": 400.0}\n\nevent: state\ndata: {\"id\": \"sensor-ambient_light\", \"state\": \"400.0 lx\", \"value\": 400.0}\n\nevent: state\ndata: {\"id\": \"sensor-ambient_light\", \"state\": \"400.0 lx\", \"value\": 400.0}\n```\n\n---\n\n### Implementation examples\n\n#### 1. [HomeAssistant addon](homeassistant_addon)\n\nAn easily installable addon that runs the server on port 8899 and polls a sensor entity for lux values.\n\n[![Open your Home Assistant instance and show the Supervisor add-on store.](https://my.home-assistant.io/badges/supervisor_store.svg)](https://my.home-assistant.io/redirect/supervisor_store/)\n\n![adding the lunarsensor repo to HomeAssistant addons](https://files.lunar.fyi/ha-addon-lunar-repo-adding.png)\n\n#### 2. Reading from a [BH1750](https://learn.adafruit.com/adafruit-bh1750-ambient-light-sensor) I²C sensor\n\n```sh\npip3 install adafruit-circuitpython-bh1750\n```\n\n```python\n# Do the sensor reading logic below\n\nimport board\nimport adafruit_bh1750\n\ni2c = board.I2C()\nsensor = adafruit_bh1750.BH1750(i2c)\n\ndef dynamic_adjust_resolution(lux):\n    if lux \u003e 300:\n        sensor.resolution = adafruit_bh1750.Resolution.LOW\n    elif lux \u003e 20:\n        sensor.resolution = adafruit_bh1750.Resolution.MEDIUM\n    else:\n        sensor.resolution = adafruit_bh1750.Resolution.HIGH\n\nasync def read_lux():\n    lux = sensor.lux\n    dynamic_adjust_resolution(lux)\n\n    return lux\n```\n\n#### 3. Reading from a [VEML7700](https://learn.adafruit.com/adafruit-veml7700) I²C sensor\n\n```sh\npip3 install adafruit-circuitpython-veml7700\n```\n\n```python\n# Do the sensor reading logic below\n\nimport board\nimport adafruit_veml7700\n\ni2c = board.I2C()\nsensor = adafruit_veml7700.VEML7700(i2c)\n\ndef dynamic_adjust_resolution(lux):\n    if lux \u003e 300:\n        sensor.light_integration_time = adafruit_veml7700.ALS_25MS\n        sensor.light_gain = adafruit_veml7700.ALS_GAIN_1_8\n    elif lux \u003e 100:\n        sensor.light_integration_time = adafruit_veml7700.ALS_50MS\n        sensor.light_gain = adafruit_veml7700.ALS_GAIN_1_4\n    elif lux \u003e 20:\n        sensor.light_integration_time = adafruit_veml7700.ALS_100MS\n        sensor.light_gain = adafruit_veml7700.ALS_GAIN_1\n    elif lux \u003e 10:\n        sensor.light_integration_time = adafruit_veml7700.ALS_200MS\n        sensor.light_gain = adafruit_veml7700.ALS_GAIN_1\n    else:\n        sensor.light_integration_time = adafruit_veml7700.ALS_400MS\n        sensor.light_gain = adafruit_veml7700.ALS_GAIN_2\n\nasync def read_lux():\n    lux = sensor.lux\n    dynamic_adjust_resolution(lux)\n\n    return lux\n```\n\n#### 4. Reading from a [HomeAssistant](https://developers.home-assistant.io/docs/api/rest/) lux sensor\n\n```python\n# Do the sensor reading logic below\n\nHOME_ASSISTANT_URL = \"http://homeassistant.local:8123\"  # Replace with your HomeAssistant server URL\nTOKEN = \"your.jwt.token\"  # Replace with your long-lived HomeAssistant API token\nSENSOR_ENTITY_ID = \"sensor.living_room_ambient_light\"  # Replace with your sensor entity id\n\nasync def read_lux():\n    async with CLIENT.get(f\"{HOME_ASSISTANT_URL}/api/states/{SENSOR_ENTITY_ID}\", headers={\"Authorization\": f\"Bearer {TOKEN}\"}) as response:\n        sensor = await response.json()\n        if not json:\n            return None\n\n        return float(sensor[\"state\"])\n```\n\n#### 5. Reading from a [Phillips Hue API](https://developers.meethue.com/develop/get-started-2/) motion sensor\n\n```python\nimport math\n\n# Do the sensor reading logic below\n\nHUE_API_URL = os.getenv(\"HUE_API_URL\") # set env variable to: http://\u003cbridge_ip_address\u003e/api/\u003cuser_id\u003e\nHUE_SENSOR_ID = os.getenv(\"HUE_SENSOR_ID\") # send GET request to $HUE_API_URL/sensors and search for \"Hue ambient light sensor\"\n\nasync def read_lux():\n    async with CLIENT.get(f\"{HUE_API_URL}/sensors/{HUE_SENSOR_ID}\") as response:\n        sensor = await response.json()\n        if not json:\n            return None\n        \n        # Converting light level measured by sensor: 10000*log10(lux)+1\n        lightlevel = sensor['state']['lightlevel']\n        lux = lightlevel - 1\n        lux = lux / 10000\n        lux = math.pow(10, lux)\n\n        return float(lux)\n```\n\n\n### Running in Docker\n\nExample for starting on `localhost:8080`\n\n```shell\ndocker build -t lunarsensor .\ndocker run -p 127.0.0.1:8080:80 -d lunarsensor \n```\n---\n\n### Pointing Lunar to the sensor server\n\nLunar expects to find the sensor at the `lunarsensor.local` address by default.\n\nThis can be changed using the `defaults` command on the Mac where Lunar is running.\n\nThere are three settings that affect where Lunar looks for the sensor:\n\n- `sensorHostname` set by default to `lunarsensor.local`\n- `sensorPort` set by default to `80`\n- `sensorPathPrefix` set by default to `/`\n\nFor example, if you would like to have Lunar listen for sensor events at `homeassistant.local:8123/lunar/events` you would run the following commands:\n\n```sh\ndefaults write fyi.lunar.Lunar sensorHostname homeassistant.local\ndefaults write fyi.lunar.Lunar sensorPort 8123\ndefaults write fyi.lunar.Lunar sensorPathPrefix /lunar\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falin23%2Flunarsensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falin23%2Flunarsensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falin23%2Flunarsensor/lists"}