Sunrise Simulator. Wake up gently to light

Wow good catch! It was a mistake, i corrected it. You only need to pass the variables from above

Not sure if I’m using this properly.

  • Copy/Pasted the above script into a new script.
  • Created a new automation that triggers @ 6 a.m.
    • Action =
service: script.mysunrisescriptname
data:
  duration: 10
  target: 
    - light.nightstand
  brightness: 100

The result is all my lights (and not just the single entity) perform the action.

Any ideas?

Mine is working; however, the transition is very abrupt. Not very smooth transitioning to brightness. Not sure if it is a zigbee coordinator thing or not. I was using phillps hue in the pass and notice the transition up/down when controlling the brightness is better. Also, my light is ikea non rgb.

Hello sorry for the delayed answer. Please double check if you have misspelled the entity

This script is very nice and it works correctly, is there a way to interrupt it while it’s running?
I can’t figure out how to do it. If it’s possible, I would like to create an automation that stops the script if a light bulb in the bedroom is turned on, because it means someone has woken up.

I I’ve been using this blueprint for a few years to do a similar thing, but based in my phone’s alarm. It’s probably my favourite smart home automation:

You can call the script.turn_off service

Thank you for telling me about this command, I didn’t know it.
This way I partially solved the problem, but not completely. I think it doesn’t always work because there is a delay before the system detects that the light has been turned off through Alexa, and if a light intensity variation is made by the script during that time, the light turns back on.

We should modify the script by adding a check at each intensity variation:

  • If the light bulb is still on, the execution should continue normally.
  • If the light bulb is off (it could have been turned off by Alexa, Homeassistant, the proprietary app, etc.), the cycle should be interrupted and nothing else should be done.

I tried to modify it myself, but I wasn’t successful.

I had already tried this blueprint, but it seems that the initial and final colors cannot be adjusted.
I really like this script (Sunrise Simulator) because it transitions from red to yellow, simulating a real sunrise.

I was able to modify the script as I need it, unfortunately I don’t know how to use the name of the light coming from the automation parameters so I entered the name of the light directly inside the script.

These are the pieces of code I added:

repeat:
  count: "1"
  sequence:
    - service: light.turn_on
      data:
        brightness_pct: |
          {{ iif(repeat.index < 100, repeat.index, 100) }}
        color_temp: >
          {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) * 2.5 *
          (100/maxbri) ) ) | int, 185 ) }}
        transition: |
          {{ ( ( dur * 60) / maxbri ) | int }}
      target:
        entity_id: |
          {{ temp }}
    - delay:
        milliseconds: |
          {{ (  ( ( dur * 60) / maxbri ) * 1000
           ) | int }}

repeat:
  count: "1"
  sequence:
    - service: light.turn_on
      data:
        rgb_color: |
          {% if repeat.index <= maxbri/6 %}
            [255,0,0]
          {% elif repeat.index <= (maxbri/6) * 2  %}
            [255,77,0]
          {% elif repeat.index <= (maxbri/6) * 3 %}
            [255,103,0]
          {% elif repeat.index <= (maxbri/6) * 4 %}
            [255,129,0]  
          {% else %}
            [255,167,0]
          {% endif %}
        brightness_pct: |
          {{ iif(repeat.index < 100, repeat.index, 100) }}
        transition: |
          {{ (  ( dur * 60) / maxbri  ) | int }}
      target:
        entity_id: |
          {{ color }}
    - delay:
        milliseconds: |
          {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}

condition: state
entity_id: light.sleeproom
state: "on"
enabled: true

This is the result in case someone needs it or wants to make it parametric:

alias: Sunrise Simulator Manual with power off support
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              count: "1"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000
                       ) | int }}
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri }}"
                - condition: state
                  entity_id: light.sleeproom
                  state: "on"
                  enabled: true
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              count: "1"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri }}"
                - condition: state
                  entity_id: light.sleeproom
                  state: "on"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and states[sun].attributes.supported_color_modes | select('match', 'color_temp') | list | count > 0 
            and states[sun].attributes.supported_color_modes | count == 1 %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes
                or 'xy' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single
