"Fade In" Light effect - Lessons Learned, Failures, and some success?

With much frustration, I decided to do a lot of repetitive code.

I was unable to perform the loop as expected, and my bulbs did not support transition.
Here is some automation I have for when I am home, about an hour before the sun goes down, my main light in the living room will increase it’s brightness every 10ish minutes until 5 minutes before, when it’s at full capacity.

I’ve also posted below some failed attempts and the reason I decided to go this route.
Feedback, comments, suggestions are appreciated!

Automation

########################################################
## Gradually increase living room light.
########################################################
- alias: 'Living Room 10'
  trigger:
    platform: sun
    event: sunset
    offset: "-1:00:00"
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: light.livingroom_lamp_level_3_0
        state: 'off'
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.LivingRoomLamp10

- alias: 'Living Room 25'
  trigger:
    platform: sun
    event: sunset
    offset: "-00:50:00"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.light.livingroom_lamp_level_3_0.attributes.brightness < 25 }}'
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.LivingRoomLamp25

- alias: 'Living Room 50'
  trigger:
    platform: sun
    event: sunset
    offset: "-00:30:00"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.light.livingroom_lamp_level_3_0.attributes.brightness < 50 }}'
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.LivingRoomLamp25

- alias: 'Living Room 100'
  trigger:
    platform: sun
    event: sunset
    offset: "-00:15:00"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.light.livingroom_lamp_level_3_0.attributes.brightness < 100 }}'
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.LivingRoomLamp100

- alias: 'Living Room 250'
  trigger:
    platform: sun
    event: sunset
    offset: "-00:15:00"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.light.livingroom_lamp_level_3_0.attributes.brightness < 100 }}'
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: scene.turn_on
    entity_id: scene.LivingRoomLamp250

Scenes

- name: LivingRoomLamp10
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 10

- name: LivingRoomLamp25
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 25

- name: LivingRoomLamp50
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 50

- name: LivingRoomLamp100
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 100

- name: LivingRoomLamp150
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 150

- name: LivingRoomLamp250
  entities:
    light.livingroom_lamp_level_3_0:
      state: on
      brightness: 250

FAILED ATTEMPT ONE
This one, I feel like had a lot of potential. I think its just my syntax. If anyone knows syntax, I would love feedback!

Script

light_fade_in:
  alias: Light Fade In
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.livingroom_lamp_level_3_0
        brightness: 5
    - delay:
        seconds: 1
    - service: script.turn_on
      data:
        entity_id: script.plus_five


plus_five:
  alias: Add Five to Brightness
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.livingroom_lamp_level_3_0
        **## Here is the line that is causing me issues... in programming terms, I just want to do the equivilent of "light.brightness += 5"**
        **brightness: "{{ states.light.livingroom_lamp_level_3_0.attributes.brightness|int + 5 }}"**
    - delay:
        seconds: 1
    - service: script.turn_on
      data:
        entity_id: script.light_loop

light_loop:
  alias: Light Loop
  sequence:
    - delay:
        seconds: 1
    - service: script.turn_on
      data:
        entity_id: script.plus_five

FAILED ATTEMPT TWO
This one is a little to “jerky” for my liking. It did not feel smooth or natural. Maybe tuning the brightness values and the delay it could be better.

################
# Fade in light
################

light_fade_in:
  alias: Light Fade In
  sequence:
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 5
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 6
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 7
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 8
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 9
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 10
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 11
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 12
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 13
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 14
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 15
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 16
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 17
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 18
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 19
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 20
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 21
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 22
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 23
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 24
    - delay:
        seconds: 0.4
    - service: light.turn_on
      entity_id: light.livingroom_lamp_level_3_0
      data_template:
        brightness: 25
    - delay:
        seconds: 0.4
2 Likes

Can’t you just use transition time and set the time appropriately? Or maybe it just works with hue lights?

Else I’d recommend doing this in Appdaemon instead.

/R

It looks like your 100 and 250 levels both get executed 15 minutes before sunset. I’m assuming that’s a mistake?

Anyway, one of the things I love about programming is there are so many different ways to solve the same problem. In your current solution, you’re using multiple triggers to turn on the lights. Which is good if you come home half-way through the hour before sunset.

If you don’t care about the automation firing off if you get home during the sunset hour, another approach would be to use one automation and one script, like this:

################
# Fade in light
################

light_fade_in:
  alias: Light Fade In
  sequence:
    - service: scene.turn_on
      entity_id: scene.LivingRoomLamp10
    - delay:
        minutes: 10 
    - service: scene.turn_on
      entity_id: scene.LivingRoomLamp25
    - delay:
        minutes: 20
    - service: scene.turn_on
      entity_id: scene.LivingRoomLamp50
    - delay:
        minutes: 15
    - service: scene.turn_on
      entity_id: scene.LivingRoomLamp100
    - delay:
        minutes: 10
    - service: scene.turn_on
      entity_id: scene.LivingRoomLamp250

Also, I believe brightness: 255 is 100%. 250 is just under 100% brightness for your lamp.

Now, if you wanted something where the lights would turn on to the right level if you got home in the sunset hour, and could do it in one automation rule (no scenes or scripts), I would look into templates. Gets a bit tricky, but you might be able to do something like this (this is an example, can’t guarantee it will work and haven’t tested it):

########################################################
## Gradually increase living room light.
########################################################
- alias: 'Living Room Light Up'
  trigger:
    - platform: time
      minutes: '/10' #every 10 minutes
    - platform: state
      entity_id: device_tracker.phone_1
      from: 'not_home'
      to: 'home'
    platform: sun
    event: sunset
    offset: "-1:00:00"
  condition:
    condition: and
    conditions:
      - condition: sun
        before: sunset
      - condition: sun
        after: sunset
        after_offset: '-01:00:00' #execute 1 hour before sunset, but only before sunset
      - condition: or
        conditions:
        - condition: state
          entity_id: 'device_tracker.phone1_s5'
          state: 'home'
        - condition: state
          entity_id: 'device_tracker.phone2_s5'
          state: 'home'
  action:
    service: light.turn_on
    entity_id: light.livingroom_lamp_level_3_0
    data: 
      brightness: >
        {% if as_timestamp(states.sun.next_setting) - as_timestamp(now()) < 300  %} #5 minutes
          255
        {% if as_timestamp(states.sun.next_setting) - as_timestamp(now()) < 600  %} #10 minutes
          100
        {% if as_timestamp(states.sun.next_setting) - as_timestamp(now()) < 1800  %} #30 minutes
          25
        {% if as_timestamp(states.sun.next_setting) - as_timestamp(now()) < 3000  %} #50 minutes
          10
        {% endif %}

That will fire off every 10 minutes, or when someone gets home. It then checks the sun component, and tries to work out how many minutes are left until sunset. If there’s less than 50 minutes to sunset, set the light to 10. If it’s less than 30 minutes, so the light to 25, etc etc.

2 Likes

Phil,

Thanks for this. There is a lot of great information in here.

I think the issue I am facing is still unsolved though: I really want to be able to say light.brightness++ and have that iterate every second until it reaches a threshold.

I looked at Robban’s suggestion for Appdaemon. This may be an option, however, I was expecting to see AppDaemon injecting new entity_ids into hass for me to be able to trigger on certain events. I’ve not had success setting it up. Robban, if you have any videos or tutorials on setting this up, I’d greatly appreciate it. I’m learn by doing, and the documentation was more for set up and creating the automations, but not so much for integration into home-assistant.

I think you’re asking to generic a question, if you can be more precise I can of course help you.

My Philips Hue Lights get discovered automatically

/R

Thank you for this. Going to adapt to what I’ve got hopefully :slight_smile:

Anyone here willing to share their fade effect for non-hue lights? That’d be much appreciated.

I quite like the idea of the script that you can call, but dunno how to make it clean.

My idea would be to create a script like this:

light_fade_in:
  alias: Light Fade In
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.livingroom_lamp_level_3_0
        data_template:
          brightness: "{{ brightness | int +5 }}"
    - delay:
        seconds: 1
    - service: script.light_fade_in
      data:
        brightness: '{{state.attributes.brightness }}'

and the automation would be something like :

automation:
  trigger:
    platform: state
    entity_id: sensor.whatever
    from: 'off'
    to: 'on'
  action:
    - service: script.light_fade_in
      data:
        brightness: '{{state.attributes.brightness }}'

Would that be possible?

1 Like

I have used this in the past:

  wakeup:
    alias: 'Lights - Wakeup'
    sequence:
      - service: light.turn_on
        data:
          entity_id: light.room_light  
          brightness: '8'
      - delay: 
          minutes: 2
      - service: script.turn_on
        entity_id: script.generictransition 
        data:
          variables: 
            lightname: 'room_light'
      
  ## Light must be on first ***
  generictransition:
    alias: 'Lights - Generic Transition'
    sequence:
      - condition: and 
        conditions:
          - condition: template
            value_template: '{{ states.light[lightname].state == "on" }}'
          - condition: template
            value_template: '{{ states.light[lightname].attributes.brightness < 252 }}'
      - service: light.turn_on
        data_template:
          entity_id: '{{ "light."~lightname }}'
          brightness: '{{ states.light[lightname].attributes.brightness + 7 | round(0) }}'
      - delay: 
          seconds: 1
      - service: script.turn_off
        entity_id: script.generictransition
      - delay: 
          seconds: 3
      - service: script.turn_on
        entity_id: script.generictransition
        data_template:
          variables: 
            lightname: '{{ lightname }}'

but the recursive call to the script doesn’t seem to work anymore.

This allowed me to pass any light name into the generictransition script. I found that, when it was working, I had to turn off the script before I could call it again (as you can see).

This is what I’ve been using for a while, the only difference is I have it fade in slowly, usually over the course of an hour, but depending on how close to sunset it is it starts at a different brightness level.

sequence:
  - service: light.turn_on
    entity_id: light.living_room_lights
    data_template:
      brightness: >
       {%- if is_state("light.living_room_lights", "off") -%}
         {%-if(((as_timestamp(states.sun.sun.attributes.next_setting)) - as_timestamp(now())) / 60 /60) >= 1 and is_state("sun.sun", "above_horizon") %}
         20
         {% elif (((as_timestamp(states.sun.sun.attributes.next_setting)) - as_timestamp(now())) / 60 /60) >= 0 and is_state("sun.sun", "above_horizon") %}
         40
         {%else%}
         80
         {%endif%}
       {%else%}
        {{(states.light.living_room_lights.attributes.brightness | int) + 5}}{%endif%}
  - delay:
      minutes: 5
  - condition: and
    conditions:
      - condition: template
        value_template: '{{(states.light.living_room_lights.attributes.brightness | int) < 80}}'
      - condition: template
        value_template: '{{is_state("light.living_room_lights","on")}}'
  - service: script.turn_off
    entity_id: script.living_room
  - service: script.turn_on
    entity_id: script.living_room
3 Likes

Was able to do this using Python Script:

Posted the solution here: Light Fade In