Light Fade In

@david_kalbermatten First off, I just want to say that I so appreciate your having written this fading script—it’s been super handy!

And not to take away from your efforts at all, but I think that I miiight’ve come across a potential bug?

Known-good scenario:

If a given lamp has a starting value at 1% brightness and then I call this service, this works as expected. (The light fades from 1% to 5% over a span of 5 minutes.)

- service: python_script.lightfader
  data:
    entity_id: light.living_room_lamps
    transition: 300
    brightness: 5

Weirdness scenario:

On the other hand, if that lamp has a starting value of 0% brightness (off) and then I call that same service, that doesn’t seem to work? (In my testing, the light remains at 0%.)

- service: python_script.lightfader
  data:
    entity_id: light.living_room_lamps
    transition: 300
    brightness: 5

Would you happen to have any ideas there?

1 Like

Found the error

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 224, in execute
    exec(compiled.code, restricted_globals)
  File "lightfader.py", line 46, in <module>
  File "lightfader.py", line 12, in calc_delta
TypeError: bad operand type for abs(): 'NoneType'

The cause seems to be from me trying to fetch the brightness value without a fallback. So when the light is off (0) it doesn’t actually get a number but NoneType which cannot be interpreted by the math function abs()

This line should read:

actual_brightness = int(hass.states.get(entity_id).attributes.get('brightness') or 0)

and not:

actual_brightness = hass.states.get(entity_id).attributes.get('brightness')

Might be that it used to have the brightness attribute even for lights that were turned off and something changed on the side of Home Assistant :smiley:

I updated my post above accordingly. Thanks for bringing this to my attention!

1 Like

@david_kalbermatten Thanks bunches for nailing that down—and for replying so quickly! You’re a champ!

@david_kalbermatten Hello again, David!

I seem to be getting some unusual results when I try to fade multiple lights in parallel?

Known-good scenario:

If I have three lights that each start at 0% (off), and if I set them to fade to 10% over a span of 25 seconds, this works as expected.

parallel:
  - service: python_script.lightfader
    data:
      entity_id: light.living_room_lamps
      brightness: 10
      transition: 25
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_left
      brightness: 10
      transition: 25
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_right
      brightness: 10
      transition: 25

Weirdness scenario 1:

On the other hand, if I have three lights that each start at 0% (off), and if I set them to fade to 10% over a span of 20 seconds, then that doesn’t quite seem to work?

In this situation, one of the lights usually ends up at 10%, and the remaining two end up at 6%.

parallel:
  - service: python_script.lightfader
    data:
      entity_id: light.living_room_lamps
      brightness: 10
      transition: 20
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_left
      brightness: 10
      transition: 20
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_right
      brightness: 10
      transition: 20

Weirdness scenario 2:

Alternately, if I have three lights that each start at 0% (off), and if I set them to fade to 10% over a span of 10 seconds, that also produces some weird results?

In this situation, all three lights make it to 2% but then stop fading further.

parallel:
  - service: python_script.lightfader
    data:
      entity_id: light.living_room_lamps
      brightness: 10
      transition: 10
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_left
      brightness: 10
      transition: 10
  - service: python_script.lightfader
    data:
      entity_id: light.dining_room_floor_lamp_right
      brightness: 10
      transition: 10

Would you happen to have any ideas there?