Help with Automation to turn on light based on sensor value

Hi there,

Trying to get my first automation working from the front end so I can see how the automations.yaml gets formatted.

I have two entities
sensor.4_in_1_sensor_light_11
and
light.hall_26

I want to turn the light on when the lux level goes below a value of 5 and off when the lux level goes above 20. Any assistance would be appreciated. I’ve read over the templating section but you need an engineering degree to wade through that :yum:

2 Likes

I don’t doubt that this can be done with templating, however, templates are not my forte. Something like this I would split into two automations.

Use the numeric state trigger and the above or below option.

At it’s simplest:

trigger:
  platform: numeric_state
  entity_id: sensor.4_in_1_sensor_light_11
  below: 5
action:
  service: homeassistant.turn_on
  entity_id: light.hall_26

and

trigger:
  platform: numeric_state
  entity_id: sensor.4_in_1_sensor_light_11
  above: 20
action:
  service: homeassistant.turn_off
  entity_id: light.hall_26

Of course, if your light causes the light level to rise above 20, then you’re likely end up strobing the light on and off at night. I’d advise that you check the light level when the hall light is on, and then set the level to say 5 above that.

@Tinkerer Thank you so much. I’ll try that tonight.

Not having too much joy unfortunately. Struggling with the new format for the automations.yaml available from the front end. Can’t get that basic automation to save from the front end (using Chrome) under any circumstances. Pasted the following into automations.yaml and now they show up under the “automations” side bar but clicking on them (to edit) produces a blank page but at least it works! Thank you.

- id: ID1
  alias: Hall Light Auto On
  trigger:
  - platform: numeric_state
    entity_id: sensor.4_in_1_sensor_light_11
    below: 5
  action:
  - service: homeassistant.turn_on
    entity_id: light.hall_26
  
- id: ID2
  alias: Hall Light Auto Off
  trigger:
  - platform: numeric_state
    entity_id: sensor.4_in_1_sensor_light_11
    above: 20
  action:
  - service: homeassistant.turn_off
    entity_id: light.hall_26

Hi @xbmcnut,

I was having issues with an almost identical automation myself, and contacted another forum member (@fanaticDavid) who had a working solution that was similar to what I was looking for. This is how I achieve it, works faultlessly. Credit to David for his help.

Firstly, create a Binary Sensor for each instance;

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

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

Then, this is the automation based on Low Light Level;

# LIGHT LEVEL IS LOW - SWITCH ON #
- 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'
    for:
      minutes: 2
  condition:
    condition: and
    conditions:
      - condition: time
        after: "08:00:00"
      - condition: sun
        before: sunset
        before_offset: "00:45:05"
      - condition: state
        entity_id: group.family
        state: 'home'
  action:
    - service: light.turn_on
      # Yeelight RGB Bulb
      entity_id: light.steps
      data:
        brightness: 60
        color_temp: 222
        transition: 3
    - service: light.turn_on
      # LIFX RGB Bulb
      entity_id: light.lounge
      data:
        brightness: 110
        color_temp: 222
        transition: 5

and High Light Level;

# LIGHT LEVEL IS HIGH - SWITCH OFF #
- 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'
    for:
      minutes: 3
  condition:
    condition: and
    conditions:
      - condition: time
        after: "08:00:00"
      - condition: sun
        before: sunset
        before_offset: "00:45:05"
      - condition: state
        entity_id: group.family
        state: 'home'
  action:
    service: light.turn_off
    entity_id:
      - light.steps
      - light.lounge
4 Likes

Thank you! That’s helpful. Can you edit your automations from the front end?

@xbmcnut, No probIems, happy to share. I was pulling my hair out trying to work it out myself.

I haven’t tried to edit through the front end. I do all my edits through Notepad++ and Samba share in Windows.

