Help with Automation to turn on light based on sensor value

yes what @Tinkerer says.
you could try some automation built on that as follows, and adapt to your liking/needs:

automation:
  - alias: 'Low Lux warning'
    id: 'Low Lux warning'
#    initial_state: on
    trigger:
      platform: state
      entity_id: binary_sensor.low_lux_input
      to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.notify_notify
      state: 'on'
    action:
      service: notify.notify
      data_template:
        message: >
          Lux low: {{ states.sensor.mean_indoor_lux.state }} 
          {{states.sensor.mean_indoor_lux.attributes.unit_of_measurement}}

  - alias: 'High Lux warning'
    id: 'High Lux warning'
#    initial_state: on
    trigger:
      platform: state
      entity_id: binary_sensor.high_lux_input
      to: 'on'
    condition:
      condition: state
      entity_id: input_boolean.notify_notify
      state: 'on'
    action:
      service: notify.notify
      data_template:
        message: >
          Lux high: {{ states.sensor.mean_indoor_lux.state }} 
          {{ states.sensor.mean_indoor_lux.attributes.unit_of_measurement }}

if you’d like the binary_sensors to be responsive to your input via the frontend (and not hardcode the threshold) you could adapt as follows, and as i have done in the above example:

input_number:
  high_lux:
    name: High Lux
    icon: mdi:gauge
#    initial: 100
    min: 0
    max: 400
    step: 10

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

and

binary_sensor:
- 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 %}

2 Likes

Hi @Mariusthvdb , I’ve tried your code, I can see the changes of the two binary sensors when playing with the lux sensor, but for some reason I can’t get it to push any message (I even tried to remove the condition you had).
This is my code in the automation:

- alias: 'Low Lux warning'
  id: 'Low Lux warning'
  trigger:
  - entity_id: binary_sensor.low_lux_input
    platform: state
    to: 'on'
  action:
  - data_template:
      message: "Lux low: {{ sensor.hue_motion_sensor_1_light_level }} \n"
    service: notify.notify
