Dimming lights over time

Hey to all!

I am a new guy at HA, just came from Hubitat elevation. I know there is a learning curve to get to use HA, but one of my automations is keeping me from turning the Hubitat Hub off…

Can someone help me set this automation?

My daughter has a Phillips Hue bulb and a xiaomi button by her bed.
When she goes to sleep, she presses the button, her light turns on at 50% brightness. It stays at that brightness for 15 min and then starts to dimm 5% each 5 minutes until it gets to 3% brightness.
When she wakes up in the morning, she presses the button and the light turns off.

Now the tricky part:
There are days when she takes longer to fall asleep, she is used to pressing the button again to toggle the light off and press it again so that the light will come back at 50% and restart the dimming process.

I just cant make it work!

Previous tries…
I did it with call service - lights:toggle - set brightness 45% / wait for 5 min / call service - lights: toggle - brightness 40% and so on… For some reason the light fails to return to 50% when the button is pressed again or fails to turn off.

Created helpers (timers) that would cycle each 5 min and trigger a brightness reduction… but this did not work at all…

Used a Hue blueprint link but it fails to dimm.

Ideas? Please help!

Hi Gustavo, will give it a shot to help you with some rough ideas (I’m not that good yet in doing this virtually)

The trigger is pushing the button.
With multiple actions that have conditions (state of the light) with a template

service: light.turn_on
data:
  transition: "{{ 1 if is_state('light.bedroomright', 'on') else 10 }}"
  brightness_pct: "{{ 10 if is_state('light.bedroomright', 'off') else 75 }}"
target:
  entity_id:
    - light.bedroomdoor
    - light.bedroomleft

You can also fire scripts from within the automation (service turn on script)

If that’s an automation you created in Home Assistant, post it (in YAML format).

If it’s a rule you created in Hubitat, post screenshots of it.

I got it working, maybe a little complex but it works with an automation and a script.

Automation:

alias: testlight
description: ""
trigger:
  - platform: state
    entity_id:
      - <your button>
    from: "off"
    to: "on"
  - platform: state
    entity_id:
      - <your button>
    from: "on"
    to: "off"
condition: []
action:
  - if:
      - condition: state
        entity_id: <the light>
        state: "off"
        enabled: true
    then:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          entity_id: <the light>
      - delay:
          hours: 0
          minutes: 15
          seconds: 0
          milliseconds: 0
      - service: script.turn_on
        data: {}
        target:
          entity_id: <script.testlightdim>
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: <the light>
mode: single

Script:

alias: testlightdim
sequence:
  - repeat:
      count: "9"
      sequence:
        - delay:
            hours: 0
            minutes: 5
            seconds: 0
            milliseconds: 0
        - service: light.turn_on
          data:
            brightness_step_pct: -5
          target:
            entity_id: <the light>
mode: single

1 Like

I think this can be simplified to an automation with two “Light: Turn on” service calls.

  1. Brightness value: 50%
  2. Wait 15 minutes
  3. Brightness value: 3%, Transition: 2820 seconds (i.e. 47 minutes)

If you make the trigger for the automation the button and set the automation mode to “restart”, I think that should do what you need?

Hi to all!

I gave up doing it in the beutifull way… and decided to go “old school”.

Created an automation linking the button to the light just to toggle it.
Created a second automation that triggers a timer. This timer is used as a condition for the automation to keep running.
Created a third automation that reloads the second automation and cancels the timer if the light is turned off.

First automation:
Just toggles the light by the push of the button

Second automation:

alias: Luz noturna (dimmer thru timer)
description: >-
  Sets brightness to 50% when turned on and starts timer. Decreases brightness
  thru time, if the timer is active and the light is on.  