I’m making another video in my Hass.io series and wanted to show noobs how to create a simple automation (turn a light on when it’s dark) but can’t get the front end to work at all. I use NP++ too but wanted to see how much easier it was from the front end as I’ve struggled with yaml formatting (and the ever changing formatting requirements) for the last year. I took me four goes to get HA to start with one simple automation file even though I’d checked the file with four different online yaml checkers! :disappointed:

I have had similar issues, being new to HASS, it has changed about twice already in a few months, hard to learn the right way when things change regularly!

I should have mentioned also that I’m using Bens (@bruhautomation) DIY Multisensor in conjuction with this automation. I have made 3 of them now for around my house, bloody great bit of kit for very little money.

That is on my to do list too!

could we set the threshold with an input_number.high_lux/low_lux. Thus enabling easier experimentation?

- platform: threshold
  name: "Low LUX"
  threshold: {{states.input_number.low_lux.state}}
  type: lower
  entity_id: sensor.mean_indoor_lux

Watch that the threshold sensor no longer supports a threshold: option.

1 Like

a yes, thanks!

but what about the template?

- platform: threshold
  name: "Low LUX"
  lower: {{states.input_number.low_lux.state}}
  entity_id: sensor.mean_indoor_lux

- platform: threshold
  name: "High LUX"
  upper: {{states.input_number.high_lux.state}}
  entity_id: sensor.mean_indoor_lux

gives this unfortunately:

CHECK CONFIG
Testing configuration at /config
ERROR:homeassistant.util.yaml:invalid key: "OrderedDict([('states.input_number.low_lux.state', None)])"
  in "/config/binary_sensors/threshold_binary_sensors.yaml", line 160, column 0
Failed config
  General Errors: 
    - invalid key: "OrderedDict([('states.input_number.low_lux.state', None)])"
  in "/config/binary_sensors/threshold_binary_sensors.yaml", line 160, column 0

would we still need something like this to be able to use the slider:

- alias: 'Temperature warning'
  trigger:
    platform: state
    entity_id: sensor.living_room_temperature
  condition:
    condition: template
    value_template: '{{ (states("sensor.living_room_temperature") | float) < (states("input_slider.warning_temperature") | float) }}'
  action:
    - service: notify.notify
      data_template:
        message: 'Temperature: {{ states.sensor.living_room_temperature.state }} {{ states.sensor.living_room_temperature.attributes.unit_of_measurement }}'

The documentation for the component would show if it supported templates, and I see nothing to show that it supports it.

You’d need to use a standard template binary sensor, something like the following:

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 }}

You can work in the use of the slider there

had the same idea, tested a bit and made this little package, maybe useful for others too, adjust to your own situation (automations don’t use the template sensor yet, might be even easier to do so):

##########################################################################################
# Package to use for automating based on Lux levels
# MtHvdB 20180413
##########################################################################################

homeassistant:
  customize:
    input_boolean.lux_low_active:
      templates:
        icon: >
          if (state === 'on') return 'mdi:bell'; else return 'mdi:bell-off';
        rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

    input_boolean.lux_high_active:
      templates:
        icon: >
          if (state === 'on') return 'mdi:bell'; else return 'mdi:bell-off';
        rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

# using customizing or device_class (in sensor definition below)
#    binary_sensor.low_lux_input:
#      templates:
#        icon: >
#          if (state === 'on') return 'mdi:bell'; else return 'mdi:bell-off';
#        rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

#    binary_sensor.high_lux_input:
#      templates:
#        icon: >
#          if (state === 'on') return 'mdi:bell'; else return 'mdi:bell-off';
#        rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

input_number:
  high_lux:
    name: High Lux
    icon: mdi:gauge
    initial: 100
    min: 1
    max: 400
    step: 5

  low_lux:
    name: Low Lux
    icon: mdi:gauge
    initial: 40
    min: 0
    max: 200
    step: 5

input_boolean:
  lux_low_active:
    name: Lux low active
  lux_high_active:
    name: Lux high active

