Automatic Dimming Light Timer

I used daenny’s Sun Event timers as an inspiration to create the following automatic dimming light timer:
I utilized some dummy sensors to calculate Unix time for simpler logic. I also used a dummy group to reduce the number of automations required. It is all set up as a package

sensor:
  - platform: template
    sensors:
      auto_outdoor_light_start:
        friendly_name: 'Lights on at '
        value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_slider.auto_outdoor_light_offset_on") | int)  * 60)  | timestamp_custom("%I:%M %p") }}'
      auto_outdoor_light_dim:
        friendly_name: 'Lights dim at '
        value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_midnight) + (states("input_slider.auto_outdoor_light_offset_dim") | int)  * 60)  | timestamp_custom("%I:%M %p") }}'
      auto_outdoor_light_stop:
        friendly_name: 'Lights off at '
        value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) - (states("input_slider.auto_outdoor_light_offset_off") | int)  * 60)  | timestamp_custom("%I:%M %p") }}'
# The following sensors are used to control the timing logic
      outdoor_light_on_unix_time:
        friendly_name: 'Unix Time On'
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_slider.auto_outdoor_light_offset_on") | int)  * 60)}}'
      outdoor_light_dim_unix_time:
        friendly_name: 'Unix Time Dim'
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_slider.auto_outdoor_light_offset_dim") | int)  * 60)}}'
      outdoor_light_off_unix_time:
        friendly_name: 'Unix Time Off'
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising) - (states("input_slider.auto_outdoor_light_offset_off") | int)  * 60)}}'
      
  - platform: time_date
    display_options:
      - 'time'

input_slider:
  auto_outdoor_light_offset_on:
    name: Minutes after sunset
    icon: mdi:timer
    initial: 0
    min: -60
    max: 60
    step: 10
  auto_outdoor_light_on_level:
    name: Auto on brightness
    icon: mdi:brightness-6
    initial: 80
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_dim:
    name: Minutes after midnight
    icon: mdi:timer
    initial: 30
    min: 0
    max: 120
    step: 10
  auto_outdoor_light_dim_level:
    name: Auto dim brightness
    icon: mdi:brightness-6
    initial: 20
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_off:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 60
    min: 0
    max: 240
    step: 10
  auto_outdoor_light_on_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_dim_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_off_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10

