Regarding saving previous brightness state in node red, and also saving other variables

Hello everybody, I’m new and trying to learn HA and node red.
I’m having a lot of trouble understanding how to properly save and use variables, but after following a tutorial and doing some modification I created the following flow:

[{"id":"4b73c9b419648040","type":"tab","label":"Växtlampa nattetid brightness","disabled":false,"info":"","env":[]},{"id":"a27606769946ca0a","type":"server-state-changed","z":"4b73c9b419648040","name":"","server":"738c03d.3cc58fc","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"binary_sensor.rorelsesensor_ovan_hall_motion","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"on","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":false,"for":"","forType":"num","forUnits":"seconds","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":450,"y":280,"wires":[["24cbb73e5d47669a"],["e3127f9ee5d55aed"]]},{"id":"5a2e8de8e7218666","type":"api-call-service","z":"4b73c9b419648040","name":"Increase brightness","server":"738c03d.3cc58fc","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":["vaxtlampa_citrontrad"],"deviceId":[],"entityId":[],"data":"{\"brightness_pct\":\"100\",\"transition\":\"0\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1130,"y":220,"wires":[[]]},{"id":"e3127f9ee5d55aed","type":"trigger","z":"4b73c9b419648040","name":"","op1":"","op2":"off","op1type":"str","op2type":"str","duration":"15","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":850,"y":460,"wires":[["faf1cb82832941a4"]]},{"id":"97ba94990ec0f948","type":"api-call-service","z":"4b73c9b419648040","name":"return brightness to previous","server":"738c03d.3cc58fc","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":["vaxtlampa_citrontrad"],"deviceId":[],"entityId":[],"data":"{\"brightness\":data.attributes.brightness,\"transition\":1}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1420,"y":460,"wires":[[]]},{"id":"8308f36a6421c68e","type":"api-call-service","z":"4b73c9b419648040","name":"Increase brightness","server":"738c03d.3cc58fc","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":["vaxtlampa_citrontrad"],"deviceId":[],"entityId":[],"data":"{\"brightness_pct\":\"100\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1290,"y":360,"wires":[[]]},{"id":"da177a6cf90533c4","type":"debug","z":"4b73c9b419648040","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1400,"y":580,"wires":[]},{"id":"ad1129413c29b927","type":"inject","z":"4b73c9b419648040","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":600,"wires":[["faf1cb82832941a4"]]},{"id":"24cbb73e5d47669a","type":"api-current-state","z":"4b73c9b419648040","name":"Save previous brightness","server":"738c03d.3cc58fc","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.extended_color_light_1_2","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"},{"property":"lightBrightness","propertyType":"flow","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":870,"y":240,"wires":[["5a2e8de8e7218666"]]},{"id":"faf1cb82832941a4","type":"change","z":"4b73c9b419648040","name":"","rules":[{"t":"set","p":"data","pt":"msg","to":"lightBrightness","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":460,"wires":[["97ba94990ec0f948","da177a6cf90533c4"]]},{"id":"738c03d.3cc58fc","type":"server","name":"Home Assistant","addon":true}]

This flow sets an area of lights to 100% brightness when a motion sensor is activated, and then restore the previous brightness after a while.

It works, but I’m hoping I could ask some questions because even though i created it it was trial and error and I’m not 100% sure how it works:

  • The state change node (the trigger) has “current state equals previous state” unchecked because other ways of refreshing the timer failed. But why isn’t the “previous brightness overwritten” upon multiple activations? For example the first time it changes from 30% brightness to 100% brightness. If someone moves before the 15 sec timer it seems it should trigger a new message that now overwrites the previous brightness with 100% brightness. But this doesn’t happen, the flow actually remembers that is originally was 30% brightness. Why? How?

  • Right now the brightness is remembered by creating the variable flow.lightBrightness which saves the entity (data?) and then overwriting the message data with this flow variable. Is it possible to only save the brightness value in the flow variable? How would that look?

Thank you in advance if anyone takes the time to answer my questions

In the output of the event state set payload to use

$entities('light.dj').attributes.brightness

change the light entity to yours.

You can use a change node instead of the current state and use msg.payload
image

In the action/call service node you can use

{"brightness": $flowContext('bri_test')}

to recall the value.

1 Like

Thanks a lot for the reply!

May I ask: Your way is the answer to my first question right, how to specifically save the brightness. That’s very helpful since it allows me to be more flexible in future projects. Is there a difference using a change node instead of current state? Also, couldn’t you define the flow variable directly at the event state trigger?

I still don’t understand why the brightness variable isn’t overridden each time the sensor is triggered, I mean its great that it works but i seems like it shouldn’t. Once during the last two days the lights got stuck att 100% brightness and I suspect this has something to do with it

The current state has additional functionality and the default settings for the output is on the verbose side. I like to keep messages as simple as possible. I also think it’s a little easier when you come back later and need to understand what you did.

Also, couldn’t you define the flow variable directly at the event state trigger?

Yes, in the output of the event state where you set msg.payload, msg can be changed to flow.

image

I used to do this keep everything compact aka as much as I can program in a single node. When I needed to come back to a flow I found it time consuming to try and decipher what I was actually thinking at the time.

It should be how often does this happen. In the lights log is there any unavailable or unknown state for that light?

Hey, you’re right. The lights “got stuck” att 100% again and when I checked the logs all the lights in the fixture became unavailable around that time.
At the same time two other lights in other parts of the house became unavailable as well.

So all in all it seems the erratic behaviour is not due to a bug in the node red flow, but rather a network/connection issue?

I guess the next step would be to troubleshoot the Rpi and the zigbee network, consider switching zigbee channels etc? The Zigbee dongle and the router are very close to each other for starters, maybe they are interfering.

Also, I think I figured out what I misunderstood about the NR flow - I thought that every time the motion sensor detected new motion it would save the current brightness to the flow variable, which would override the variable with 100% brightness since the flow sets the brightness to maximum.
But rather it must be that the motion sensor device turns on, sets the flow variable and then simply keeps being on while new motion is detected. Thus not actually proceeding with rewriting the flow variable, instead keeping its original value (whatever the brightness was before motion was detected).

I really appreciate the help, thanks.

In the event state node, check all the boxes towards the bottom. So it does not react to unavailable or unknown states.