Why is my script not working correctly?

Hello,

I have a script that, if it would work correctly, should take in a light entity (with entity_id), a minimum brightness value (in %) and a number of steps (variable name steps).
It should, when called, increase the brightness of the light to the next of steps equally-spaced steps while starting at the minium brightness.
If the light is brighter than the last step, it should set it to the lowest step.

I currently have this script:

alias: Lichtstufe weiterschalten
mode: single
fields:
  light_entity:
    selector:
      target:
        entity:
          domain: light
    name: Licht
    required: true
  min_brightness:
    selector:
      number:
        min: 0
        max: 100
        step: 1
        unit_of_measurement: "%"
    name: Mindesthelligkeit
    default: 65
  steps:
    selector:
      number:
        min: 1
        max: 10
    name: Stufen
    default: 4
    required: false
sequence:
  - variables:
      entity_id: |-
        {% if light_entity.entity_id is string %}
          {{ light_entity.entity_id }}
        {% else %}
          {{ light_entity.entity_id[0] }}
        {% endif %}
      min_b: "{{ (min_brightness | int) | round(0) | int }}"
      max_b: 100
      current_brightness: "{{ state_attr(entity_id, 'brightness') | int(0) }}"
      step_count: "{{ steps | int }}"
      step_size: |-
        {% if step_count > 1 %}
          {{ ((max_b - min_b) / (step_count - 1)) | float }}
        {% else %}
          0
        {% endif %}
      current_step: |-
        {% if current_brightness < min_b %}
          0
        {% else %}
          {{ ((current_brightness - min_b) / step_size) | round(0) | int }}
        {% endif %}
      next_step: |-
        {% set ns = current_step + 1 %} {% if ns >= step_count %}
          0
        {% else %}
          {{ ns }}
        {% endif %}
      target_brightness: "{{ (min_b + (next_step * step_size)) | round(0) | int }}"
  - target:
      entity_id: "{{ entity_id }}"
    data:
      brightness_pct: "{{ target_brightness }}"
    action: light.turn_on
description: Schaltet die Helligkeit eines Lichts zyklisch durch vordefinierte Stufen.
icon: mdi:lightbulb

How could this be fixed?

It would be helpful if you download and share the debug trace from a run of the script.

Two issues I see off the top…

  1. Your step_size defaults to 0, then in current_step that can lead to a division by 0 error.
  2. The variable current_brightness is based on a 254 point scale, but everything else seems to be based on a percentage/100 pt scale.
  3. While not technically wrong, I would use an Entity Selector instead of a Target Selector.
alias: Lichtstufe weiterschalten
mode: single
fields:
  light_entity:
    selector:
      entity:
        filter:
          - domain: light
    name: Licht
    required: true
  min_brightness:
    selector:
      number:
        min: 0
        max: 100
        step: 1
        unit_of_measurement: "%"
    name: Mindesthelligkeit
    default: 65
  steps:
    selector:
      number:
        min: 1
        max: 10
    name: Stufen
    default: 4
    required: false
sequence:
  - variables:
      entity_id: |-
        {% if light_entity is string %}
          {{ light_entity }}
        {% else %}
          {{ light_entity[0] }}
        {% endif %}
      min_b: "{{ min_brightness | int(0)}}"
      max_b: 100
      current_brightness: |
        {% set b = state_attr(entity_id, 'brightness')|default(0,1) %}
        {{ ((b/ 254) * 100) | int(0) }}
      step_count: "{{ steps | int }}"
      step_size: |-
        {% if step_count > 1 %}
          {{ ((max_b - min_b) / (step_count - 1)) | float }}
        {% else %}
          1
        {% endif %}
      current_step: |-
        {% if current_brightness < min_b %}
          0
        {% else %}
          {{ ((current_brightness - min_b) / step_size) | round(0) | int }}
        {% endif %}
      next_step: |-
        {% set ns = current_step + 1 %} {% if ns >= step_count %}
          0
        {% else %}
          {{ ns }}
        {% endif %}
      target_brightness: "{{ (min_b + (next_step * step_size)) | round(0) | int }}"
  - target:
      entity_id: "{{ entity_id }}"
    data:
      brightness_pct: "{{ target_brightness }}"
    action: light.turn_on
description: Schaltet die Helligkeit eines Lichts zyklisch durch vordefinierte Stufen.
icon: mdi:lightbulb

I pasted the script from your post and here is the trace:

