Switch based on calculated threshold

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.

  1. When triggered, check if the light is on and, if it is, the current brightness.
  2. Compare the current brightness to the average of the dim and bright settings.
  3. 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?

Make sure the data field type is set to J: Expression and use

(
    $dim := $number($entities('input_number.br3_lamp_dim').state);
    $bright := $number($entities('input_number.br3_lamp_bright').state);
    $mid := ($dim + $bright) / 2;
    $current := $entities('light.br3_short_lamp').attributes.brightness;
    $brightness := $current > $mid ? $dim : $bright;
    
    {
        "brightness_pct": $brightness
    }
)
1 Like

Thanks @Kermit

I have your solution working.

So that I can learn how to use J: Expressions in other contexts, is there also a way to put the test into a Switch node, using the flow structure that I’ve got above?
What I think that I want to learn how to do is replace the number 128 in the Node below with a J Expression involving
$number($entities('input_number.test_bright').state)
and
$number($entities('input_number.test_dim').state)

[{"id":"3aaa56b19bf306f5","type":"inject","z":"ffbd7f06.4a014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":268,"y":2736,"wires":[["99782632a89df3da"]]},{"id":"99782632a89df3da","type":"api-current-state","z":"ffbd7f06.4a014","name":"Dim","server":"","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.br3_lamp_dim","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"dim","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":404,"y":2736,"wires":[["9c675a370622fdba"]]},{"id":"9c675a370622fdba","type":"api-current-state","z":"ffbd7f06.4a014","name":"Bright","server":"","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"input_number.br3_lamp_bright","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"bright","propertyType":"msg","value":"","valueType":"entityState"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":532,"y":2736,"wires":[["5c3ca55f37bbdb6f"]]},{"id":"5c3ca55f37bbdb6f","type":"api-current-state","z":"ffbd7f06.4a014","name":"Current","server":"","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.br3_short_lamp","state_type":"num","blockInputOverrides":false,"outputProperties":[{"property":"current","propertyType":"msg","value":"$entity().attributes.brightness","valueType":"jsonata"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":686,"y":2736,"wires":[["37b0cc91602b8f2e"]]},{"id":"9ce5e4f3622d1810","type":"switch","z":"ffbd7f06.4a014","name":"","property":"current","propertyType":"msg","rules":[{"t":"gt","v":"mid","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":964,"y":2736,"wires":[["267d2ef0ec239caf"],["c799f4502cbf89b2"]]},{"id":"37b0cc91602b8f2e","type":"change","z":"ffbd7f06.4a014","name":"mid","rules":[{"t":"set","p":"mid","pt":"msg","to":"(dim + bright) / 2","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":836,"y":2736,"wires":[["9ce5e4f3622d1810"]]},{"id":"267d2ef0ec239caf","type":"api-call-service","z":"ffbd7f06.4a014","name":"Dim","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.br3_short_lamp","data":"{\"brightness_pct\": dim}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1108,"y":2736,"wires":[[]]},{"id":"c799f4502cbf89b2","type":"api-call-service","z":"ffbd7f06.4a014","name":"Bright","server":"","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.br3_short_lamp","data":"{\"brightness_pct\": bright}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1108,"y":2784,"wires":[[]]}]
1 Like