automation:
  - alias: 'auto_outdoor_light_time_setting'
    initial_state: True
    trigger:
      - platform: sun
        event: sunrise
      - platform: homeassistant
        event: start
    action:
      - service: input_slider.select_value
        entity_id: input_slider.auto_outdoor_light_on_time
        data_template:
          value: '{{states.sensor.outdoor_light_on_unix_time.state|int}}'
      - service: input_slider.select_value
        entity_id: input_slider.auto_outdoor_light_dim_time
        data_template:
          value: '{{states.sensor.outdoor_light_dim_unix_time.state|int}}'
      - service: input_slider.select_value
        entity_id: input_slider.auto_outdoor_light_off_time
        data_template:
          value: '{{states.sensor.outdoor_light_off_unix_time.state|int}}'

  - alias: 'auto_outdoor_light'
    initial_state: True
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      condition: or
      conditions:
        - condition: state
          entity_id: 'sun.sun'
          state: 'below_horizon'
    action:
      - service: light.turn_off
        data_template:
          entity_id: >
            {%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
              group.automatic_lights_outside
            {%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
              group.automatic_lights_outside_dummy
            {%else%}
              group.automatic_lights_outside_dummy
            {%endif%}
      - service: light.turn_on
        data_template:
          entity_id: >
            {%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
              group.automatic_lights_outside_dummy
            {%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
              group.automatic_lights_outside
            {%else%}
              group.automatic_lights_outside
            {%endif%}
          brightness_pct: > 
            {%if (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_off_time.state|int)%}
              0
            {%elif (as_timestamp(now()))|int>=(states.input_slider.auto_outdoor_light_dim_time.state|int)%}
              {{states.input_slider.auto_outdoor_light_dim_level.state|int}}
            {%else%}
              {{states.input_slider.auto_outdoor_light_on_level.state|int}}
            {%endif%}

group:

# This first group is used as a dummy group in the service call
  Automatic Lights Outside Dummy:
    name: AutomatiC Lights Outside Dummy

  Automatic Lights Outside:
    name: Automatic Lights Outside
    entities:
      - light.front_walkway_level

  Outdoor Timer Light:
    name: Outdoor Lights Timer Settings
    control: hidden
    entities:
    - automation.auto_outdoor_light
    - input_slider.auto_outdoor_light_on_level
    - input_slider.auto_outdoor_light_offset_on
    - sensor.auto_outdoor_light_start
    - input_slider.auto_outdoor_light_dim_level
    - input_slider.auto_outdoor_light_offset_dim
    - sensor.auto_outdoor_light_dim
    - input_slider.auto_outdoor_light_offset_off
    - sensor.auto_outdoor_light_stop
11 Likes

I ended up simplifying my code to get rid of the input sliders as a data storage. I was able to extract the time since last midnight and used that for the logic instead:

sensor:
  - platform: template
    sensors:
      auto_outdoor_light_start:
        friendly_name: 'Lights on at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_slider.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_dim:
        friendly_name: 'Lights dim at '
        value_template: '{{(7*3600+(states("input_slider.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_stop:
        friendly_name: 'Lights off at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_slider.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_start_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_slider.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_slider.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%M")|int*60}}'
      auto_outdoor_light_dim_seconds:
        value_template: '{{((7*3600+(states("input_slider.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%M")|int)*60}}'
      auto_outdoor_light_stop_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_slider.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_slider.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%M")|int*60}}'

  - platform: time_date
    display_options:
      - 'time'

input_slider:
  auto_outdoor_light_offset_on:
    name: Minutes after sunset
    icon: mdi:timer
    initial: 0
    min: -60
    max: 60
    step: 10
  auto_outdoor_light_on_level:
    name: Auto on brightness
    icon: mdi:brightness-6
    initial: 80
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_dim:
    name: Minutes after midnight
    icon: mdi:timer
    initial: 30
    min: 0
    max: 120
    step: 10
  auto_outdoor_light_dim_level:
    name: Auto dim brightness
    icon: mdi:brightness-6
    initial: 20
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_off:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 60
    min: 0
    max: 240
    step: 10

automation:

  - alias: 'auto_outdoor_light_off'
    initial_state: True
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_stop_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int) <= states.sensor.auto_outdoor_light_start_seconds.state|int}}'
        - condition: state
          entity_id: 'group.automatic_lights_outside'
          state: 'on'
    action:
      - service: light.turn_off
        entity_id: group.automatic_lights_outside


  - alias: 'auto_outdoor_light_on'
    initial_state: True
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_start_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)}}'
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)<=states.sensor.auto_outdoor_light_stop_seconds.state|int}}'
    action:
      - service: light.turn_on
        data_template:
          entity_id: 'group.automatic_lights_outside'
          brightness_pct: > 
            {%if states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)<=states.sensor.auto_outdoor_light_stop_seconds.state|int%}
              {{states.input_slider.auto_outdoor_light_dim_level.state|int}}
            {%else%}
              {{states.input_slider.auto_outdoor_light_on_level.state|int}}
            {%endif%}

group:

  Automatic Lights Outside:
    name: Automatic Lights Outside
    entities:
      - light.front_walkway_level

  Outdoor Timer Light:
    name: Outdoor Lights Timer Settings
    control: hidden
    entities:
    - automation.auto_outdoor_light_on
    - automation.auto_outdoor_light_off
    - input_slider.auto_outdoor_light_on_level
    - input_slider.auto_outdoor_light_offset_on
    - sensor.auto_outdoor_light_start
    - input_slider.auto_outdoor_light_dim_level
    - input_slider.auto_outdoor_light_offset_dim
    - sensor.auto_outdoor_light_dim
    - input_slider.auto_outdoor_light_offset_off
    - sensor.auto_outdoor_light_stop
3 Likes

Thanks for posting your scripts. I have installed them and they work great except for me, the midnight function starts at 8.30 in the morning. As I’m in the UK, I think it might be a time difference problem. Is there an easy way to correct this?

Hello,

The script works off of Unix time and in the sensors section at the top you need to adjust the offset time. In the code this is set to 7*3600 to adjust the time by 7 hours. Change the “7” to a value that makes it match your time zone.

Thanks, I put the offset to -1 and it seems to work.

Hello @Code,

thanks for sharing this with the community. I really appreciate it!

I’ve today decided to give this a shot and after a bit of fiddling, I think I’ve managed to get this up (I can’t say Up and Running yet). First of all, it looks like input_slider is deprecated and input_number needs to be used instead.

