LDR brightness meter script for RPi

Here is my first Python script ever. It reads an GPIO connected LDR photoresistant sensor and provides the data to Home Assistant. This can be done on the HA system self, but it can also be used on a headless RPi. It can report the value to the console or to an API endpoint of a HA system. It can report the value 1 or multiple times, depending on the input.

usage: ha_ldr.py [-h] [-p PIN] [-m MAX] [-l LOOP] [-u URL] [-s SLEEP]
             [-f LOGFILE] [-v]

optional arguments:
  -h, --help            show this help message and exit
  -p PIN, --pin PIN     LDR GPIO data pin
  -m MAX, --max-value MAX
                    Max brightness value reported
  -l LOOP, --max-loop LOOP
                    Max number of loops, 0 for indefinate
  -u URL, --url URL     Url to post the value to, if not provided the output
                    is console
  -s SLEEP, --sleep SLEEP
                    Number of seconds to wait for next brightness
                    retreival
  -f LOGFILE, --log-file LOGFILE
                    Log file location
  -v, --verbose         Show verbose logging

Command line Sensor

I have not tested this, please let me know if there are any issues.

sensor:
  - platform: command_line
    name: Brightness
    command: "python3 /path/to/ha_ldr.py -l 1"
    # The lower the number reported, the higher the lumen
    unit_of_measurement: "Units"

Headless RPi

This is how i use this script.

* * * * * root python3 /path/to/ha_ldr.py -l 5 -s 10 -u "http://homeassistant:8123/api/states/input_number.brightness?api_password=yourapikey" -v -f "/var/log/ha_ldr.log"

Link

2 Likes

There is an update to this script, from now on it will be able to handle bearer tokens.

https://github.com/Skons/ha_ldr