Automation - Sunrise, Sunset and Light Values

Hi,
Have developed the following automation script, but it is not firing like I expect. I am not sure what is wrong. Essentially, I would like a light to turn on when motion in a room is detected. the light should only operate between sunrise and sunset or when the light values are below a certain level.
I have the script working fine when I just had the sunrise/Sunset conditions, but since adding the Light sensor it wont fire anymore. The light sensor is a LDR with values from 0 to 1024.

- alias: Turn on Study Light when there is movement at night
  trigger:
    platform: state
    entity_id: binary_sensor.study_motion
    to: 'on'
  condition:
    condition: and
    conditions: 
      - condition: numeric_state
        entity_id: sensor.study_light_level
        below: '500'
      - condition: or
        conditions:
          - condition: sun
            after: sunset
          - condition: sun
            before: sunrise  
  action:
    - service: homeassistant.turn_on
      entity_id: switch.study_light

Hello.

Try the following:

- alias: Turn on Study Light when there is movement at night
  trigger:
    platform: state
    entity_id: binary_sensor.study_motion
    to: 'on'
  condition:
    condition: and
    conditions: 
      - condition: numeric_state
        entity_id: sensor.study_light_level
        below: 500
      - condition: or
        conditions:
          - condition: sun
            after: sunset
          - condition: sun
            before: sunrise  
  action:
    - service: homeassistant.turn_on
      entity_id: switch.study_light

I’d say that putting ‘’ in a numeric state condition should make it think it is a tring rather than a number.

1 Like

Hi thanks, can’t believe i missed that.
for the record I did remove the and condition and just make them all Or. Waiting for the morning to test completley. here is the final code

- alias: Turn on Light when there is movement at night or light levels are low
  trigger:
    platform: state
    entity_id: binary_sensor.study_motion
    to: 'on'
  condition:
    condition: or
    conditions: 
      - condition: numeric_state
        entity_id: sensor.study_light_level
        below: 500
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise  
  action:
    - service: homeassistant.turn_on
      entity_id: switch.study_light
1 Like

No worries, sometimes a different set of eyes looking at it, makes all the difference.

Good luck with your testing.