Even though my HA does not detect any syntax issue, it does not do what I would expect. It certainly did not turn on the lights when the time came. I’ve noticed something very weird that is happening with my test light: it will get automatically turned off when the clock ticks 5 min. If I switch it on at 00:17, it will get turned off at 00:20, if I turn it on at 00:20:01, it will remain on until 00:25:00, and then it will get switched off. Ain’t it funny? :slight_smile: if I tried to come up with a code with that objective, I would fail!! :slight_smile:

Any idea what may be going on? This is the code:

Automations:

- alias: 'auto_outdoor_light_time_setting'
  initial_state: True
  trigger:
    - platform: sun
      event: sunrise
    - platform: homeassistant
      event: start
  action:
    - service: input_number.select_value
      entity_id: input_number.auto_outdoor_light_on_time  
      data_template:
        value: '{{states.sensor.outdoor_light_on_unix_time.state|int}}'
    - service: input_number.select_value
      entity_id: input_number.auto_outdoor_light_dim_time
      data_template:
        value: '{{states.sensor.outdoor_light_dim_unix_time.state|int}}'
    - service: input_number.select_value
      entity_id: input_number.auto_outdoor_light_off_time
      data_template:
        value: '{{states.sensor.outdoor_light_off_unix_time.state|int}}'

- alias: 'auto_outdoor_light'
  initial_state: True
  trigger:
    - platform: time
      minutes: '/5'
      seconds: 00
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: 'sun.sun'
        state: 'below_horizon'
  action:
    - service: light.turn_off
      data_template:
        entity_id: >
          {%if (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_off_time.state|int)%}
            group.automatic_lights_outside
          {%elif (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_dim_time.state|int)%}
            group.automatic_lights_outside_dummy
          {%else%}
            group.automatic_lights_outside_dummy
          {%endif%}
    - service: light.turn_on
      data_template:
        entity_id: >
          {%if (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_off_time.state|int)%}
            group.automatic_lights_outside_dummy
          {%elif (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_dim_time.state|int)%}
            group.automatic_lights_outside
          {%else%}
            group.automatic_lights_outside
          {%endif%}
        brightness_pct: > 
          {%if (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_off_time.state|int)%}
             0
          {%elif (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_dim_time.state|int)%}
            {{states.input_number.auto_outdoor_light_dim_level.state|int}}
          {%else%}
            {{states.input_number.auto_outdoor_light_on_level.state|int}}
          {%endif%}

Configuration:

  - platform: template
    sensors:
      auto_outdoor_light_start:
        friendly_name: 'Lights on at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_dim:
        friendly_name: 'Lights dim at '
        value_template: '{{(-1*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_stop:
        friendly_name: 'Lights off at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_start_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%M")|int*60}}'
      auto_outdoor_light_dim_seconds:
        value_template: '{{((-1*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%M")|int)*60}}'
      auto_outdoor_light_stop_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%M")|int*60}}'

  - platform: time_date
    display_options:
      - 'time'

input_number:
  auto_outdoor_light_offset_on:
    name: Minutes after sunset
    icon: mdi:timer
    initial: 0
    min: -60
    max: 60
    step: 10
  auto_outdoor_light_on_level:
    name: Auto on brightness
    icon: mdi:brightness-6
    initial: 80
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_dim:
    name: Minutes after midnight
    icon: mdi:timer
    initial: 30
    min: 0
    max: 120
    step: 10
  auto_outdoor_light_dim_level:
    name: Auto dim brightness
    icon: mdi:brightness-6
    initial: 20
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_off:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 60
    min: 0
    max: 240
    step: 10
  auto_outdoor_light_on_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_dim_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_off_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10

I believe that this behaviour is tied to this portion of the automation part:

trigger:
- platform: time
  minutes: '/5'
  seconds: 00

For some reason, my HA is doing the math with veredict this condition is true

entity_id: >
          {%if (as_timestamp(now()))|int>=(states.input_number.auto_outdoor_light_off_time.state|int)%}
            group.automatic_lights_outside

This is what I have defined in HA:

and it is 00:20 in the night over here so I wouldn’t expect for that condition to be true now.

Any ideas os should I just assume that since I started this today, it needs to pass a few hours until the whole thing gets stable?

