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