{
  "trace": {
    "last_step": "sequence/0",
    "run_id": "319a031dfef6e5d1b8fa98b09f63a0bc",
    "state": "stopped",
    "script_execution": "error",
    "timestamp": {
      "start": "2025-10-20T03:32:21.814707+00:00",
      "finish": "2025-10-20T03:32:21.826019+00:00"
    },
    "domain": "script",
    "item_id": "licht_stufenschaltung",
    "error": "TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2025-10-20T03:32:21.818147+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.licht_stufenschaltung",
              "state": "off",
              "attributes": {
                "last_triggered": "2025-10-19T16:47:23.503961+00:00",
                "mode": "single",
                "current": 0,
                "icon": "mdi:lightbulb",
                "friendly_name": "Lichtstufe weiterschalten"
              },
              "last_changed": "2025-10-20T03:32:05.875524+00:00",
              "last_reported": "2025-10-20T03:32:05.875524+00:00",
              "last_updated": "2025-10-20T03:32:05.875524+00:00",
              "context": {
                "id": "01K7ZTS6KKDWR23J6BZNANBFJK",
                "parent_id": null,
                "user_id": null
              }
            },
            "min_brightness": 65,
            "steps": 4,
            "light_entity": "light.lichter_wohnzimmer_licht_1_wohnzimmer",
            "context": {
              "id": "01K7ZTSP5NSE27Z44AJ89M555X",
              "parent_id": null,
              "user_id": "d927859175f244159ecefee34677158b"
            }
          },
          "error": "TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'"
        }
      ]
    },
    "config": {
      "alias": "Lichtstufe weiterschalten",
      "mode": "single",
      "fields": {
        "light_entity": {
          "selector": {
            "entity": {
              "filter": [
                {
                  "domain": "light"
                }
              ]
            }
          },
          "name": "Licht",
          "required": true
        },
        "min_brightness": {
          "selector": {
            "number": {
              "min": 0,
              "max": 100,
              "step": 1,
              "unit_of_measurement": "%"
            }
          },
          "name": "Mindesthelligkeit",
          "default": 65
        },
        "steps": {
          "selector": {
            "number": {
              "min": 1,
              "max": 10
            }
          },
          "name": "Stufen",
          "default": 4,
          "required": false
        }
      },
      "sequence": [
        {
          "variables": {
            "entity_id": "{% if light_entity.entity_id is string %}\n  {{ light_entity }}\n{% else %}\n  {{ light_entity[0] }}\n{% endif %}",
            "min_b": "{{ min_brightness | int(0)}}",
            "max_b": 100,
            "current_brightness": "{{ ((state_attr(entity_id, 'brightness')/ 254) * 100) | int(0) }}",
            "step_count": "{{ steps | int }}",
            "step_size": "{% if step_count > 1 %}\n  {{ ((max_b - min_b) / (step_count - 1)) | float }}\n{% else %}\n  1\n{% endif %}",
            "current_step": "{% if current_brightness < min_b %}\n  0\n{% else %}\n  {{ ((current_brightness - min_b) / step_size) | round(0) | int }}\n{% endif %}",
            "next_step": "{% set ns = current_step + 1 %} {% if ns >= step_count %}\n  0\n{% else %}\n  {{ ns }}\n{% endif %}",
            "target_brightness": "{{ (min_b + (next_step * step_size)) | round(0) | int }}"
          }
        },
        {
          "target": {
            "entity_id": "{{ entity_id }}"
          },
          "data": {
            "brightness_pct": "{{ target_brightness }}"
          },
          "action": "light.turn_on"
        }
      ],
      "description": "Schaltet die Helligkeit eines Lichts zyklisch durch vordefinierte Stufen.",
      "icon": "mdi:lightbulb"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01K7ZTSP5NSE27Z44AJ89M555X",
      "parent_id": null,
      "user_id": "d927859175f244159ecefee34677158b"
    }
  },
  "logbookEntries": [
    {
      "state": "on",
      "entity_id": "script.licht_stufenschaltung",
      "icon": "mdi:lightbulb",
      "when": 1760931141.8168063,
      "context_user_id": "d927859175f244159ecefee34677158b"
    },
    {
      "state": "off",
      "entity_id": "script.licht_stufenschaltung",
      "icon": "mdi:lightbulb",
      "when": 1760931141.8250544,
      "context_user_id": "d927859175f244159ecefee34677158b",
      "context_state": "on",
      "context_entity_id": "script.licht_stufenschaltung"
    }
  ]
}

Looks like I missed something in the definition of entity_id. I’ve corrected it above.

1 Like

The error message is still the same, here is the trace:

