Sunrise and sunset for canary breeding

Hi, i’m new here, i trying tò setup a manual sunrise/sunset for my gf’s father who’s a canary breeder and nerds tò shorten/lenghten the days by creating and artificiali sunrise and sunset

Toghether with chatgpt i set up some variables in configuration, and then set up this automation:

`- alias: “Sunrise and Sunset Simulation”
description: “Gradually adjusts brightness to simulate sunrise and sunset.”
mode: restart # Ensures the automation restarts if triggered while still running
trigger:
# Trigger for sunrise simulation based on manual input time
- platform: template
value_template: “{{ states(‘sensor.time’) == states(‘input_datetime.alba_orario’)[0:5] }}”
id: “sunrise”

# Trigger for sunset simulation based on manual input time
- platform: template
  value_template: "{{ states('sensor.time') == states('input_datetime.tramonto_orario')[0:5] }}"
  id: "sunset"

action:
- choose:
# Sunrise Simulation
- conditions:
- condition: trigger
id: “sunrise”
sequence:
# Define variables for duration, step count, and brightness increment
- variables:
total_duration: “{{ states(‘input_number.alba_durata’) | int * 60 }}” # Convert minutes to seconds
step_count: 50 # Number of brightness steps
brightness_increment: “{{ 255 / step_count }}” # Increment per step
step_duration: “{{ total_duration / step_count }}” # Duration per step

        # Turn on the light at the lowest brightness
        - service: light.turn_on
          target:
            entity_id: light.e27_2018_philips_pantografo
          data:
            brightness: 1

        # Gradually increase brightness over the specified duration
        - repeat:
            count: "{{ step_count }}"  # Repeat for the specified number of steps
            sequence:
              - service: light.turn_on
                target:
                  entity_id: light.e27_2018_philips_pantografo
                data:
                  brightness: "{{ [state_attr('light.e27_2018_philips_pantografo', 'brightness') | int(default=1) + brightness_increment | int, 255] | min }}"
                  transition: "{{ step_duration | int }}"
              - delay:
                  seconds: "{{ step_duration | int }}"  # Wait for the transition to complete before next step

    # **Sunset Simulation**
    - conditions:
        - condition: trigger
          id: "sunset"
      sequence:
        # Define variables for duration, step count, and brightness decrement
        - variables:
            total_duration: "{{ states('input_number.tramonto_durata') | int * 60 }}"  # Convert minutes to seconds
            step_count: 50  # Number of brightness steps
            brightness_decrement: "{{ 255 / step_count }}"  # Decrement per step
            step_duration: "{{ total_duration / step_count }}"  # Duration per step

        # Gradually decrease brightness over the specified duration
        - repeat:
            count: "{{ step_count }}"  # Repeat for the specified number of steps
            sequence:
              - service: light.turn_on
                target:
                  entity_id: light.e27_2018_philips_pantografo
                data:
                  brightness: "{{ [state_attr('light.e27_2018_philips_pantografo', 'brightness') | int(default=255) - brightness_decrement | int, 0] | max }}"
                  transition: "{{ step_duration | int }}"
              - delay:
                  seconds: "{{ step_duration | int }}"  # Wait for the transition to complete before next step

        # Turn off the light completely at the end of the sunset simulation
        - service: light.turn_off
          target:
            entity_id: light.e27_2018_philips_pantografo`

Which does the job, but in the log the seem tò not shut down (It stops dimming the lights, but in the log It shows as ongoing even After hours). Me and chatgpt can’t seem tò find why

Can you spot anything?

Thank you

Try adding an UNTIL clause like this:

# Turn on the light at the lowest brightness
        - service: light.turn_on
          target:
            entity_id: light.e27_2018_philips_pantografo
          data:
            brightness: 1

        # Gradually increase brightness over the specified duration
        - repeat:
            count: "{{ step_count }}"  # Repeat for the specified number of steps
            sequence:
              - service: light.turn_on
                target:
                  entity_id: light.e27_2018_philips_pantografo
                data:
                  brightness: "{{ [state_attr('light.e27_2018_philips_pantografo', 'brightness') | int(default=1) + brightness_increment | int, 255] | min }}"
                  transition: "{{ step_duration | int }}"
              - delay:
                  seconds: "{{ step_duration | int }}"  # Wait for the transition to complete before next step
            until:
              - condition: or
                conditions:
                  - condition: template
                    value_template: "{{ state_attr('light.e27_2018_philips_pantografo', 'brightness') == 0 }}"
                  - "{{ repeat.index >= step_count }}"
                  - "{{ states.light.e27_2018_philips_pantografo.state == 'off' }}"

You can adjust the conditions to your liking.

*** Note that the indention of UNTIL is at the same level as COUNT and SEQUENCE, not at the REPEAT level.