Blueprint help! Motion sensor, with lux, only during the day

Hello!

I’m losing my marbles with this blueprint. I want it to turn the downstairs lights on when motion is detected, but only when the room gets below 150lux, and between sunrise and sunset (with an offset).

blueprint:

  name: Motion/Sun-activated Light with illuminance

  description: Turn on a light when motion is detected, illuminance is below a

    set Lux level, and during sunlight hours.

  domain: automation

  input:

    motion_entity:

      name: Motion Sensor

      selector:

        entity:

          domain: binary_sensor

          device_class: motion

          multiple: true

    lux_entity:

      name: Illuminance Sensor

      selector:

        entity:

          domain: sensor

          device_class: illuminance

          multiple: false

    lux_level:

      name: Illuminance level

      description: If lux is below this value and motion is detected, the light will

        turn on.

      default: 100

      selector:

        number:

          min: 0.0

          max: 1000.0

          step: 1.0

          mode: slider

    light_target:

      name: Light

      selector:

        target:

          entity:

            domain: light

    no_motion_wait:

      name: Wait time

      description: Time to leave the light on after last motion is detected.

      default: 120

      selector:

        number:

          min: 0.0

          max: 3600.0

          unit_of_measurement: seconds

          step: 1.0

          mode: slider

    brightness:

      name: Light brightness

      description: The brightness of the lights.

      default: 254

      selector:

        number:

          min: 5.0

          max: 255.0

          step: 1.0

          mode: slider

    sunset_offset:

      name: Offset to turn off lights

      description: Offset before sunset

      default: 60

      selector:

        number:

          min: -60

          max: 60

          unit_of_measurement: minutes

    sunrise_offset:

      name: Offset to turn on lights

      description: Define offset after sunrise

      default: 45

      selector:

        number:

          min: -60

          max: 60

          unit_of_measurement: minutes

mode: restart

max_exceeded: silent

trigger:

  platform: state

  entity_id: !input 'motion_entity'

  from: 'off'

  to: 'on'

condition:

  condition: and

  conditions:

  - condition: numeric_state

    entity_id: !input 'lux_entity'

    below: !input 'lux_level'

   

  - condition: or

    conditions:

      - condition: sun

        after: sunrise

        after_offset: !input sunrise_offset

      - condition: sun

        before: sunset

        before_offset: !input sunset_offset

action:

- service: light.turn_on

  data:

    brightness: !input 'brightness'

  target: !input 'light_target'

- wait_for_trigger:

    platform: state

    entity_id: !input 'motion_entity'

    from: 'on'

    to: 'off'

- delay: !input 'no_motion_wait'

- service: light.turn_off

  target: !input 'light_target'

What am I missing?

Which error are you getting? It’s not turning on or is it turning on when the luminance is above the level you selected?

Apologies, I should have been more clear.

the lux motion part works as expected, but the sunset/sunrise constraints don’t seem right. The lights turn on regardless. From the trace it would seem that if the lux condition is met, then it is always after sunrise or before sunset.

I tried without the ‘or’ condition, but that didnt work either.

By the way, as the light might affect the illuminate level, I would suggest you to add in your condition a test for the current state of the light, so the automation runs if the light is already on, independently of the illuminance level, so you ensure any movement will reset the wait timer.

So, you don’t wanna the light to turn on during the night, is that correct? It’s only between sunrise and sunset (with the offset)?

Exactly right.

BTW, good idea RE the state of the light. Revised to this:


blueprint:

  name: Motion/Sun-activated Light with illuminance

  description: Turn on a light when motion is detected, illuminance is below a

    set Lux level, and during sunlight hours.

  domain: automation

  input:

    motion_entity:

      name: Motion Sensor

      selector:

        entity:

          domain: binary_sensor

          device_class: motion

          multiple: true

    lux_entity:

      name: Illuminance Sensor

      selector:

        entity:

          domain: sensor

          device_class: illuminance

          multiple: false

    lux_level:

      name: Illuminance level

      description: If lux is below this value and motion is detected, the light will

        turn on.

      default: 100

      selector:

        number:

          min: 0.0

          max: 1000.0

          step: 1.0

          mode: slider

    light_target:

      name: Light

      selector:

        target:

          entity:

            domain: light

    no_motion_wait:

      name: Wait time

      description: Time to leave the light on after last motion is detected.

      default: 120

      selector:

        number:

          min: 0.0

          max: 3600.0

          unit_of_measurement: seconds

          step: 1.0

          mode: slider

    brightness:

      name: Light brightness

      description: The brightness of the lights.

      default: 254

      selector:

        number:

          min: 5.0

          max: 255.0

          step: 1.0

          mode: slider

    sunset_offset:

      name: Offset to turn off lights

      description: Offset before sunset

      default: 60

      selector:

        number:

          min: -60

          max: 60

          unit_of_measurement: minutes

    sunrise_offset:

      name: Offset to turn on lights

      description: Define offset after sunrise

      default: 45

      selector:

        number:

          min: -60

          max: 60

          unit_of_measurement: minutes

mode: restart

max_exceeded: silent

trigger:

  platform: state

  entity_id: !input 'motion_entity'

  from: 'off'

  to: 'on'

condition:

  condition: and

  conditions:

  - condition: state

    entity_id: !input 'light_target'

    state: off

  - condition: numeric_state

    entity_id: !input 'lux_entity'

    below: !input 'lux_level'

   

  - condition: or

    conditions:

      - condition: sun

        after: sunrise

        after_offset: !input sunrise_offset

      - condition: sun

        before: sunset

        before_offset: !input sunset_offset

