Dynamic lighting based on Lux sensor

Hello,

I’m trying to create an automation that turns on some lights based on Lux levels from a sensor. For some reason it isn’t working and I’m not sure why:

- id: '1618199875741'
  alias: Light Control - Living Room - Adaptive
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.livingroom_multisensor_illuminance
  condition:
  - condition: time
    after: 08:00:00
    before: '20:00:00'
  action:
  - service: light.turn_on
		data_template:
  		entity_id: light.living_room_lights
  		transition: 10
  		brightness: |-
    		{% if states("sensor.livingroom_multisensor_illuminance") > "16" %}
      		0
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "16" %}
      		10
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "15.5" %}
      		20
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "15" %}
      		30
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "14.5" %}
      		40
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "14" %}
      		50
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "13.5" %}
      		60
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "13" %}
      		70
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "12.5" %}
      		80
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "12" %}
      		90
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "11.5" %}
      		100
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "11" %}
      		110
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "10.5" %}
      		120
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "10" %}
      		130
   		  {% elif states("sensor.livingroom_multisensor_illuminance") < "9.5" %}
      		140
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "9" %}
      		150
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "8.5" %}
      		160
    		{% elif states("sensor.livingroom_multisensor_illuminance") < "8" %}
      		170
    {% elif states("sensor.livingroom_multisensor_illuminance") < "7.5" %}
      180
    {% endif %}
      180
    {% endif %}
  mode: single

I’d appreciate some help figuring this out :slight_smile:

Did you try a simple version first?

First major red flag, TAB ALERT!!! YAML is spaces only.

Couple of other things, the indentation of the last elif’s is waayyyyyyyy off. I also see two endif's but you only have a single if. You do a string compare which may not work, try

states("sensor.livingroom_multisensor_illuminance")|float > 16

And because all the levels just change linear you could just change it to maths

- id: '1618199875741'
  alias: Light Control - Living Room - Adaptive
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.livingroom_multisensor_illuminance
  condition:
  - condition: time
    after: 08:00:00
    before: '20:00:00'
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.living_room_lights
      transition: 10
      brightness: >-
        {% if states("sensor.livingroom_multisensor_illuminance")|float > 16 %}
        0
        {% elif states("sensor.livingroom_multisensor_illuminance")|float < 7 %}
        180
        {% else %}
        {{ ((16 - states("sensor.livingroom_multisensor_illuminance")|float) * 20)|round(0)|int }}
        {% endif
  mode: single

I think…

Thanks, yeah I caught those issues last night. I switched it up a bit base on this thread. Still doesn’t see to work:

- id: '1618199875741'
  alias: Light Control - Living Room - Adaptive
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.livingroom_multisensor_illuminance
  condition:
  - condition: time
    after: '08:00:00'
    before: '20:00:00'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.living_room_lights
        transition: 10
        brightness: >
          {%- set livingroombrightness = ((-18.6 * states('sensor.livingroom_multisensor_illuminance') | float) + 308.6) %}
          {% if states('sensor.livingroom_multisensor_illuminance')  < 7 %}
            178
          {% else %}
            {{ livingroombrightness | round(0) }}
          {% endif %}

I figured out the issue. I had forgotten about the template editor found that the above config was producing a value as a string. So after some changes, it is now working correctly. Here is the working solution:

- id: '1618199875741'
  alias: Light Control - Living Room - Adaptive
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.livingroom_multisensor_illuminance
  condition:
  - condition: time
    after: '08:00:00'
    before: '20:00:00'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.living_room_lights
        transition: 10
        brightness: >
          {%- set livingroombrightness = ((-18.6 * states("sensor.livingroom_multisensor_illuminance") | float) + 308.6 )| round(0) | int %}
          {% if states("sensor.livingroom_multisensor_illuminance")|float > 16 %}
            0
          {% elif states("sensor.livingroom_multisensor_illuminance")|float < 7 %}
            178
          {% else %}
            {{ livingroombrightness }}
          {% endif %}