Control lights based on luminance - lights goes on/off between the trigger values

Hi,

I’m using a Aeotec Multi 6 to measure the indoor luminance, to control my indoor lights. It has been working quite well, but now I’ve experience that the sensor does not report the value “in time”, i.e. it gets too dark before it reports to the controller and when I do a refresh on the device, it reports directly. I found out that the refresh value can be altered, but only maximum of 60 seconds.

This will cause that the luminance value will flicking up and down with 1 or 2 lux between every refresh, no big deal unless it is between the value that I triggers on. I.e. my “shade” and “dark” scripts toggles every second minute until the luminance value have been stable on either side of the trigger value.

I’m looking for adding a “for minutes: 10” statement, but cannot see where I can add this for my automation. Any one of you guys that have a idea and can help me?

My automation:

- alias: Turn on or off light based on lux
  trigger:
    platform: state
    entity_id: sensor.light_sensor
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sensor.someone_home
        state: 'yes'
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      # - condition: time
      #   after: '08:00:00'
  action:
    - service_template: >-
        {% if is_state ("sensor.light_sensor", "Bright") %}
          script.bright_lights
        {% elif is_state ("sensor.light_sensor",  "Dim") %}
          script.dim_lights
        {% elif is_state ("sensor.light_sensor",  "Shade") %}
          script.shade_lights
        {% elif is_state ("sensor.light_sensor", "Dark") %}
          script.dark_lights
        {% else %}
          unknown
        {% endif %}

And my sensor that gives me the trigger values

light_sensor:
  friendly_name: Inside Light Sensor
  value_template: >-
    {% if states("sensor.dinner_room_luminance") | float >= 29 %}
      Bright
    {% elif states("sensor.dinner_room_luminance") | float < 27 and states("sensor.dinner_room_luminance") | float >= 20 %}
      Dim
    {% elif states("sensor.dinner_room_luminance") | float < 18 and states("sensor.dinner_room_luminance") | float >= 15 %}
      Shade
    {% elif states("sensor.dinner_room_luminance") | float < 13 and is_state("input_boolean.night_mode", "off") %}
      Dark
    {% elif states("sensor.dinner_room_luminance") | float < 13 and is_state("input_boolean.night_mode", "on") %}
      Night
    {% else %}
      unknown
    {% endif %}

Maybe this could work? It will need an automation for each brightness state so you might want to simplify the condition, for example with another template sensor.

- alias: Turn to Bright light based on lux
  trigger:
    platform: state
    entity_id: sensor.light_sensor
    to: 'Bright'
    for:
      minutes: 10
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: sensor.someone_home
        state: 'yes'
      - condition: state
        entity_id: sun.sun
        state: 'above_horizon'
      # - condition: time
      #   after: '08:00:00'
  action:
    - service:
          script.bright_lights
1 Like

Actually, maybe just change the top of your original automation to this:

- alias: Turn on or off light based on lux
  trigger:
    platform: state
    entity_id: sensor.light_sensor
    to: ["Bright","Dim","Shade","Dark","Night"]
    for:
      minutes: 10
  condition:
    # [...]
1 Like

Thanks, the last one was very attractive. Have got another tip as well. Will try both and see how well it work. Appreciate you input and effort on this @amelchio :slight_smile:

@amelchio I did test your idea, but unfortunate I get an error. [automation]: expected str for dictionary value @ data[‘trigger’][0][‘to’].

Any idea of what it can be?

1 Like

My bad, to does not actually accept a list of states. Sorry about that.

I’m a little confused. It doesn’t update fast enough, but now it updates too quick? :slight_smile:

You could possibly use a statistics sensor to get the mean value for some period of time, that might be more stable: https://home-assistant.io/components/sensor.statistics/

Or, you could build hysteresis into your automations. Say in the morning you’d have your “Shade” scene come on at 10 lux (so above: 10) , but then as it’s getting darker in the evening, trigger it on below: 8. (I might have that backwards… but I think you might know what I mean!)

Yeah, found out the static sensor and this works much better. However, still get flickering between my levels.

I do trigger on dark/shade values, and it is when the value goes from dark to shade (in my case below 15 lux to between 15 and 25 lux).

Think i need to but a higher value for sale size. Started with 6 (from an example) but now moved into default 20. Will see how that works.