On @Maureen_Boyle 's note, I’m too using -1 and I’m in Spain. I believe it has something to do with DST?

Thanks in advance,

On my attempt to further debug what is going on, I discovered what Packages are and moved all the code into one single file within the Packages folder. Nice discovery :slight_smile:

This is how it is currently running setup my system:

So, this is the code which currently runs on my end:

core-ssh:/config/packages# cat automatic_dimming.yaml
sensor:
  - platform: template
    sensors:
      auto_outdoor_light_start:
        friendly_name: 'Lights on at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_dim:
        friendly_name: 'Lights dim at '
        value_template: '{{(-1*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_stop:
        friendly_name: 'Lights off at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_start_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%M")|int*60}}'
      auto_outdoor_light_dim_seconds:
        value_template: '{{((-1*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%M")|int)*60}}'
      auto_outdoor_light_stop_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%M")|int*60}}'

input_number:
  auto_outdoor_light_offset_on:
    name: Minutes after sunset
    icon: mdi:timer
    initial: 0
    min: -60
    max: 60
    step: 10
  auto_outdoor_light_on_level:
    name: Auto on brightness
    icon: mdi:brightness-6
    initial: 80
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_dim:
    name: Minutes after midnight
    icon: mdi:timer
    initial: 30
    min: 0
    max: 120
    step: 10
  auto_outdoor_light_dim_level:
    name: Auto dim brightness
    icon: mdi:brightness-6
    initial: 20
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_off:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 60
    min: 0
    max: 240
    step: 10
  auto_outdoor_light_on_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_dim_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10
  auto_outdoor_light_off_time:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 0
    min: 0
    max: 2000000000
    step: 10

automation:
- alias: 'auto_outdoor_light_time_setting'
  initial_state: True
  trigger:
    - platform: time
      minutes: '/5'
      seconds: 00
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{states.sensor.auto_outdoor_light_stop_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int) <= states.sensor.auto_outdoor_light_start_seconds.state|int}}'
      - condition: state
        entity_id: 'group.automatic_lights_outside'
        state: 'on'
  action:
    - service: light.turn_off
      entity_id: group.automatic_lights_outside

- alias: 'auto_outdoor_light'
  initial_state: True
  trigger:
    - platform: time
      minutes: '/5'
      seconds: 00
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{{states.sensor.auto_outdoor_light_start_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)}}'
      - condition: template
        value_template: '{{states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)<=states.sensor.auto_outdoor_light_stop_seconds.state|int}}'
  action:
    - service: light.turn_on
      data_template:
        entity_id: 'group.automatic_lights_outside'
        brightness_pct: > 
          {%if states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600-(now().strftime("%M")|int)*60-(now().strftime("%S")|int)<=states.sensor.auto_outdoor_light_stop_seconds.state|int%}
            {{states.input_number.auto_outdoor_light_dim_level.state|int}}
          {%else%}
            {{states.input_number.auto_outdoor_light_on_level.state|int}}
          {%endif%}

Big improvement: As soon as I rebooted the system around 22:00, the bulb switched on automatically!! and boy did it feel great!! :slight_smile:

I stayed up until the second condition was supposed to kick in, but the time passed well over 12:30 AM, and the bulb would not dim a bit.

Now it is 1:10AM and I just rebooted the system, and guess what: 5 minutes later the bulb dimmed down to 20%.

I’m under the impression that the code is good (well, I simply copied it from above and replaced input_slider with input_number) and for some reason it won’t react to the different automations defined, unless for some reason some purging mechanism kicks in (triggered by the reboot).

I will now leave the bulb on until the next condition at 5:39 today and report back.

Any hint or guidance on how to troubleshoot (any) code would be very welcome. In case it makes any difference, I’m running hass.io on hassOS.

Many thanks

Hello again,

when I woke up today, the bulb was off but on a further inspections in HA I discovered this:

There is something wrong with the logic and I can’t put my finger on it. I could assume that the bulb switched off 30’ later than what I had selected due to some offset hiding somewhere in the code, but what strikes dumb is the fact that it went on from 6:25AM to 7:00AM.

As I said, any help would be most appreciated.

Did you find a solution to this?

I’m now using a version of this without the dimming control and, so far, it seems to be correct.

I’d be interested to know what you found.

I’m looking to find an alternate way to trigger the automations as it’s a little clunky to have it running every 5 minutes when each one really only needs to run once a day.

