Light automation advice

I have an automation below that turns on our stair lights (using a Shelly switch) when it gets dark. The trigger is the average of two z-wave sensors located in two bathrooms. These sensors update the lux level every 10 minutes.

Problem is, if the lux level goes above ‘7’ after the lights have been turned off at night (full moon), the lux level can drop again triggering the stair lights to come back on again. I’m trying to think of a way to avoid the lights auto turning on if they’ve been turned off manually when we go to bed. I do track the switch status on the Shelly but this is off (‘0’) when the lights come on automatically and on (1) when the lights are turned off but being a 3-way circuit, this can reverse if someone manually turns the lights on.

Anyone got an ideas?

  - alias: 'Turn on stair lights when dark'
    initial_state: true
    hide_entity: false
    trigger:
      platform: numeric_state
      entity_id: sensor.average_lux
      below: 7
      for:
        minutes: 15
      # platform: numeric_state
      # entity_id: sun.sun
      # value_template: '{{ state.attributes.elevation }}'
      # below: 3.0
    condition: []
    action:
        - service: homeassistant.turn_on
          entity_id: light.stair_lights

do you have home modes, or scenes already in your setup.

I use a set of ‘activities’ to trigger my complete setup and act as possible conditions in automations.

in this particular case I use activity ‘Sleep’, and the condition could be:

{{states('sensor.activity') not in ['Sleep', 'Other activity'] }}

to give you a real life example:

  - alias: 'Switch Bedside table on when motion'
    id: 'Switch Bedside table on when motion'
    initial_state: 'off'
    trigger:
      platform: state
      entity_id: binary_sensor.master_bedroom_motion_sensor
      to: 'on'
    condition:
      - condition: template
        value_template: >
          {{is_state('light.master_bedroom','off') and is_state('switch.master_bed_outlet','off')}}
      - condition: template
        value_template: >
          {{states('sensor.activity_selection') not in ['Opstaan','Naar bed','Slapen']}}
      - condition: template
        value_template: >
          {{ states('sensor.master_bedroom_motion_sensor_lux')|int <= 15 }}
    action:
      - service: light.turn_on
        entity_id: light.master_bedroom
        data:
          profile: relax
      - service: switch.turn_on
        entity_id: switch.master_bed_outlet

I am assuming you have a second automation that turns the light off, when the lux sensor goes above 7.
Couldnt you edit that one to have a time condition?
So that it will turn off the lights when the lux sensor goes above 7 and the time is after 5am (for example)

I personally found peace by automating my yard lights with solar angle sensor, so that it works all year round.


sensor:
  - platform: template
    sensors:
#get solar angle from sun.sun sensor
      solar_angle:
        friendly_name: "Sun angle"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

# A sensor to tell me when light should be on or off
      garden_wall_lights_timer:
        friendly_name: Garden Lights Timer
        entity_id: 
          - sensor.solar_angle
          - input_number.sun_angle_garden_wall_lights_threshold
        value_template: >-
           {{
           (states.sensor.solar_angle.state | float)<
           (states.input_number.sun_angle_garden_wall_lights_threshold.state | float)
           }}
#an input number to set the exact point by UI
input_number:
  sun_angle_garden_wall_lights_threshold:
    name: Sun Angle Threshold
    min: -50
    max: 50
    step: 0.1
    mode: box
    icon: mdi:camera-timer

#and the automations that do the job:

automation:
  - id: '1542121007829'
      alias: Garden Wall Lights Auto ON
      trigger:
      - entity_id: binary_sensor.garden_wall_lights_timer
        from: 'off'
        platform: state
        to: 'on' 
      condition: []
       action:
      - data:
          entity_id: switch.garden_wall_lights
        service: switch.turn_on
    - id: '1542121007830'
      alias: Garden Wall Lights Auto OFF
      trigger:
      - entity_id: binary_sensor.garden_wall_lights_timer
        from: 'on'
        platform: state
        to: 'off'
      condition: []
      action:
      - data:
          entity_id: switch.garden_wall_lights
        service: switch.turn_off
  
# (something's wrong with the identation pasting here, watch out):

