Basic question: Light sensors



  • V_LIGHT_LEVEL; does home-assistant support the SI unit LUX? I have a sketch for a LUX sensor I am trying to integrate into HA but it gives a value back in % in the GUI.

    Thanks,


  • Plugin Developer

    V_LIGHT_LEVEL should have the unit percent (%) according to the mysensors serial API. For lux use V_LEVEL and a custom unit of measurement, V_UNIT_PREFIX. Read more here:
    https://home-assistant.io/components/sensor.mysensors/#custom-unit-of-measurement

    See the example sketch here:
    https://home-assistant.io/components/sensor.mysensors/#example-sketch



  • @martinhjelmare

    Thanks martin. I think I got the code handled properly, but i'm getting a compile error.

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Derrick Rockwell (@Drock1985)
     * 
     * DESCRIPTION
     * 
     *
     */
    
    // Enable debug prints
    #define MY_DEBUG
    
    // Enable and select radio type attached
    #define MY_RADIO_NRF24
    //#define MY_RADIO_RFM69
    #define MY_NODE_ID 21
    #define MY_REPEATER_FEATURE //Enables Repeater feature for non-battery powered nodes. 
    #include <SPI.h>
    #include <MySensors.h>
    #include <DallasTemperature.h>
    #include <OneWire.h>
    #include <BH1750.h>
    #include <Wire.h>
    
    
    unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)
    #define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
    #define COMPARE_TEMP 1 // Send temperature only if changed? 1 = Yes 0 = No
    #define ONE_WIRE_BUS 4 // Pin where dallase sensor is connected 
    OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
    DallasTemperature sensors(&oneWire); // Pass the oneWire reference to Dallas Temperature. 
    int lastTemperature = 0;
    boolean receivedConfig = false;
    boolean metric = true;
    
    
    #define CHILD_ID_MOTION 1   // Id of the sensor child
    #define CHILD_ID_TEMP 2   // ID of Temperature Sensor
    #define CHILD_ID_LUX 3  // ID of Lux Sensor
    
    BH1750 lightSensor;
    
    // Initialize  messages
    MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);
    MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
    MyMessage msgLux(CHILD_ID_LUX, V_LEVEL); //Sets up custom units (this case LUX for Light)
    MyMessage msgPrefix(CHILD_ID_LUX, V_UNIT_PREFIX); // Sends controller the LUX value instead of %
    
    uint16_t lastlux = 0;
    
    void setup()  
    {  
      pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input
      lightSensor.begin();
      // Startup up the OneWire library
      sensors.begin();
      // requestTemperatures() will not block current thread
     sensors.setWaitForConversion(false);
      send(msgPrefix.set("Lux"));
      
    }
    
    void presentation()  {
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("LuxMotionTempSensor", "1.0");
    
      // Register all sensors to gw (they will be created as child devices)
      present(CHILD_ID_MOTION, S_MOTION);
      present(CHILD_ID_TEMP, S_TEMP);
      present(CHILD_ID_LUX, S_LIGHT_LEVEL);
    }
    
    void loop()     
    {     
     {     
      uint16_t lux = lightSensor.readLightLevel();// Get Lux value
      Serial.println(lux);
      if (lux != lastlux) {
          send(msgLux.set(lux));
          send(msgPrefix.set("Lux"));
          lastlux = lux;
      }}
    
       // Fetch temperatures from Dallas sensors
      sensors.requestTemperatures();
    
    
        // Fetch and round temperature to one decimal
        int temperature = (((sensors.getTempCByIndex(0)) * 10.)) / 10.;
        #if COMPARE_TEMP == 1
        if (lastTemperature != temperature && temperature != -127.00 && temperature != 85.00) {
        #else
        if (temperature != -127.00 && temperature != 85.00) {
        #endif
          // Send in the new temperature
          send(msgTemp.set(temperature,1));
          // Save new temperatures for next compare
          lastTemperature = temperature;
        }}
        // Read digital motion value
      boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
            
    //  Serial.println(tripped);
      send(msgMotion(tripped?"1":"0"));  // Send tripped value to gw 
    
      // Sleep until interrupt comes in on motion sensor. Send update every two minute.
      sleep(digitalPinToInterrupt(DIGITAL_INPUT_SENSOR), CHANGE, SLEEP_TIME);
    }
    
    

    I'll try it again tomorrow.

    Thank you again for pointing me in the right direction on custom units.


 

266
Online

7.8k
Users

8.7k
Topics

93.0k
Posts