icon: mdi:weather-sunset-up

Could you test this script:

alias: Sunrise Simulator Manual
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri\n      and not ( repeat.index > 1 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', temp)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != temp | count ) }}"
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: >
                      {{ (  ( ( states('input_number.sunrise_sim_time') |
                      int(10) * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: "{{ repeat.index <= maxbri\n      and not ( repeat.index > 1 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', color)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != color | count ) }}"
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'color_temp' in states[sun].attributes.supported_color_modes
            and states[sun].attributes.supported_color_modes | count == 1 %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single

I tried the script but something isn’t working, the light doesn’t turn on.
I downloaded the track if it can be useful.

Trace
{
  "trace": {
    "last_step": "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0",
    "run_id": "71d64fc8753c9e67248d51d1735e6134",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2023-05-15T16:49:49.162151+00:00",
      "finish": "2023-05-15T16:49:49.173542+00:00"
    },
    "domain": "script",
    "item_id": "simulatore_alba_con_supporto_spegnimento",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2023-05-15T16:49:49.166057+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.simulatore_alba_con_supporto_spegnimento",
              "state": "off",
              "attributes": {
                "last_triggered": "2023-05-15T16:49:30.252331+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Sunrise Simulator Manual"
              },
              "last_changed": "2023-05-15T16:49:30.267123+00:00",
              "last_updated": "2023-05-15T16:49:30.267123+00:00",
              "context": {
                "id": "01H0G52AM4WV16MJZ8BKNPD7Z4",
                "parent_id": "01H0G52AM226CYHKSHQTGWP7PW",
                "user_id": null
              }
            },
            "duration": 2,
            "target": [
              "light.balcone_camera"
            ],
            "brightness": 75,
            "dur": 2,
            "entities": [
              "light.balcone_camera"
            ],
            "temp": [],
            "color": [
              "light.balcone_camera"
            ],
            "maxbri": 75,
            "context": {
              "id": "01H0G52X36W56ZJYH58B79QMYH",
              "parent_id": "01H0G52X354VRSFJHEF4AC4SWN",
              "user_id": null
            }
          }
        }
      ],
      "sequence/0/parallel/0/sequence/0": [
        {
          "path": "sequence/0/parallel/0/sequence/0",
          "timestamp": "2023-05-15T16:49:49.167186+00:00"
        }
      ],
      "sequence/0/parallel/0/sequence/0/if": [
        {
          "path": "sequence/0/parallel/0/sequence/0/if",
          "timestamp": "2023-05-15T16:49:49.167334+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "sequence/0/parallel/0/sequence/0/if/condition/0": [
        {
          "path": "sequence/0/parallel/0/sequence/0/if/condition/0",
          "timestamp": "2023-05-15T16:49:49.167383+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0",
          "timestamp": "2023-05-15T16:49:49.167828+00:00",
          "result": {
            "choice": "then"
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/if": [
        {
          "path": "sequence/0/parallel/1/sequence/0/if",
          "timestamp": "2023-05-15T16:49:49.167909+00:00",
          "result": {
            "result": true
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/if/condition/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/if/condition/0",
          "timestamp": "2023-05-15T16:49:49.167949+00:00",
          "result": {
            "result": true,
            "entities": []
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0",
          "timestamp": "2023-05-15T16:49:49.168684+00:00"
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0/repeat": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0/repeat",
          "timestamp": "2023-05-15T16:49:49.168888+00:00",
          "changed_variables": {
            "repeat": {
              "first": true,
              "index": 1
            }
          },
          "result": {
            "result": false
          }
        }
      ],
      "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0": [
        {
          "path": "sequence/0/parallel/1/sequence/0/then/0/repeat/while/0",
          "timestamp": "2023-05-15T16:49:49.168931+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ]
    },
    "config": {
      "alias": "Sunrise Simulator Manual",
      "sequence": [
        {
          "parallel": [
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ temp | count > 0 }}"
                }
              ],
              "then": [
                {
                  "repeat": {
                    "while": [
                      {
                        "condition": "template",
                        "value_template": "{{ repeat.index <= maxbri\n      and not ( repeat.index > 0 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', temp)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != temp | count ) }}"
                      }
                    ],
                    "sequence": [
                      {
                        "service": "light.turn_on",
                        "data": {
                          "brightness_pct": "{{ iif(repeat.index < 100, repeat.index, 100) }}\n",
                          "color_temp": "{{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) * 2.5 * (100/maxbri) ) ) | int, 185 ) }}\n",
                          "transition": "{{ ( ( dur * 60) / maxbri ) | int }}\n"
                        },
                        "target": {
                          "entity_id": "{{ temp }}\n"
                        }
                      },
                      {
                        "delay": {
                          "milliseconds": "{{ (  ( ( states('input_number.sunrise_sim_time') | int(10) * 60) / maxbri ) * 1000\n ) | int }}\n"
                        }
                      }
                    ]
                  }
                }
              ]
            },
            {
              "if": [
                {
                  "condition": "template",
                  "value_template": "{{ color | count > 0 }}"
                }
              ],
              "then": [
                {
                  "repeat": {
                    "while": [
                      {
                        "condition": "template",
                        "value_template": "{{ repeat.index <= maxbri\n      and not ( repeat.index > 0 \n\t\t\t\t\tand states.light\n\t\t\t\t\t\t\t| selectattr('entity_id', 'in', color)\n\t    \t\t\t\t\t| selectattr('state', 'eq', 'on')\n\t\t    \t\t\t\t| list | count != color | count ) }}"
                      }
                    ],
                    "sequence": [
                      {
                        "service": "light.turn_on",
                        "data": {
                          "rgb_color": "{% if repeat.index <= maxbri/6 %}\n  [255,0,0]\n{% elif repeat.index <= (maxbri/6) * 2  %}\n  [255,77,0]\n{% elif repeat.index <= (maxbri/6) * 3 %}\n  [255,103,0]\n{% elif repeat.index <= (maxbri/6) * 4 %}\n  [255,129,0]  \n{% else %}\n  [255,167,0]\n{% endif %}\n",
                          "brightness_pct": "{{ iif(repeat.index < 100, repeat.index, 100) }}\n",
                          "transition": "{{ (  ( dur * 60) / maxbri  ) | int }}\n"
                        },
                        "target": {
                          "entity_id": "{{ color }}\n"
                        }
                      },
                      {
                        "delay": {
                          "milliseconds": "{{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}\n"
                        }
                      }
                    ]
                  }
                }
              ]
            }
          ]
        }
      ],
      "variables": {
        "dur": "{{ duration | default('10', true) | int(10) }}\n",
        "entities": "{% set res = namespace(r=[]) %}  {% if target is defined %}    \n  {% set target = [target] if target is string else target %} \n  {% for t in target %}\n    {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}\n      {% set res.r = res.r + [t] %}\n    {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}\n      {% set res.r = res.r + states.group \n                              | selectattr('entity_id', 'eq', t) \n                              | map(attribute='attributes.entity_id')\n                              | list\n                              | default([''], true)\n                              | first\n                              | select('in', states.light \n                                              | rejectattr('state','in', ['unavailable', 'unknown', None]) \n                                              | map(attribute='entity_id') \n                                              | list) \n                              | list %}\n    {% else %}\n      {% set res.r = res.r + states.light \n                               | rejectattr('state','in', ['unavailable', 'unknown', None])\n                               | selectattr('entity_id', 'in', area_entities(t))\n                               | map(attribute='entity_id') \n                               | list %}\n    {% endif %}\n  {% endfor %}\n{% else %}\n  {% set res.r = states.light \n                   | rejectattr('state','in', ['unavailable', 'unknown', None])\n                   | map(attribute='entity_id') \n                   | list %}\n{% endif %} {{ res.r }}\n",
        "temp": "{% set res = namespace(r=[], l=[]) %}\n{% if entities | default([], true) | count > 0 %}\n  {% for sun in entities %}\n    {% if states(sun) not in ['unavailable','unknown']\n        and 'color_temp' in states[sun].attributes.supported_color_modes\n        and states[sun].attributes.supported_color_modes | count == 1 %}\n      {% set res.r = res.r + [sun] %}\n    {% endif %}\n  {% endfor %}\n{% endif %}\n{{ res.r }}\n",
        "color": "{% set res = namespace(r=[], l=[]) %}\n{% if entities | default([], true) | count > 0 %}\n  {% for sun in entities %}\n    {% if states(sun) not in ['unavailable','unknown']\n        and 'hs' in states[sun].attributes.supported_color_modes %}\n      {% set res.r = res.r + [sun] %}\n    {% endif %}\n  {% endfor %}\n{% endif %}\n{{ res.r }}\n",
        "maxbri": "{% set bri = brightness | default('100', true) |int(100) %}\n{{ bri if bri <= 100 else 100 }}\n"
      },
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01H0G52X36W56ZJYH58B79QMYH",
      "parent_id": "01H0G52X354VRSFJHEF4AC4SWN",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "when": 1684169389.16382,
      "state": "on",
      "entity_id": "script.simulatore_alba_con_supporto_spegnimento"
    },
    {
      "when": 1684169389.171553,
      "state": "off",
      "entity_id": "script.simulatore_alba_con_supporto_spegnimento",
      "context_state": "on",
      "context_entity_id": "script.simulatore_alba_con_supporto_spegnimento"
    }
  ]
}

I have edited the code, sorry for the frustration, please try the new one

I have tried the new script, and it works very well. Thank you for your help and willingness!

1 Like

I’m using gledopto bulbs, which have two color modes: xy and color_temp. This automation fails for this, since color temperature operation requires the mode color_temp to be the only one mode present for the light. I wanted to use the color temperature operation here, so I removed the line

and states[sun].attributes.supported_color_modes | count == 1 

in the if statement for the temp variable, then it worked without issues. I’m not sure if using the xy mode instead of hs translates easily, but for me this is satisfactory.

Final script:

alias: Sunrise Simulator Manual
sequence:
  - parallel:
      - if:
          - condition: template
            value_template: "{{ temp | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= maxbri
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', temp)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      != temp | count ) }}
              sequence:
                - service: light.turn_on
                  data:
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    color_temp: >
                      {{ iif(repeat.index < maxbri, ( 500 - ( (repeat.index-1) *
                      2.5 * (100/maxbri) ) ) | int, 185 ) }}
                    transition: |
                      {{ ( ( dur * 60) / maxbri ) | int }}
                  target:
                    entity_id: |
                      {{ temp }}
                - delay:
                    milliseconds: >
                      {{ (  ( ( states('input_number.sunrise_sim_time') |
                      int(10) * 60) / maxbri ) * 1000
                       ) | int }}
      - if:
          - condition: template
            value_template: "{{ color | count > 0 }}"
        then:
          - repeat:
              while:
                - condition: template
                  value_template: |
                    {{ repeat.index <= maxbri
                          and not ( repeat.index > 1 
                                             and states.light
                                                        | selectattr('entity_id', 'in', color)
                                                        | selectattr('state', 'eq', 'on') | list | count 
                                                      != temp | count ) }}
              sequence:
                - service: light.turn_on
                  data:
                    rgb_color: |
                      {% if repeat.index <= maxbri/6 %}
                        [255,0,0]
                      {% elif repeat.index <= (maxbri/6) * 2  %}
                        [255,77,0]
                      {% elif repeat.index <= (maxbri/6) * 3 %}
                        [255,103,0]
                      {% elif repeat.index <= (maxbri/6) * 4 %}
                        [255,129,0]  
                      {% else %}
                        [255,167,0]
                      {% endif %}
                    brightness_pct: |
                      {{ iif(repeat.index < 100, repeat.index, 100) }}
                    transition: |
                      {{ (  ( dur * 60) / maxbri  ) | int }}
                  target:
                    entity_id: |
                      {{ color }}
                - delay:
                    milliseconds: |
                      {{ (  ( ( dur * 60) / maxbri ) * 1000 ) | int }}
variables:
  dur: |
    {{ duration | default('10', true) | int(10) }}
  entities: |
    {% set res = namespace(r=[]) %}  {% if target is defined %}    
      {% set target = [target] if target is string else target %} 
      {% for t in target %}
        {% if t[0:6] == 'light.' and t in expand(states.light) | map(attribute='entity_id') | list %}
          {% set res.r = res.r + [t] %}
        {% elif t[0:6] == 'group.' and t in states.group | map(attribute='entity_id') | list %}
          {% set res.r = res.r + states.group 
                                  | selectattr('entity_id', 'eq', t) 
                                  | map(attribute='attributes.entity_id')
                                  | list
                                  | default([''], true)
                                  | first
                                  | select('in', states.light 
                                                  | rejectattr('state','in', ['unavailable', 'unknown', None]) 
                                                  | map(attribute='entity_id') 
                                                  | list) 
                                  | list %}
        {% else %}
          {% set res.r = res.r + states.light 
                                   | rejectattr('state','in', ['unavailable', 'unknown', None])
                                   | selectattr('entity_id', 'in', area_entities(t))
                                   | map(attribute='entity_id') 
                                   | list %}
        {% endif %}
      {% endfor %}
    {% else %}
      {% set res.r = states.light 
                       | rejectattr('state','in', ['unavailable', 'unknown', None])
                       | map(attribute='entity_id') 
                       | list %}
    {% endif %} {{ res.r }}
  temp: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'color_temp' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  color: |
    {% set res = namespace(r=[], l=[]) %}
    {% if entities | default([], true) | count > 0 %}
      {% for sun in entities %}
        {% if states(sun) not in ['unavailable','unknown']
            and 'hs' in states[sun].attributes.supported_color_modes %}
          {% set res.r = res.r + [sun] %}
        {% endif %}
      {% endfor %}
    {% endif %}
    {{ res.r }}
  maxbri: |
    {% set bri = brightness | default('100', true) |int(100) %}
    {{ bri if bri <= 100 else 100 }}
mode: single

Can you please post the supported_color_modes of your lamps?
Removing this line will possible have a strange impact for lamps with colour since they also have color temperature.

@ChreeceGR, somehow it does not work for me. Could you please help me?
I’ve created the script (copy & paste), created an automation, triggered the automation manually,
but the script does seem to stop hat “Execute in parallel”.
The name of the light is correct. Already tried rerun / restarting HA.

alias: Test
description: ""
trigger: []
condition: []
action:
  - service: script.sunrise_simulator_manual
    data:
      duration: 10
      target:
        - light.hue_surimu
      brightness: 100
mode: single
1 Like

Can you please post the attributes of the light?

I‘got this Info from Zigbee2MQTT, didn‘t know where to get the info from HA.

    "brightness": 127,
    "color": {
        "h": 0,
        "hue": 0,
        "s": 100,
        "saturation": 100,
        "x": 0.7,
        "y": 0.299
    },
    "color_mode": "xy",
    "color_temp": 160,

It‘s a Philips Hue light.

Hey @ChreeceGR

Are you still active?
I found your script, I’m looking for a while for something like this. Somehow it does not work for me :frowning:

Some info

description: ""
trigger:
  - platform: time
    at: "23:37:00"
condition: []
action:
  - service: script.1691702997070
    data:
      duration: 10
      target:
        - light.slaapkamer_ledstrip
      brightness: 100
mode: single

Light:
Slaapkamer - Ledstrip

supported_color_modes: rgbw
friendly_name: Slaapkamer - Ledstrip
supported_features: 32
color_mode: rgbw
brightness: 657
hs_color: 122.133, 88.235
rgb_color: 30, 255, 38
rgbw_color: 30, 255, 38, 0
xy_color: 0.178, 0.723