yes, that also possible. And I use these too. Must say, that having full automated lighting schedules tend to be too inflexible at times, and I feel the need for manual action, especially in the more sensitive surroundings. Garden lights and motion trackers can be left alone and need some finetuning in the beginning. Home lighting, especially bedrooms with their multi condition settings of curtains and sunlight combined with motion, need a manual override. Especially when more people are around with different references …:slight_smile: For that purpose Ive made a switch which manually disables the posted automation. And the Morning rise activity enables it automatically again. Everyone happy.

having said that, you automation could be condensed to:

- id: 'Garden Wall Lights Auto'
  alias: 'Garden Wall Lights Auto'
  trigger:
    platform: state
    entity_id: binary_sensor.garden_wall_lights_timer
  condition: []
  action:
    service_template: >
      switch.turn_{{trigger.to_state.state}}
    entity_id: switch.garden_wall_lights

@Mariusthvdb, @krash. Thanks for the tips, much appreciated. I’ll look at the value templates but my use case is actually pretty simple. The stairs lights come on when it’s dark but that can be during the day if it’s really inclement outside. 99% of the time, I turn them off manually when I go to bed but there is an automation to turn them off if the lux goes above 25 for 40 minutes or more or at sunrise.

I think in an effort to have the lights come on a bit later in the evening, I may have set the low level number too low (7) as this can quite easily be reached by leaving a light on in the lux sensing area for a bit. I’ve raised that to 9 for now to see how that goes.

My thoughts for a permanent fix might be to somehow use the toggle state of the light switch to cancel the automation that turns the lights on automatically until the next sunrise or sunset but I wouldn’t know where to start to code that. :thinking: So if the light comes on near dusk but then is switched off manually (state tracked in HA but can be a 0 or 1), then the automation is cancelled until it’s actually sunset. Same for my night-time routine. Manually switching off the light when I go to bed prevents the auto-on from running until sunrise the next day.

Would that work?

You make a sensor that monitors the change from 7. Or you could just change the value based on the time of day. for example, between sunrise and sunset have it set to 7 but between sunset and sunrise change it to slightly above whatever moonlight is. You have lots of options that can all be done in this single automation. Personally, i’d go with a dynamic threshold based on the time.

Could you possibly explain that one a little further?

that’s exactly what I suggested. Instead of my automated morning routine, you’d just have to have the trigger for the sun rising to enable the automation again.

Template sensor that compares the current lux to the value 7.

sensor:
  - platform: template
    sensors:
      difference:
        value_template: "{{ states('sensor.lux') | int - 7 }}"

If it’s negative do something, if positive but low number do something. You could also build that into your automation without the sensor as a condition.

Thank you. Any chance you could example the dynamic scenario above too?

Ah, well, now that I see your original automation… You’ll have to go about it this way:

binary_sensor:
  - platform: template
    sensors:
      average_lux:
       friendly_name: Average Lux Indicator
       value_template: >
         {% set lux = states('sensor.average_lux') | int %}
         {% set threshold = 7 if is_state('sun.sun', 'above_horizon') else 15 %}
         {{ lux < threshold }}

then the automation

  - alias: 'Turn on stair lights when dark'
    initial_state: true
    hide_entity: false
    trigger:
      platform: state
      entity_id: binary_sensor.average_lux
      to: 'on'
      for:
        minutes: 15
      # platform: numeric_state
      # entity_id: sun.sun
      # value_template: '{{ state.attributes.elevation }}'
      # below: 3.0
    condition: []
    action:
        - service: homeassistant.turn_on
          entity_id: light.stair_lights

Also, maybe you could fix the whole thing by moving the lux sensor? somewhere where the moonlight wont affect it but it will still get the correct information from the sunlight?

I think you’re right. A 1 minute updating Tasmota lux sensor in the stairwell or close by would be the best bet. For now, I’ll try the solution @petro has recommended (thank you!) and see how I go.

Appreciate everyone’s thoughts.

1 Like

I’d love to hear how this turned out. I have a situation similar to this.

Hey Arnold. Before I got too carried away, I upped my below: level to 9 and this made it a lot more stable.