Running all steps of a script even if one fails

Hi folks, I have the following script (actual script contains a number more lights). It is triggered by a time automation, designed to check if any lights are on, and if they are, brighten them and up the kelvin.

sequence:
  - data_template:
      entity_id: light.bor1
      brightness: "{% if states.light.bor1.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: "{% if states.light.bor1.state == 'on' %} 6500 {% else %} 0 {% endif %}"
    action: light.turn_on
  - data_template:
      entity_id: light.hallway
      brightness: "{% if states.light.hallway.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: >-
        {% if states.light.hallway.state == 'on' %} 6500 {% else %} 0 {% endif
        %}
    action: light.turn_on
  - data_template:
      entity_id: light.cr1
      brightness: "{% if states.light.cr1.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: "{% if states.light.cr1.state == 'on' %} 6500 {% else %} 0 {% endif %}"
    action: light.turn_on

I realise probably not the cleanest way to do this (i.e. could use a loop of sorts, so open to suggestions), but my main issue is, let’s say light.bor1 is on, then it performs correctly and brightens. If light.hallways is actually off though, the script just stops, rather than moving on to the next light to check whether that’s on.

If one step fails because the light is off, how do I make sure the script continues to asess the rest of the lights?

continue_on_error: true

Why would a light.turn_on action fail because a light is off?

You can add a continue_on_error: true parameter to an action, but that may or may not do anything depending on how the integration in question handles errors and why the error occurred. If you try to execute a non-existant action or supply an action with faulty parameters then the parser will fail rather than the action and thus won’t continue anyway.

continue_on_error: true did not work unfortunately.

In traces, this is the error provided for the script when I leave the first light off in the sequence:

Executed: February 3, 2025 at 12:59:22 PM
Error: division by zero
Result:
params:
  domain: light
  service: turn_on
  service_data:
    entity_id: light.bor1
    brightness: 0
    color_temp_kelvin: 0
  target: {}
running_script: false

I have never seen a bulb that can do 0 kelvin

The else probably isn’t necessary. And you’re right, the bulbs don’t actually go to 0 Kelvin. The min on these is 2202. That said, that isn’t cause an error. If I ensure all bulbs are on, the automation runs just fine. So just trying to establish how to make it continue if a bulb in the middle of the sequence happens to not be on already.

Can you show us the code you used with continue on error?
I’m quite sure it’s either the brightness 0 or the kelvin 0 that is the cause, since those are 0 meaning there is a division by zero

Sure,including the first three here, all others follow the same pattern.

sequence:
  - data_template:
      entity_id: light.bor1
      brightness: "{% if states.light.bor1.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: "{% if states.light.bor1.state == 'on' %} 6500 {% else %} 0 {% endif %}"
    action: light.turn_on
    continue_on_error: true
  - data_template:
      entity_id: light.hallway
      brightness: "{% if states.light.hallway.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: >-
        {% if states.light.hallway.state == 'on' %} 6500 {% else %} 0 {% endif
        %}
    action: light.turn_on
    continue_on_error: true
  - data_template:
      entity_id: light.cr1
      brightness: "{% if states.light.cr1.state == 'on' %} 255 {% else %} 0 {% endif %}"
      color_temp_kelvin: "{% if states.light.cr1.state == 'on' %} 6500 {% else %} 0 {% endif %}"
    action: light.turn_on
    continue_on_error: true

So if the 0 IS the cause, then maybe something like the following could be a solve?

- data_template:
      entity_id: light.bor1
      brightness: "{% if states.light.bor1.state == 'on' %} 255 {% endif %}"
      color_temp_kelvin: "{% if states.light.bor1.state == 'on' %} 6500 {% endif %}"
    action: light.turn_on
    continue_on_error: true

Is it possible to do that? Just not have an else or should I use something else to do an else do nothing with this bulb?

I tried removing the else and I tried using null in place of 0, but that just gave the error: Error: expected int for dictionary value @ data['brightness']

give this a try instead in a new script:

sequence:
  - repeat:
      for_each:
        - light.bor1
        - light.hallway
        - light.cr1
      sequence:
        - if:
            - condition: state
              entity_id: "{{ repeat.item }}"
              state: "on"
          then:
            - action: light.turn_on
              metadata: {}
              data:
                brightness: 255
                color_temp_kelvin: 6500
              target:
                entity_id: "{{ repeat.item }}"

When trying to save the script:

Message malformed: Entity {{ repeat.item }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['sequence'][0]['repeat']['sequence'][0]['if'][0]['entity_id']

Is the entity names correct?

Yeah, they are :confused:

I just tried it myself. It seems if/then and conditions can’t be templated.
Without the if/then it works fine for me.

EDIT; just tried the loop with only turn on and brightness and it works even if one of the lights are off.

1 Like

Replace the State Condition with a Template Condition.

A State Condition doesn’t support templates.

More than likely the issue is the zero Kelvin color temperature.

Some bulbs and/or integrations only accept a color temperature in mireds. The formula for converting Kelvin to mireds is 1000000 / color_temp_kelvin which would lead to the division by zero error.

Use a lowest color temperature of 1 instead and it’ll probably work.

1 Like

How would that look syntactically?

      sequence:
        - if:
            - condition: template 
              value_template: "{{ is_state(repeat.item, 'on') }}"
          then:

EDIT

Correction. Added missing closing parenthesis.

Provides the following error now: Message malformed: extra keys not allowed @ data['sequence'][0]['repeat']['sequence'][0]['if'][0]['value_template']

Current code:

sequence:
  - repeat:
      for_each:
        - light.bor1
        - light.hallway
        - light.cr1
      sequence:
        - if:
            - condition: state
              value_template: "{{ is_state(repeat.item, 'on' }}"
          then:
            - action: light.turn_on
              metadata: {}
              data:
                brightness: 255
                color_temp_kelvin: 6500
              target:
                entity_id: "{{ repeat.item }}"

You didn’t copy my example correctly.

It’s
condition: template
not
condition: state