Hello, I have been away for a long time. I recently updated Home Assistant and found issues with my code in the new version. Basically, you cannot use “now” in a sensor anymore which means that I needed to eliminate the sensors and move the now function into the conditions.

sensor:
  - platform: template
    sensors:
      auto_outdoor_light_start:
        friendly_name: 'Lights on at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_dim:
        friendly_name: 'Lights dim at '
        value_template: '{{(7*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_stop:
        friendly_name: 'Lights off at '
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%I:%M %p")}}'
      auto_outdoor_light_start_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_setting)+(states("input_number.auto_outdoor_light_offset_on")|int)*60)|timestamp_custom("%M")|int*60}}'
      auto_outdoor_light_dim_seconds:
        value_template: '{{((7*3600+(states("input_number.auto_outdoor_light_offset_dim")|int)*60)|timestamp_custom("%M")|int)*60}}'
      auto_outdoor_light_stop_seconds:
        value_template: '{{(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%H")|int*3600+(as_timestamp(states.sun.sun.attributes.next_rising)-(states("input_number.auto_outdoor_light_offset_off")|int)*60)|timestamp_custom("%M")|int*60}}'

  - platform: time_date
    display_options:
      - 'time'

input_number:
  auto_outdoor_light_offset_on:
    name: Minutes after sunset
    icon: mdi:timer
    initial: 0
    min: -60
    max: 60
    step: 10
  auto_outdoor_light_on_level:
    name: Auto on brightness
    icon: mdi:brightness-6
    initial: 80
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_dim:
    name: Minutes after midnight
    icon: mdi:timer
    initial: 30
    min: 0
    max: 120
    step: 10
  auto_outdoor_light_dim_level:
    name: Auto dim brightness
    icon: mdi:brightness-6
    initial: 20
    min: 0
    max: 100
    step: 5
  auto_outdoor_light_offset_off:
    name: Minutes before sunrise
    icon: mdi:timer
    initial: 60
    min: 0
    max: 240
    step: 10

automation:

  - alias: 'auto_outdoor_light_off'
    initial_state: True
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      condition: and
      conditions:
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_stop_seconds.state|int <= (now().strftime("%H")|int)*3600+(now().strftime("%M")|int)*60+(now().strftime("%S")|int) <= states.sensor.auto_outdoor_light_start_seconds.state|int}}'
        - condition: state
          entity_id: 'group.automatic_lights_outside'
          state: 'on'
    action:
      - service: light.turn_off
        entity_id: group.automatic_lights_outside


  - alias: 'auto_outdoor_light_on'
    initial_state: True
    trigger:
      - platform: time
        minutes: '/5'
        seconds: 00
    condition:
      condition: or
      conditions:
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_start_seconds.state|int <= (now().strftime("%H")|int)*3600+(now().strftime("%M")|int)*60+(now().strftime("%S")|int)}}'
        - condition: template
          value_template: '{{states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600+(now().strftime("%M")|int)*60+(now().strftime("%S")|int) <= states.sensor.auto_outdoor_light_stop_seconds.state|int}}'
    action:
      - service: light.turn_on
        data_template:
          entity_id: 'group.automatic_lights_outside'
          brightness_pct: > 
            {%if states.sensor.auto_outdoor_light_dim_seconds.state|int <= (now().strftime("%H")|int)*3600+(now().strftime("%M")|int)*60+(now().strftime("%S")|int) <=states.sensor.auto_outdoor_light_stop_seconds.state|int%}
              {{states.input_number.auto_outdoor_light_dim_level.state|int}}
            {%else%}
              {{states.input_number.auto_outdoor_light_on_level.state|int}}
            {%endif%}

group:

  Automatic Lights Outside:
    name: Automatic Lights Outside
    entities:
      - light.front_walkway_level
      - light.back_porch_level

  Outdoor Timer Light:
    name: Outdoor Lights Timer Settings
    control: hidden
    entities:
    - automation.auto_outdoor_light_on
    - automation.auto_outdoor_light_off
    - input_number.auto_outdoor_light_on_level
    - input_number.auto_outdoor_light_offset_on
    - sensor.auto_outdoor_light_start
    - input_number.auto_outdoor_light_dim_level
    - input_number.auto_outdoor_light_offset_dim
    - sensor.auto_outdoor_light_dim
    - input_number.auto_outdoor_light_offset_off
    - sensor.auto_outdoor_light_stop