Voltage value range sets "day" or night"?

Hi everyone!

Just looking for a pointer in the right direction.

I have a photo transistor connected to an adc pin on an esp32 devboard. It sends a voltage value based on the amount of light it receives. 0-0.5V in the dark and 1.0-3.9V in bright light. For future automations, I’d like to eval these voltage ranges to report “day” or “night”. Is this possible, or do I just hard code these ranges within future automations?

Assuming that you want to do this in ESPHome (so not in Home Assistant) an option to try is: create a Select Template that can be in one of three states, and add some on_value_range select to your ADC sensor:

# Select setup
select:
  - platform: template
    name: "Time of day"
    id: tod
    optimistic: true
    options:
      - night
      - dusk
      - day
    initial_option: dusk

# ADC Sensor with range selections
sensor:
  - platform: adc
    pin: A0
    name: "Living Room Brightness"
    update_interval: 5s
    on_value_range:
      - above: 0.5
        below: 1.0
        then:
          - select.set:
              id: tod
              option: "dusk"
      - below: 0.5
        then:
          - select.set:
              id: tod
              option: "night"
      - above: 1.0
        then:
          - select.set:
              id: tod
              option: "day"

The state of this Select Template can then be used further in automations.

2 Likes

Thank you!!!

I know this is kind of old but, light conditions can vary a lot over the course of a day from weather, someone closing blinds and turning off lights, someone accidentally shading the LDR by putting something in front of it or whatever. LDR sensors work well for lighting automation because they will register the brightness and turn on the lights when its dark but if you need a sensor that can establish the time of day (Day, Night, Dusk, etc) why wouldn’t you just use the actual time of the day. You can pull time from NTP servers, Your local network, HA, or a Real time Clock module attatched to the esp32. An LDR seems like a really poor choice for this task unless you pair it with other conditions and sensors to get better accuracy. Light levels are nearly identical during dusk and dawn so how does this Select template and an LDR distinguish those two ?