Is there a better way to control lighting than using scenes?

My goal is to turn on different lights at different natural sun light intensity, as opposed to by what time of day it is.

I am not sure my approach using scenes is the best solution.

So far I made four different scenes; morning, daytime, evening and night, and four additional scenes for setting brightness for the same lights when motion is detected (dim up the light).

- name: gang_morgen
  entities:
    light.lys_gang_level:
      state: on
      brightness: 25
- name: gang_morgen_motion
  entities:
    light.lys_gang_level:
      state: on
      brightness: 75

Additionally I use one exterior light sensor to set the state for two sensors dark_inside and dark_outside . These are used in automations (see below).

sensor:
  dark_inside:
    entity_id: sensor.pir_ute_paviljong_luminance
    friendly_name: Mørkt innendørs
    value_template: >-
      {{ states('sensor.pir_ute_paviljong_luminance')|float < 400 }}
  dark_outside:
    entity_id: sensor.pir_ute_paviljong_luminance
    friendly_name: Mørkt utendørs
    value_template: >-
      {{ states('sensor.pir_ute_paviljong_luminance')|float < 15 }} 
automation:
  trigger:
    - platform: state
      entity_id: sensor.dark_inside
      to: 'False'
      for: '00:02:00'
    - platform: time   # This is to make sure daytime is set at winter even if the sun is not up
      at: '08:00:00'   # Then we can still trigger light using luminance on top of day mode.

Can this be done in a more effective and code-neat way?
Any ideas and thoughts are most welcome :slight_smile:

I use the custom component called ‘Circadian Lighting’ (i think the official integration ‘Flux’ does something similar?) to control my lights brightness and colortemperature. Very satisfied.

1 Like

I have my own sensor that calculates brightness based on a linear equation and a lux sensor.

I just substitute brightness: "{{ states('sensor.calculated_light_brightness') }}" everywhere I turn a light on.

1 Like

Would you be willing to share that?

I use scenes for pretty much all my lighting (sans holiday lights) along with TOD sensors for modes. The only place I don’t do that is for my master bedroom lights and my office lights where I use Flux. I honestly don’t think I could do it any other way and get the same results.

1 Like

It’s pretty specific to my sensor values (BH1750, modified exposure time) and my preferences for outside light level vs inside, but sure:

- platform: template
  sensors:
    calculated_light_brightness:
      friendly_name: 'Calculated Light Brightness'
      value_template: >
          {{ [ [ (-0.1785 * states('sensor.outside_light_level')|float + 298.35)|int, 255 ]|min, 0 ]|max }}
      unit_of_measurement: "Int"

123 came up with a more readable version of ensuring the value is between 0 and 255, it will work but I haven’t tried it yet:

      value_template: >
          {{ [ 0, (-0.1785 * states('sensor.outside_light_level')|float + 298.35)|int, 255 ]|sort[1] }}

1 Like

Can you share some of the code you are using, just to give us an idea of how you put the logic together?
I ended up using one scene for each room, regarding the time of day an with/without motion detected.
This gives me 8(!) combinations per room, which is indeed a lot when it comes to code maintenance and error solving.
I am sure there is an easier way…?

Sure. I use NodeRed for most of my automations. It took me about a week to set everything up, but once I did, it became really easy to make modifications going forward.

To set it up, I have 5 TOD sensors (early morning, morning, afternoon, evening, night) and an input_boolean (input_boolean.good_night) that is triggered by an input_boolean that gets switched on by voice command. I also make heavy use of the Workday sensor as well.

- platform: workday
  country: US
  workdays: [mon, tue, wed, thu, fri]
  excludes: [sat, sun, holiday]
  add_holidays:
    - '2020-10-08'
    - '2020-10-09'
    - '2020-10-15'
    - '2020-10-16'

- platform: tod
  name:  Mode Manager - Early Morning
  after: sunrise
  after_offset: '-00:45'
  before: '08:00'

- platform: tod
  name: Mode Manager - Morning
  after: '08:00'
  before: '12:00'

- platform: tod
  name: Mode Manager - Afternoon
  after: '12:00'
  before: sunset
  before_offset: '-00:45'

- platform: tod
  name: Mode Manager - Evening
  after: sunset
  after_offset: '-00:45'
  before: '20:30'

- platform: tod
  name: Mode Manager - Night
  after: '20:30'
  before: '23:59'

Then for each room, I have scenes matching the TOD sensors (I only listed the first 3 as this is already a long post lol):

  name: Kitchen Off
  entities:
    group.kitchen_lights: false

- id: kitchen_morning
  name: Kitchen Morning
  entities:
    group.kitchen_sink_lights:
      state: true
      brightness: 128
    light.kitchen_stove_light:
      state: true
      brightness: 128
    light.kitchen_under_cabinet_lights:
      state: true
      brightness: 204
    light.kitchen_cabinet_lights:
      state: true
      brightness: 128
    light.kitchen_overhead_lights:
      state: true
      brightness: 77
    light.kitchen_bar_light_1:
      state: false
    light.kitchen_bar_light_2:
      state: true
      brightness: 77
    light.kitchen_bar_light_3:
      state: false

- id: kitchen_afternoon
  name: Kitchen Afternoon
  entities:
    group.kitchen_sink_lights:
      state: true
      brightness: 153
    light.kitchen_stove_light:
      state: true
      brightness: 153
    light.kitchen_under_cabinet_lights:
      state: true
      brightness: 204
    light.kitchen_cabinet_lights:
      state: true
      brightness: 153
    light.kitchen_overhead_lights:
      state: true
      brightness: 128
    light.kitchen_bar_light_1:
      state: false
    light.kitchen_bar_light_2:
      state: true
      brightness: 128
    light.kitchen_bar_light_3:
      state: false

Each room has a motion sensor(s) and/or contact sensor and each one is available in HA as a switch (so that I can turn on/off motion detection when I need to).

This is my kitchen flow.

When motion gets triggered, it goes through the top path into a function node which determines what mode the house is in and sets the lights accordingly.

When motion stops it goes through the bottom path and adjusts a timeout based upon what mode the house is in:

I’ve tried a few times to recreate this is native HA automations and never had very good success (granted, I haven’t tried it with the new changes in 0.114+). In NodeRed, it was easy for me to setup and maintain.

2 Likes

Hi!

@Code-in-progress, I adopted your strategy of controlling rooms with scenes.
I have my automations in Node Red already, but it was getting complicated setting individual brightness levels based on time of day through functions for all rooms.
This is much neater to maintain, thanks!

It is working well, but I could not get the get.global to work, instead I used the “Get Entities” node.
One more block but pretty neat solution I think.

I have not been able to get the stoptimer to work with individual timers however.
Would you mind sharing the code to your flow, or explain how it works?
I am a little confused because stoptimer doesn’t seem to have any input for the message with the different durations.

Thanks in advance (and hope you are still on here)! :slight_smile: