Light control with light meter and motion detector

BACKGROUND
When it comes to decorative illumination, it has been somehow tiring to me that every second or third week I had to readjust the timers that switch the lights on because of the shifting of the solar hour with respect to the CET.
At the same time, some nights we go to bed earlier than other. Because of this, sometimes the lights switch off either “too late” or “too early”. All this challenged me to think: I want the decorative illumination to switch on always at the same level of ambient light (illumination) and then switch off at certain time depending on whether we are awake or not.

THE OBJECTIVE
To implement the attached event flow on HA.

THE CHALLENGE
The biggest challenge I found was to find a cheap light meter on the 433MHz band. There are quite many cheap temperature and humidity sensors out in market but hard to find a cheap light meter that would provide illumination measurements, not only a triggering.

THE SENSOR SOLUTION
Check my post “Hack: Turning a 433MHz thermometer into a light meter

THE AUTOMATION
Points to consider

  1. The automation solution I developed makes use of hass-variables component found in GitHub since I need to keep track of the point in time the decoration lights should be off.
  2. As shown in the diagram flow, I do not want the decoration lights to turn on again until the next day/morning. The decoration lights should stay off until the sunrise. This means that the system should discard false-positive situations where the sensor could get a high/low reading during the night.

There are six automation rules I developed to fulfil my requirements. The two main ones are the ones controlling the luminosity level and so triggering the decoration lights accordingly, they are: “Decoration low illuminance trigger” and “Decoration high illuminance trigger”.

  • “Decoration low illuminance trigger”
  • “Decoration high illuminance trigger”
  • “Decoration time to off time” --> Basically turns the lights off and set the variable to True to keep them off during the whole night.
  • “Decoration time to off PIR” --> It complements the previous one in case the HA gets restarted during the night. You should notice that I consider the early morning (after 00:00) which -at least for me, is still night.
  • “Decoration sunrise check” --> secures the decoration lights turns on if the state of the flag (night_off variable) changes to False, e.g. sunrise time but still very dark.
  • “Decoration: resets night_off flag on sunrise” to False so the decoration light can turn on again during the day or night again, depending on the illuminance conditions.
    - alias: "Decoration low illuminance trigger"
      trigger:
        platform: template
        value_template: "{{ (states.sensor.lux_value.state | int < 12) and (states.variable.night_off.state == 'False') }}"
      action:
        service: switch.turn_on
        entity_id: switch.decoration

    - alias: "Decoration high illuminance trigger"
      trigger:
        platform: template
        value_template: "{{ states.sensor.lux_value.state | int > 20 }}"
      action:
        service: switch.turn_off
        entity_id: switch.decoration

    - alias: "Decoration time to off time"
      trigger:
        platform: time
        at: '22:30:00'
      condition:
        condition: state
        entity_id: binary_sensor.motionlr
        state: 'off'
        for:
          minutes: 20
      action:
        - service: switch.turn_off
          entity_id: switch.decoration
        - service: variable.set_variable
          data:
            variable: night_off
            value_template: 'True'

    - alias: "Decoration time to off PIR"
      trigger:
        platform: state
        entity_id: binary_sensor.motionlr
        to: 'off'
        for:
          minutes: 20
      condition:
        condition: or
        conditions:
          - condition: time
            after: '22:30:00'
          - condition: and
            conditions:
              - condition: sun
                before: sunrise
              - condition: time
                after: '00:00:00'
      action:
        - service: switch.turn_off
          entity_id: switch.decoration
        - service: variable.set_variable
          data:
            variable: night_off
            value_template: 'True'

    - alias: "Decoration sunrise check"
      trigger:
        platform: state
        entity_id: variable.night_off
        to: 'False'
      condition:
        condition: numeric_state
        entity_id: sensor.lux_value
        below: 12
      action:
        service: switch.turn_on
        entity_id: switch.decoration

    - alias: 'Decoration: reset night_off flag on sunrise'
      trigger:
        - platform: sun
          event: sunrise
      action:
        - service: variable.set_variable
          data:
            variable: night_off
            value_template: 'False'
        - service: switch.turn_off
          entity_id: switch.decoration

And this is the variable declaration:

variable:
  night_off:
    value: false

Notes

  • I really recommend allowing some tolerance to the low and high illuminance values. As it can be seen in the snippet, low illuminance level is set at 12 lux whilst high illuminance level is at 20 lux. This is to avoid the system flapping around the threshold value.

Just let me know if you want me to explain more in detail any aspect I have missed or if you have suggestions to improve the solution.

6 Likes