I am trying to create a set of scripts for my Ikea on/off switches. These switches are connected via Zigbee2MQTT and have a brightness_up/down topic when holding the switch and a brightness_stop when releasing.
The scripts work have fields for light (entity_id), direction (up or down) and, to make them super flexible, I’d like to pass the delay in tenths of seconds and the step in brightness to use, since I’d like to adjust some lights in smaller steps than others. The brightness_stop will stop both scripts from activating each other.
As long as I only use the light and direction fields and set the delay and step statically, the scripts work fine. They repeat until I release the button. When I pass the integer values from my test script to the delay script and keep static values in the delay script, it still works fine, but when I try to pass the values from the delay script back to the test script, it stops functioning.
# test.yaml
alias: Testscript
fields:
direction:
description: 'brightness up or down'
example: up|down
light:
description: 'the light entity to change'
example: 'light.bedroom'
step:
description: 'brightness step'
example: 16
sequence:
- service: light.turn_on
data_template:
entity_id: '{{ light }}'
brightness: >
{% if direction == 'up' %}
{{ (state_attr(light,'brightness')|default(0)) + (step|default(16)) }}
{% else %}
{{ (state_attr(light,'brightness')|default(255)) - (step|default(16)) }}
{% endif %}
- service: input_number.set_value
data_template:
entity_id: input_number.test
value: "{{ step|default(13) }}"
- service: script.turn_on
data_template:
entity_id: script.delay
variables:
light2: '{{ light }}'
direction2: '{{ direction }}'
step2: '{{ step|default(8) }}'
# delay.yaml
alias: Delay
fields:
direction2:
description: 'direction for brightness'
example: 'up|down'
light2:
description: 'the light entity to change'
example: 'light.bedroom'
step2:
description: 'brightness step'
example: 16
sequence:
- service: script.turn_off
entity_id: 'script.test'
- delay:
milliseconds: 200
- service: input_number.set_value
data_template:
entity_id: input_number.test2
value: "{{ step2|default(13) }}"
- service: script.turn_on
data_template:
entity_id: 'script.test'
variables:
direction: '{{ direction2 }}'
light: '{{ light2 }}'
step: '{{ step2|default(8) }}'
As soon as I change the last line to step: 16
, all starts working. I’ve tried with or without default, piping it through |int but nothing seems to work.