action:

- service: light.turn_on

  data:

    brightness: !input 'brightness'

  target: !input 'light_target'

- wait_for_trigger:

    platform: state

    entity_id: !input 'motion_entity'

    from: 'on'

    to: 'off'

- delay: !input 'no_motion_wait'

- service: light.turn_off

  target: !input 'light_target'

So, if you want during day only you should replace that OR by AND.

After the sunset is always after the sunrise, and before sunrise is always before the sunset, so your “OR” clause will always be true.

Try this:

condition:
  condition: and
  conditions:
  - condition: state
    entity_id: !input 'light_target'
    state: off
  - condition: numeric_state
    entity_id: !input 'lux_entity'
    below: !input 'lux_level'
  - condition: and ##### this is the only line I've changed #####
    conditions:
      - condition: sun
        after: sunrise
        after_offset: !input sunrise_offset
      - condition: sun
        before: sunset
        before_offset: !input sunset_offset

I’ll give it a try. Thanks!

1 Like

Alright, so the solution looks like this:

blueprint:
  name: Motion/Sun-activated Light with illuminance
  description: Turn on a light when motion is detected, illuminance is below a
    set Lux level, and during sunlight hours.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
          multiple: false
    lux_entity:
      name: Illuminance Sensor
      selector:
        entity:
          domain: sensor
          device_class: illuminance
          multiple: false
    lux_level:
      name: Illuminance level
      description: If lux is below this value and motion is detected, the light will
        turn on.
      default: 100
      selector:
        number:
          min: 0.0
          max: 1000.0
          step: 1.0
          mode: slider
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          step: 1.0
          mode: slider
    brightness:
      name: Light brightness
      description: The brightness of the lights.
      default: 254
      selector:
        number:
          min: 5.0
          max: 255.0
          step: 1.0
          mode: slider
    sunset_offset:
      name: Offset to turn off lights
      description: Offset before sunset
      default: 60
      selector:
        number:
          min: -60
          max: 60
          unit_of_measurement: minutes
    sunrise_offset:
      name: Offset to turn on lights
      description: Define offset after sunrise
      default: 45
      selector:
        number:
          min: -60
          max: 60
          unit_of_measurement: minutes

mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input 'motion_entity'
  from: 'off'
  to: 'on'

condition:
  condition: and
  conditions:
  - condition: numeric_state
    entity_id: !input 'lux_entity'
    below: !input 'lux_level'
  - condition: sun
    after: sunrise
    after_offset: !input sunrise_offset
    before: sunset
    before_offset: !input sunset_offset

action:
- service: light.turn_on
  data:
    brightness: !input 'brightness'
  target: !input 'light_target'
- wait_for_trigger:
    platform: state
    entity_id: !input 'motion_entity'
    from: 'on'
    to: 'off'
- delay: !input 'no_motion_wait'
- service: light.turn_off
  target: !input 'light_target'

I do have a follow-up issue. I couldn’t add a condition for checking if the light was already on, it throws an error, but it also prevents the automation from repeating if motion is detected.

Is there a simple-ish was to check if the lights are aleady on, usually by the Google home, and then not run the automation if they are?

So, I now understand that identifying Google home controls is problematic because it doesn’t have a user id.

Is there a way to assigne a user id to it?

Alternatively, how do I have the ‘light being off’ as a condition to starting the automation, but not affect the repeating nature of the automation?

Edit: I suspect the solution would be something related this type of condition:

condition:
  - condition: template
    value_template: '{{ trigger.to_state.context.user_id == none }}'

But how do I get it to check the to_state of the lights?

Ignore me, it seems to be working.

Final blueprint:

blueprint:
  name: Motion/Sun-activated Light with illuminance
  description: Turn on a light when motion is detected, illuminance is below a
    set Lux level, and during sunlight hours.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
          multiple: false
    lux_entity:
      name: Illuminance Sensor
      selector:
        entity:
          domain: sensor
          device_class: illuminance
          multiple: false
    lux_level:
      name: Illuminance level
      description: If lux is below this value and motion is detected, the light will
        turn on.
      default: 100
      selector:
        number:
          min: 0.0
          max: 1000.0
          step: 1.0
          mode: slider
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          step: 1.0
          mode: slider
    brightness:
      name: Light brightness
      description: The brightness of the lights.
      default: 254
      selector:
        number:
          min: 5.0
          max: 255.0
          step: 1.0
          mode: slider
    sunset_offset:
      name: Offset to turn off lights
      description: Offset before sunset
      default: 60
      selector:
        number:
          min: -60
          max: 60
          unit_of_measurement: minutes
    sunrise_offset:
      name: Offset to turn on lights
      description: Define offset after sunrise
      default: 45
      selector:
        number:
          min: -60
          max: 60
          unit_of_measurement: minutes

mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input 'motion_entity'
  from: 'off'
  to: 'on'

condition:
  condition: and
  conditions:
  - condition: numeric_state
    entity_id: !input 'lux_entity'
    below: !input 'lux_level'
  - condition: sun
    after: sunrise
    after_offset: !input sunrise_offset
    before: sunset
    before_offset: !input sunset_offset

action:
- service: light.turn_on
  data:
    brightness: !input 'brightness'
  target: !input 'light_target'
- wait_for_trigger:
    platform: state
    entity_id: !input 'motion_entity'
    from: 'on'
    to: 'off'
- delay: !input 'no_motion_wait'
- service: light.turn_off
  target: !input 'light_target'