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?