trigger:
  - platform: state
    entity_id:
      - light.luz_noturna_light
    from: "off"
    to: "on"
    id: Light turned ON
  - platform: state
    entity_id:
      - light.luz_noturna_light
    from: "on"
    to: "off"
    id: Light turned OFF
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Light turned ON
        sequence:
          - service: timer.start
            data:
              duration: "2145"
            target:
              entity_id: timer.timer_luz_noturna
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id: light.luz_noturna_light
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 500
          - if:
              - condition: state
                entity_id: timer.timer_luz_noturna
                state: active
              - condition: state
                entity_id: light.luz_noturna_light
                state: "on"
            then:
              - delay:
                  hours: 0
                  minutes: 15
                  seconds: 0
                  milliseconds: 0
              - service: light.turn_on
                data:
                  brightness_pct: 45
                  transition: 10
                target:
                  entity_id: light.luz_noturna_light
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.luz_noturna_light
              - service: timer.cancel
                data: {}
                target:
                  entity_id: timer.timer_luz_noturna
          - if:
              - condition: state
                entity_id: timer.timer_luz_noturna
                state: active
              - condition: state
                entity_id: light.luz_noturna_light
                state: "on"
            then:
              - delay:
                  hours: 0
                  minutes: 8
                  seconds: 0
                  milliseconds: 0
              - service: light.turn_on
                data:
                  brightness_pct: 40
                  transition: 10
                target:
                  entity_id: light.luz_noturna_light
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.luz_noturna_light
              - service: timer.cancel
                data: {}
                target:
                  entity_id: timer.timer_luz_noturna
          - if:
              - condition: state
                entity_id: timer.timer_luz_noturna
                state: active
              - condition: state
                entity_id: light.luz_noturna_light
                state: "on"
            then:
              - delay:
                  hours: 0
                  minutes: 5
                  seconds: 0
                  milliseconds: 0
              - service: light.turn_on
                data:
                  brightness_pct: 30
                  transition: 10
                target:
                  entity_id: light.luz_noturna_light
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.luz_noturna_light
              - service: timer.cancel
                data: {}
                target:
                  entity_id: timer.timer_luz_noturna
          - if:
              - condition: state
                entity_id: timer.timer_luz_noturna
                state: active
              - condition: state
                entity_id: light.luz_noturna_light
                state: "on"
            then:
              - delay:
                  hours: 0
                  minutes: 3
                  seconds: 0
                  milliseconds: 0
              - service: light.turn_on
                data:
                  brightness_pct: 15
                  transition: 10
                target:
                  entity_id: light.luz_noturna_light
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.luz_noturna_light
              - service: timer.cancel
                data: {}
                target:
                  entity_id: timer.timer_luz_noturna
          - if:
              - condition: state
                entity_id: timer.timer_luz_noturna
                state: active
              - condition: state
                entity_id: light.luz_noturna_light
                state: "on"
            then:
              - delay:
                  hours: 0
                  minutes: 2
                  seconds: 0
                  milliseconds: 0
              - service: light.turn_on
                data:
                  brightness_pct: 2
                  transition: 10
                target:
                  entity_id: light.luz_noturna_light
            else:
              - service: light.turn_off
                data: {}
                target:
                  entity_id: light.luz_noturna_light
              - service: timer.cancel
                data: {}
                target:
                  entity_id: timer.timer_luz_noturna
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.timer_luz_noturna
      - conditions:
          - condition: trigger
            id:
              - Light turned OFF
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.timer_luz_noturna
mode: single

Third automation:

alias: SYSTEM - ON/OFF Luz Noturna automation
description: ""
trigger:
  - platform: state
    entity_id:
      - light.luz_noturna_light
    from: "on"
    to: "off"
condition: []
action:
  - service: timer.cancel
    data: {}
    target:
      entity_id: timer.timer_luz_noturna
  - service: automation.turn_off
    data:
      stop_actions: true
    target:
      entity_id: automation.nova_tentativa_luz_noturna
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - service: automation.turn_on
    data: {}
    target:
      entity_id: automation.nova_tentativa_luz_noturna
mode: single

Not great, but it works…

In the end, it could be easier if there was an action like “set brightness level”. I can only set brightness using lights:on or lights:toggle

Don’t you have this?!?

Hi @Nick4 !

I do, but to get to that I have to: Call Service - and choose lights toggle or lights ON. There’s no way to set brightness levels without influencing the light’s status.
Because of that, if my daughter decides to turn off the light while the automation was still running; the bulb would turn on again; when the time to change brightness arrives.

Is there any other way to set brightness levels, without having to call the bulb ON or Toggle?

I’ve done some testing, and maybe this is what you are looking for:

On a button press ( you’ll have to define that trigger)

It will check the current state.
if on it will go off
if off, it will set it once to 50% and wait a second
then it will repeat 9 times and at every cycle it checks if the light is on if yes, a decrease of 5% over a time of 2,5 minutes if no. stop it.

alias: dimming lights
description: Dim Lights if turned on with button. Turn off if on when button pressed
trigger: []
condition: []
action:
  - if:
      - condition: device
        type: is_on
        device_id: ### <-- Select lamp
        entity_id: ### <-- Select lamp
        domain: light
    then:
      - service: light.turn_on
        data:
          brightness_pct: 50
        target:
          entity_id: light.luz_noturna_light
      - delay:
          hours: 0
          minutes: 0
          seconds: 5
          milliseconds: 0
      - repeat:
          count: "9"
          sequence:
            - if:
                - condition: device
                  type: is_on
                  device_id: ### <-- Select lamp
                  entity_id: ### <-- Select lamp
                  domain: light
              then:
                - service: light.turn_on
                  data:
                    brightness_step_pct: -5
                    transition: 179
                  target:
                    entity_id: light.luz_noturna_light
              else:
                - stop: Light is not on.
                  error: true
  - if:
      - condition: device
        type: is_off
        device_id: ### <-- Select lamp
        entity_id: ### <-- Select lamp
        domain: light
    then:
      - type: turn_off
        device_id: ### <-- Select lamp
        entity_id: ### <-- Select lamp
        domain: light
mode: single


1 Like

What’s the problem in changing the brightness that way?

Hi @Nick4 !

Again… I’m new to this and not used to having to turn on a ligtht every time I want to change its brightness.

In this automation the issue was that if the light was turned off while the automation was still running, it would turn back on when the time came.
Using a brightness change without having to turn the light on would prevent that.

But again. It can be done by just pausing or triggering the automation….

Thanks for the help!

Thank you this script (did not try automation) worked for me, since Tasmota only support limited transition times of max 30seconds or something along those lines.