Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Bruno Santos
Published © CC BY

Luminosity Sensor for Home Assistant

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

IntermediateFull instructions provided1 hour6,913
Luminosity Sensor for Home Assistant

Things used in this project

Hardware components

Wemos D1 Mini
×1
Wemos Battery Shield
×1
Light Dependent Resistor - LDR
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Wiring Schematics

You can go for Option 1 or Option 2

Code

Luminosity_Code

Arduino
This is the code for the Wemos D1 Mini - Just paste it in Arduino IDE and change the values for your needs
/*
 * This code will put the wemos in sleep every 2.5 minuts
 * Hopping it will save battery
 */

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

//#define DEBUG 1
#define DEBUG 0 

// MQTT stuff
#define MQTT_SERVER "HA IP ADDRESS"
#define MQTT_USER "mosquitto_user"
#define MQTT_PASSWORD "mosquitto_password"
#define MQTT_PORT 1883
#define MQTT_TOPIC "sensor/luminosity"

//since we're sleeping, don't need the update time - we're updating when we woke up
#define SLEEP_TIME 180 //3 minutes in seconds 

int lightSensor = A0;

// WIFI
const char *ssid = "wifi_network";
const char *password = "wifi_password";

// MQTT


WiFiClient espClient;
PubSubClient client (espClient);


int lightValue = 0;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  
  //wifi setup
  #if DEBUG
    Serial.println("");
    Serial.println("");
    Serial.print("Connecting to wifi...");
    Serial.println(ssid);
  #endif
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print("."); 
  }
  #if DEBUG
    Serial.println ("");
    Serial.print("WiFi connected: ");
    Serial.println (WiFi.localIP());
  #endif
  
  //mqtt
  client.setServer(MQTT_SERVER, MQTT_PORT);

  if (!client.connected()) {
    //loop until connected
    while (!client.connected()) {
      #if DEBUG
        Serial.println("Connecting to MQTT..");
      #endif
      if (client.connect("LightSensor", MQTT_USER, MQTT_PASSWORD)) {
        #if DEBUG
          Serial.println("Connected!");
        #endif
      }
      else {
        Serial.print("Error rc=: ");
        Serial.println (client.state());
        Serial.println (" retry in 5 seconds...");
        delay(5000);
      }
    }
  }
 
  client.loop();
  //read luminosity
  lightValue = analogRead(lightSensor);
  #if DEBUG
    Serial.print("Light value: ");
    Serial.println (lightValue);
  #endif
  client.publish (MQTT_TOPIC, String(lightValue).c_str(), true);
  //if we don't put a delay, we'll go to sleep before publishing to MQTT
  delay(500);
  ESP.deepSleep (SLEEP_TIME * 1000000);

}

void loop() {
  // put your main code here, to run repeatedly:
  
}

automation.yaml

YAML
This is how I use the sensor value to turn the lights on or not. If value below 450, turn lights on
# Luz #1 garagem -  ligar
# Apenas se luminosidade inferior a 600
- alias: Luz garagem 1 on
  trigger:
    platform: mqtt
    topic: tele/sonoffBridge/RESULT
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{trigger.payload_json.RfReceived.Data == "EE9D2E" }}'
      - condition: numeric_state
        entity_id: 'sensor.Luminosidade_garagem'
        below: 450
  action:
    service: homeassistant.turn_on
    data:
      entity_id: switch.Luz_Garagem_1

sensor.yaml

YAML
Add this lines to create a new sensor. Remember the state_topic must be the same you configured in Arduino code
# Sensor luminosity
- platform: mqtt
  name: "Luminosidade_garagem"
  state_topic: "sensor/luminosity"
  qos: 0
  unit_of_measurement: "cd"

Credits

Bruno Santos

Bruno Santos

6 projects • 4 followers
Linux and Raspberry PI lover! Systems and Network Administrator, programmer, kind of a Nerd!

Comments