```

I've got other automation's sending messages to Pushover, so messaging works...

you should use:

 - data_template:
      message: "Lux low: {{ states('sensor.hue_motion_sensor_1_light_level') }} \n"

always test the template in dev-template to see if it’s got a valid output.
didnt you see an error message in the log?

btw, Ive changed these sensors to look like:

binary_sensor:
  - platform: template
    sensors:
      low_lux_input:
        friendly_name: Low lux input
        device_class: light
        value_template: >
          {{(states('sensor.mean_indoor_lux') | float) < 
            (states('input_number.low_lux') | float)}}

      high_lux_input:
        friendly_name: High lux input
        device_class: light
        value_template: >
          {{(states('sensor.mean_indoor_lux') | float) > 
            (states('input_number.high_lux') | float)}}
1 Like

@Mariusthvdb

Thanx! it was a misunderstanding on the content of the message from my side :slight_smile:

I have slightly expanded the ability to turn on and off the light by the light sensor. To turn on and off the light, we use low_lux and high_lux. In fact, we rigidly specify the boundaries and if the lighting on the street is below low_lux, then the light will turn on, and if it is above high_lux, then it will turn on. The meaning of this idea is that if it is still light on the street according to the light sensor and the light has not fallen below low_lux, and the light level in the room is lower than on the street, then you need to manually adjust low_lux. I made an adaptive adjustment of the light. Using the separation of who turned on the light, a person or automation. If automation works, the low_low and high_lux ranges will remain unchanged, but if you turn on the light manually, the low_lux and high_lux ranges will change.

Watch the video how it works

To work, you need two automations.

The first automation is responsible for turning on and off the light by low_lux and high_lux

alias: 'Living room: Ceiling chandelier. The light sensor.'
description: 'Operation of lighting by the light sensor'
trigger:
  - platform: state
    entity_id: sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux
condition:
  - condition: time
    after: '12:00'
    before: '21:59'
  - condition: state
    entity_id: input_boolean.gostinaia_potolochnaia_liustra_korrektirovka_sveta
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{(states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              | float) <
              (states('input_number.lights_in_the_living_room_low_lux') |
              float)}}
        sequence:
          - type: turn_on
            device_id: xxxxxxxxxxxxxxxxxxxxx
            entity_id: light.yeelight_ceiling4_0x000000000000000
            domain: light
            brightness_pct: 40
      - conditions:
          - condition: template
            value_template: >-
              {{(states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              | float) >
              (states('input_number.lights_in_the_living_room_high_lux') |
              float)}}
        sequence:
          - type: turn_off
            device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxx
            entity_id: light.yeelight_ceiling4_0x000000000000000
            domain: light
    default: []
mode: single
max: 10

The second automation is responsible for adjusting the low_lux and high_lux ranges. Adjusts the ranges if the light was turned on by a person

alias: 'Living room: Ceiling chandelier. The light sensor. Adjustment'
description: ''
trigger:
  - platform: state
    entity_id: light.yeelight_ceiling4_0x000000000000000
condition:
  - condition: template
    value_template: '{{ trigger.to_state.context.parent_id == none }}'
  - condition: state
    entity_id: input_boolean.gostinaia_potolochnaia_liustra_korrektirovka_sveta
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'on'
          - condition: template
            value_template: >
              {% if
              states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
              | float < 500 %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_low_lux
            data:
              value: >-
                {{
                states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
                }}
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_low_lux.state
              >
              states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_low_lux
            data:
              value: '100'
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_high_lux.state
              <
              states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_high_lux
            data:
              value: >-
                {{
                states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
                }}
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_high_lux.state
              | float > 800 %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_high_lux
            data:
              value: '300'
    default: []
mode: single

Текст на русском (Text in Russian)

Я немного расширил возможность включения и выключения света по датчику света. Для включения и выключения света мы используем low_lux и high_lux. По сути жестко указываем границы и если на улице освещение будет ниже low_lux, то свет включится, а если выше high_lux, то включится. Смысл этой затеи в том, что, если по датчику света на улице еще светло и свет не опустился ниже low_lux, а в помещении уровень света ниже, чем на улице, то нужно вручную корректировать low_lux. Я сделал адаптивную регулировку света. Используя разделение кто включил свет, человек или автоматизация. Если сработает автоматизация, то диапазоны low_lux и high_lux останутся неизменными, но если включить свет вручную, то то диапазоны low_lux и high_lux изменятся.

Смотрим видео как это работает

Для работы нужны две автоматизации.

Первая автоматизация отвечает за включение и выключение света по low_lux и high_lux

alias: 'Гостиная: Потолочная люстра. Датчик света.'
description: Работа освещения по датчику света
trigger:
  - platform: state
    entity_id: sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux
condition:
  - condition: time
    after: '12:00'
    before: '21:59'
  - condition: state
    entity_id: input_boolean.gostinaia_potolochnaia_liustra_korrektirovka_sveta
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{(states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              | float) <
              (states('input_number.lights_in_the_living_room_low_lux') |
              float)}}
        sequence:
          - type: turn_on
            device_id: xxxxxxxxxxxxxxxxxxxxx
            entity_id: light.yeelight_ceiling4_0x000000000000000
            domain: light
            brightness_pct: 40
      - conditions:
          - condition: template
            value_template: >-
              {{(states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              | float) >
              (states('input_number.lights_in_the_living_room_high_lux') |
              float)}}
        sequence:
          - type: turn_off
            device_id: xxxxxxxxxxxxxxxxxxxxxxxxxxx
            entity_id: light.yeelight_ceiling4_0x000000000000000
            domain: light
    default: []
mode: single
max: 10

Вторая автоматизация отвечает за корректировку диапазонов low_lux и high_lux. Корректирует диапазоны в том случае, если свет был включен человеком

alias: 'Гостиная: Потолочная люстра. Датчик света. Корректировка'
description: ''
trigger:
  - platform: state
    entity_id: light.yeelight_ceiling4_0x000000000000000
condition:
  - condition: template
    value_template: '{{ trigger.to_state.context.parent_id == none }}'
  - condition: state
    entity_id: input_boolean.gostinaia_potolochnaia_liustra_korrektirovka_sveta
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'on'
          - condition: template
            value_template: >
              {% if
              states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
              | float < 500 %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_low_lux
            data:
              value: >-
                {{
                states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
                }}
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_low_lux.state
              >
              states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_low_lux
            data:
              value: '100'
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_high_lux.state
              <
              states('sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux')
              %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_high_lux
            data:
              value: >-
                {{
                states.sensor.gostinaia_datchik_sveta_levoe_okno_illuminance_lux.state
                }}
      - conditions:
          - condition: state
            entity_id: light.yeelight_ceiling4_0x000000000000000
            state: 'off'
          - condition: template
            value_template: >
              {% if states.input_number.lights_in_the_living_room_high_lux.state
              | float > 800 %} true {% else %} false {% endif %}
        sequence:
          - service: input_number.set_value
            target:
              entity_id: input_number.lights_in_the_living_room_high_lux
            data:
              value: '300'
    default: []
mode: single