Search

Luminosity Sensor For Home Assistant

About the project

Make a luminosity sensor with a Wemos D1 mini and interface it with your HA. Prevent your lights to turn on if sufficient luminosity

Project info

Difficulty: Moderate

Platforms: Arduino

Estimated time: 1 hour

License: Creative Commons Attribution CC BY version 4.0 or later (CC BY 4+)

Items used in this project

Hardware components

Wemos D1 Mini Wemos D1 Mini x 1
Light Dependent Resistor Light Dependent Resistor x 1
Resistor 10K Ohm 1/4 Watt PTH - 20 pk Resistor 10K Ohm 1/4 Watt PTH - 20 pk x 1
Wemos Battery Shield Wemos Battery Shield x 1

Software apps and online services

Arduino IDE Arduino IDE

Story

Why the sensor

Every time I arrived home at night, in the garage, I wasn't able to see anything. Having a little baby, was always hard for me or my wife, to get out of the car and take the baby out in the dark. We need to turn the lights on.

So, I started automating my house and one of the things I did was to put a shelly 2.5 connected to the lights switch. Using a motion sensor, everything connected to Home Assistant, the lights turn on everything motion was detected.

But, this was happening either at night or day. To prevent turning the light in the day, I've decided to create a luminosity sensor, interface it with Home Assistant and only turn the light if the luminosity was bellow a certain value.

It had to be small, cheap and easy for anyone to create, thus this project was born.

Building it

To build it, just follow the schematics, choose Option 1 or Option 2 - or come up with one yourself to power the Wemos

Here's one in a breadboard and with a 3D printed case

Deep Sleep

To save battery, the code uses the Wemos deep sleep mode to save battery.

For deep sleep to work, you need to put a blob of solder in the Wemos were it says "sleep".

The solder blob to allow deep sleep

WARNING: After you put the solder blob, uploading sketches to the Wemos becomes much harder. It is possible, but involves having to reset the board a lot of times.

Only do this after uploading the code if you don't want to have to reset the board a lot of times.

Code

The code doesn't have a lot of configurations. Just fill the following parameters to reflect your configurations

  • MQTT_SERVER
  • MQTT_USER
  • MQTT_PASSWORD
  • MQTT_PORT (if not standard)
  • MQTT_TOPIC (you can put whatever you want here, but in home assistant configuration must be the same)
  • *ssid - wifi network name
  • *password - wifi password
  • SLEEP_TIME (in seconds): The frequency of updates to HA

Home Assistant

In Home Assistant, we just need to add a new sensor and I'm going to show how you can get the values from the new sensor.

The following lines are to be added to sensor.yaml file (or configuration.yaml if you don't have them separated).

  1. # Sensor luminosity
  2. - platform: mqtt
  3. name: "Luminosidade_garagem"
  4. state_topic: "sensor/luminosity"
  5. qos: 0
  6. unit_of_measurement: "cd"

Restart Home Assistant after adding the sensor.

And here's an example on how to get the values from the sensor

  1. - condition: numeric_state
  2. entity_id: 'sensor.Luminosidade_garagem'
  3. below: 450

And here's the sensor values in Home Assistant entity

Sensor values history

Problems

If you don't see any values, after you power on the sensor, check in Home Assistant Mosquitto broker logs if it's getting there:

  1. 1568671110: New connection from 192.168.2.72 on port 1883.
  2. 1568671110: New client connected from 192.168.2.72 as LightSensor (p2, c1, k15, u'mosquittouser').

Final notes

You can adjust, in the Arduino code, the frequency in which the sensor sends updates on the luminosity (and consequentially the sleep time) to HA.

What can and will happen is, sometimes the sensor just finished sending an update (let's say 900) and your sensor detects movement and the lights don't turn on because there's enough luminosity. But, the luminosity drops to almost pitch black, but the lights don't turn on because the last update was a value of 900 and the sensor hasn't updated the HA.

You can play with the time to best suit your needs.

TODO

I need to design a better case for the sensor

Sensor case

Schematics and circuit diagrams

Wiring Schematics

You can go for Option 1 or Option 2

Code

Luminosity_Code

This is the code for the Wemos D1 Mini - Just paste it in Arduino IDE and change the values for your needs

automation.yaml

This is how I use the sensor value to turn the lights on or not. If value below 450, turn lights on

sensor.yaml

Add this lines to create a new sensor. Remember the state_topic must be the same you configured in Arduino code

Credits

Photo of feiticeir0

feiticeir0

Linux and Raspberry PI lover!Systems Administrator, programmer, kind of a Nerd! Love to create things that may or not work, but could save the world someday ! 🤓

   

Leave your feedback...