I have a button that when pressed toggles a light on/off on a single press and between bright and dim one a single press if it is already on. I currently have the automation working in yaml, but now want to transition the automation across to Node Red. The on/off part is working fine. The bright/dim isn’t.
I use input_number sliders with ranges of 1 to 255 on an admin lovelace dashboard that I use to control how dim or bright the dim and bright settings should be. The logic of the flow is simple.
- When triggered, check if the light is on and, if it is, the current brightness.
- Compare the current brightness to the average of the dim and bright settings.
- If the current is currently brighter than the average of the settings, switch to Dim and vice versa.
The working yaml code is:
- data_template:
brightness_pct: "{% set mid = ((states('input_number.br3_lamp_dim') | int +\
\ states('input_number.br3_lamp_bright') | int)/2) | int %} {% set current\
\ = state_attr('light.br3_short_lamp', 'brightness') | int %} {% if current\
\ > mid %}\n {{ states('input_number.br3_lamp_dim') | int }}\n{% else %}\n\
\ {{ states('input_number.br3_lamp_bright') | int }}\n{% endif %} \
\ \n"
entity_id: light.br3_short_lamp
service: light.turn_on
So far, I’ve created this in Node Red.
Everything is working if I use a hard coded threshold (eg 128) for the automation to choose between Dim add Bright. However, I want to use the average of the two sliders as the threshold and can’t get it working. Do I need extra Current State nodes and if so, what is the best way to combine values?