{
  "trace": {
    "last_step": "sequence/0",
    "run_id": "6f630476a63a9f682460869d331f0ddc",
    "state": "stopped",
    "script_execution": "error",
    "timestamp": {
      "start": "2025-10-20T04:16:33.432930+00:00",
      "finish": "2025-10-20T04:16:33.452759+00:00"
    },
    "domain": "script",
    "item_id": "licht_stufenschaltung",
    "error": "TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'",
    "trace": {
      "sequence/0": [
        {
          "path": "sequence/0",
          "timestamp": "2025-10-20T04:16:33.440090+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "script.licht_stufenschaltung",
              "state": "off",
              "attributes": {
                "last_triggered": "2025-10-20T03:40:24.337850+00:00",
                "mode": "single",
                "current": 0,
                "icon": "mdi:lightbulb",
                "friendly_name": "Lichtstufe weiterschalten"
              },
              "last_changed": "2025-10-20T04:16:17.177686+00:00",
              "last_reported": "2025-10-20T04:16:17.177686+00:00",
              "last_updated": "2025-10-20T04:16:17.177686+00:00",
              "context": {
                "id": "01K7ZXA3RSVH5Z9G02XSRDFY7A",
                "parent_id": null,
                "user_id": null
              }
            },
            "min_brightness": 65,
            "steps": 4,
            "light_entity": "light.lichter_wohnzimmer_licht_1_wohnzimmer",
            "context": {
              "id": "01K7ZXAKMQS2DG0AGF35SC5C1D",
              "parent_id": null,
              "user_id": "d927859175f244159ecefee34677158b"
            }
          },
          "error": "TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'"
        }
      ]
    },
    "config": {
      "alias": "Lichtstufe weiterschalten",
      "mode": "single",
      "fields": {
        "light_entity": {
          "selector": {
            "entity": {
              "filter": [
                {
                  "domain": "light"
                }
              ]
            }
          },
          "name": "Licht",
          "required": true
        },
        "min_brightness": {
          "selector": {
            "number": {
              "min": 0,
              "max": 100,
              "step": 1,
              "unit_of_measurement": "%"
            }
          },
          "name": "Mindesthelligkeit",
          "default": 65
        },
        "steps": {
          "selector": {
            "number": {
              "min": 1,
              "max": 10
            }
          },
          "name": "Stufen",
          "default": 4,
          "required": false
        }
      },
      "sequence": [
        {
          "variables": {
            "entity_id": "{% if light_entity is string %}\n  {{ light_entity }}\n{% else %}\n  {{ light_entity[0] }}\n{% endif %}",
            "min_b": "{{ min_brightness | int(0)}}",
            "max_b": 100,
            "current_brightness": "{{ ((state_attr(entity_id, 'brightness')/ 254) * 100) | int(0) }}",
            "step_count": "{{ steps | int }}",
            "step_size": "{% if step_count > 1 %}\n  {{ ((max_b - min_b) / (step_count - 1)) | float }}\n{% else %}\n  1\n{% endif %}",
            "current_step": "{% if current_brightness < min_b %}\n  0\n{% else %}\n  {{ ((current_brightness - min_b) / step_size) | round(0) | int }}\n{% endif %}",
            "next_step": "{% set ns = current_step + 1 %} {% if ns >= step_count %}\n  0\n{% else %}\n  {{ ns }}\n{% endif %}",
            "target_brightness": "{{ (min_b + (next_step * step_size)) | round(0) | int }}"
          }
        },
        {
          "target": {
            "entity_id": "{{ entity_id }}"
          },
          "data": {
            "brightness_pct": "{{ target_brightness }}"
          },
          "action": "light.turn_on"
        }
      ],
      "description": "Schaltet die Helligkeit eines Lichts zyklisch durch vordefinierte Stufen.",
      "icon": "mdi:lightbulb"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01K7ZXAKMQS2DG0AGF35SC5C1D",
      "parent_id": null,
      "user_id": "d927859175f244159ecefee34677158b"
    }
  },
  "logbookEntries": [
    {
      "state": "on",
      "entity_id": "script.licht_stufenschaltung",
      "icon": "mdi:lightbulb",
      "when": 1760933793.4363258,
      "context_user_id": "d927859175f244159ecefee34677158b"
    },
    {
      "state": "off",
      "entity_id": "script.licht_stufenschaltung",
      "icon": "mdi:lightbulb",
      "when": 1760933793.451577,
      "context_user_id": "d927859175f244159ecefee34677158b",
      "context_state": "on",
      "context_entity_id": "script.licht_stufenschaltung"
    }
  ]
}

Well it worked for me before I posted, then I tested on a different light and it didn’t… I’ve added a default for the current_brightness variable, and now it’s working with both lights.

Third time’s the charm…?

1 Like

Thank you, now it works…