Help with Automation to turn on light based on sensor value

Thanks,
This below lines are in the configuration.yaml

binary_sensor:
  - platform: threshold
    name: "Low LUX"
    entity_id: sensor.sn1_ldr
    type: lower
    threshold: 330

In automation.yaml

alias: Light Level is Low - Switch On
trigger:
  - platform: state
    name: "Low LUX"
    entity_id: binary_sensor.low_lux
    to: on 
    action: 
        -  service: : light.turn_on
           entity_id: light.sn1_led

Just be aware that the GUI automation editor will throw an error if you use OR saying OR is not supported condition… but the automation works perfectly.

@xbmcnut- Please let me know if you get it working with turning the Light on with Sensor value. Please give the details …so I can verify what I am missing here.

The formatting there is very wrong - I’d suggest you take some time to read the documentation. It should look a bit more like:

  - alias: Light Level is Low - Switch On
    trigger:
      - platform: state
        name: "Low LUX"
        entity_id: binary_sensor.low_lux
        to: 'on'
    action: 
      - service: light.turn_on
        entity_id: light.sn1_led

However, for what you’re trying to do with two light levels and motion on the higher one:

  - alias: Light Level is Low - Switch On
    trigger:
      - platform: state
        name: "Low LUX"
        entity_id: binary_sensor.YOUR_MOTION_SENSOR
        to: 'on'
      - platform: numeric_state
        name: "Low LUX"
        entity_id: sensor.sn1_ldr
        below: 330
    condition:
      - condition: numeric_state
        entity_id: sensor.sn1_ldr
        below: 450
    action: 
      - service: light.turn_on
        entity_id: light.sn1_led

That will trigger when either there’s motion, or the light level falls below 330. Then it will check that the light level is below 450 (which it will be if the light is below 330), and if so, turn on the light.

Thanks, i will give a try.

1 Like

It is not working , the moment I put the get the values from the Lux value from sensor, it does not do anything.

I changed the code to verify the other format like -

indent preformatted text by 4 spaces
- id: Light Level is Low - Switch On
  alias: Exterior Lighting on when dark outside
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -4.0
action:
  service: homeassistant.turn_on
  entity_id: switch.testled1

The above code works -
So I made changes like -

- id: Light Level is Low Sensor- Switch On
  alias: Exterior Lighting Sensor on when dark outside
  trigger:
    below: 450
    entity_id: binary_sensor.low_lux
    platform: numeric_state
    value_template: '{{ state.attributes.sensor_value }}'
  action:
  - entity_id: switch.testled2
    service: homeassistant.turn_on 

But it is not working as well.
I tried your solution -

- alias: Light Level is Low - Switch On
    trigger:
      - platform: state
        entity_id: binary_sensor.low_lux
        to: 'on'
    action: 
      - service: light.turn_on
        entity_id: light.sn1_led

It is not working for me. Please guide…

can you enter the binary_sensor.low_lux in the state dev tools? does it exist at all?

You’re using the wrong sensor - use the one that returns the numeric value of the light level.

Yes, it is there

binary_sensor.low_lux

State - On

Attributes-
entity_id: sensor.sn1_ldr
hysteresis: 0
lower: 450
position: below
sensor_value: 272
type: lower
upper: null
friendly_name: Low LUX

Please let me know which one I should use here ?? Sorry new to HA and not sure which one to use…

Your last automation triggers on the state Change on/off (not numeric_state but state )so you can only test it when that actually happens, by triggering it in the dev tools. Have you done the latter?

You can also create the input_number from my package and use that. With that you can easily keep triggering the automation by sliding the slider.

Sadly my crystal ball is broken :wink:

We don’t know what entities you’ve got, or what their state and attributes are. I’m guessing that sensor.sn1_ldr is what you’re after, as I posted before, but… :man_shrugging:

This is in my configuration.yaml file

binary_sensor:
  - platform: threshold
    name: "Low LUX"
    lower: 450
    entity_id: sensor.sn1_ldr

  - platform: threshold
    name: "High LUX"
    upper: 500
    entity_id: sensor.sn1_ldr 

This is in my automation.yaml file -

- id: Light Level is Low - Switch On
  alias: Light Level is Low - Switch On
  trigger:
    platform: state
    entity_id: binary_sensor.low_lux
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.sn1_led

- id: Light Level is High - Switch Off
  alias: "Light Level is High - Switch Off"
  trigger:
    platform: state
    entity_id: binary_sensor.high_lux
    to: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: light.sn1_led

This is shown in Developer tool (States(<>)) in HA.

Entity 
binary_sensor.low_lux 

State - 
on

Attributes- 

entity_id: sensor.sn1_ldr
hysteresis: 0
lower: 450
position: below
sensor_value: 298
type: lower
upper: null
friendly_name: Low LUX

I can see the state of the Binary sensor is “ON” but the lights are turning on with it. Anything I am missing ??

low_lux = on means less light than the threshold, so turn on lights. Seems to be desired behavior?
btw, your low_lux threshold is rather high… should be more useful at 50 or so. equally your high lux is a tiny bit high tp be discerningly useful. lower to 350 or 400 and have more use for it (of course depending on the position of the sensors, and the general value of that position for you complete setting)

because of the latter, I also use a mean sensor to generate the overall light threshold in the house and compare to that to be able to finetune

Thanks, sorry my bad

Binary sensor is “ON” but the lights are **not** turning on with it. 

Yes the threshold is high , but i was keeping this for a testing purpose.
I guess I have kind of figured out why the lights are not turning on.

I think I have to add a loop to run for few minutes. I will test and update soon.

I got it working , as the state was “ON” already so the automation was not triggered anytime. I opened the automation and see the status .

LowLightAutomation

So trgigerring it manually results in proper result.

Thanks all for all the support and sorry to bother too much. Appreciate the prompt response, not seen anywhere else.

669/5000
Best. I am new here . and my English is bad
try with this code: from you
my automation lamps on when lower is then.
it does not work.
can you help me code automation lamps on
in me automation.yaml
I’m only a beginner.

thank you very much for your help
Kind regards

binary_sensor:

  • platform: template
    sensors:
    low_lux:
    friendly_name: “Low LUX”
    value_template:> -
    {{states (‘sensor.mean_indoor_lux’) | int <5}}
  • platform: template
    sensors:
    high_lux:
    friendly_name: “HighLUX”
    value_template:> -
    {{states (‘sensor.mean_indoor_lux’) | int> 5000}}

497/5000
hello I am new. and me English is bad.
try to make automation code for 14 days
am a beginner.
have this code primer sensor. used works perfectly
automation.yaml really does not want to succeed.
could anyone help me please I do not come out.
am also a beginner.
thank you in advance
i need a automation.yaml when sensor lux low then livingroom lights On.
tanks all
i have thiis code sensor and works perfectly:
only me automation.yaml please who can help me

You had a number of spaces where they didn’t belong, and possibly some missing brackets. Try the following:

binary_sensor:
   - platform: template
     sensors:
       low_lux:
         friendly_name: "Low LUX"
         value_template: {{ (states('sensor.mean_indoor_lux') | int) < 5 }}
       high_lux:
         friendly_name: "HighLUX"
         value_template: {{ (states('sensor.mean_indoor_lux') | int) > 5000 }}