group:
  lux_coordinates:
    name: Lux coordinates
    entities:
      - input_number.high_lux
      - input_number.low_lux
      - binary_sensor.high_lux
      - binary_sensor.low_lux
      - binary_sensor.high_lux_input
      - binary_sensor.low_lux_input
      - sensor.mean_indoor_lux #philips hue sensors_lux in Hue group
      - input_boolean.lux_low_active
      - input_boolean.lux_high_active

automation:
  - alias: 'Low Lux warning'
    id: '2511601480001'
    trigger:
      platform: state
      entity_id: sensor.mean_indoor_lux
    condition:
      condition: template
      value_template: '{{ (states("sensor.mean_indoor_lux") | float) < (states("input_number.low_lux") | float) }}'
    action:
      - service: notify.notify
        data_template:
          message: 'Lux: {{ sensor.mean_indoor_lux.state }} {{ states.sensor.mean_indoor_lux.attributes.unit_of_measurement }}'

  - alias: 'High Lux warning'
    id: '2511601480002'
    trigger:
      platform: state
      entity_id: sensor.mean_indoor_lux
    condition:
      condition: template
      value_template: '{{ (states("sensor.mean_indoor_lux") | float) > (states("input_number.high_lux") | float) }}'
    action:
      - service: notify.notify
        data_template:
          message: 'Lux: {{ sensor.mean_indoor_lux.state }} {{ states.sensor.mean_indoor_lux.attributes.unit_of_measurement }}'

  - alias: 'Low Lux activate'
    id: '2511601480003'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_boolean.lux_low_active
    condition: []
    action:
      - service: automation.turn_on
        data:
          entity_id: automation.low_lux_warning

  - alias: 'High Lux activate'
    id: '2511601480004'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_boolean.lux_high_active
    condition: []
    action:
      - service: automation.turn_on
        data:
          entity_id: automation.high_lux_warning

binary_sensor:
##########################################################################################
# Homemade template threshold to use in automations and set with input_number in Frontend
##########################################################################################
- platform: template
  sensors:
    low_lux_input:
      friendly_name: "Low lux input"
      device_class: light
      value_template: >
          {% if (states("sensor.mean_indoor_lux") | float) < (states("input_number.low_lux") | float) %}
           True
          {% else %}
            False
          {% endif %}
    high_lux_input:
      friendly_name: "High lux input"
      device_class: light
      value_template: >
          {% if (states("sensor.mean_indoor_lux") | float) > (states("input_number.high_lux") | float) %}
           True
          {% else %}
            False
          {% endif %}

##########################################################################################
# Threshold uses fixed threshold, replaced by template sensor based on input_number
##########################################################################################
- platform: threshold
  name: "Low LUX"
  lower: 50
  entity_id: sensor.mean_indoor_lux

- platform: threshold
  name: "High LUX"
  upper: 200
  entity_id: sensor.mean_indoor_lux

Hi , I tried the solution as above , but it seems I have trouble.

My Automation -

  • id: Light Level is Low - Switch On
    alias: Light Level is Low - Switch On
    trigger:
    • below: ‘450’
      entity_id: binary_sensor.low_lux
      platform: numeric_state
      action:
    • data:
      entity_id: light.sn1_led
      entity_id: light.sn1_led
      service: homeassistant.turn_on

My Sensor -
binary_sensor:

  • platform: threshold
    name: “Low LUX”
    lower: 450
    entity_id: sensor.sn1_ldr
    But the light is not turning on when LUX is below 450 .

Also is there a way i can have 2 conditions , mean Light < 450 LUX and if there is a motion.

Thanks,
Manoj

Yes, conditions support and, and or.

Thanks,
Also if possible , please let me know what is missing in the above automation as it does not work for me.

1 Like

You need to format it correctly (see the big blue box at the top) before we can tell what your automation looks like. Then you need to tell us what does not work for me means.