Adding a sensor to an automation?

I have an automation for a light that looks like the following (note – some stuff has been deleted to make this simpler to understand).

  alias: Kitchen Sink Light On
  trigger:
  - platform: time
    at: '18:45:00'
  - platform: sun
    event: sunset
  condition:
  - condition: time
    after: '18:45:00'
  - condition: sun
    after: sunset
  action:
  - type: turn_on

The automation works fine – the light comes on at the later of 6:45 pm or sunset. Thanks to some help from folks here, I was able to put together a luminance sensor and would like to include its value in the condition. Specifically, I would like the light to trigger:

  • The later of 6:45 pm or sunset
  • After 6:45 pm and before sunset if the luminance is less than some value

Not sure how to include this – would you please offer some guidance.

You can use a Numeric state condition for your luminance sensor:

 alias: Kitchen Sink Light On
  trigger:
  - platform: time
    at: '18:45:00'
  - platform: sun
    event: sunset
  condition:
  - condition: time
    after: '18:45:00'
  - or:
    - condition: numeric_state
      entity_id: sensor.luminance
      below: X
    - condition: sun
      after: sunset
  action:
  - type: turn_on

The first thing I did was create a trivially simply automation to turn on a test device after a certain time and that worked. I then tried your code, but it did not work. Here are some more details:

This is a simplified copy of the device definition (copied from ESPHome UI, clicked Edit under the device’s name)

esphome:
  name: luxsensor
sensor:
  - platform: bh1750
    name: "Light Level"

This is copied directly from automations.yaml.

 alias: TestLumAuto
  trigger:
  - platform: time
    at: '12:20:00'
  condition:
  - condition: numeric_state
    entity_id: sensor.light_level
    below: '20'
  - condition: time
    before: 00:00:00
    after: '12:00:00'
  action:
  - type: turn_on
    entity_id: switch.testdev_basement_switch
    domain: switch

What am I doing wrong?

I have a thought about the issue: When I looked at the log for the sensor, I saw things like "‘Light Level’: Got illuminance=32.3lx’. If the ‘x’ is part of the value being return, I understand why a simple mathematical comparison would fail. If this is the case, what can I do to resolve the issue, i.e., how can I compare the return value to some other value?

Check the state of the sensor in the Developer Tools > States tool.

Here is what I found:
sensor.light_level
Light Level 17.6
state_class: measurement
unit_of_measurement: lx
device_class: illuminance
friendly_name: Light Level

That looks perfectly fine, but your time condition looks off. before: 00:00:00 doenst look right to me. Try removing the line? Have you checked the automation trace to see if the automation is triggered and what conditions succeed or not?