Create and store brightness from current_state node

I am trying to save the brightness data from the Current_state node and use it in an automation where you increase and decrease the brightness by a percentage.

Where i am stuck is saving the current brightness and state of device.

You can also find my flow below:
[{“id”:“ba081bb8.8696c8”,“type”:“tab”,“label”:“IKEA remote v2”,“disabled”:false,“info”:""},{“id”:“7029c8e8.0d1b28”,“type”:“switch”,“z”:“ba081bb8.8696c8”,“name”:“Action”,“property”:“payload”,“propertyType”:“msg”,“rules”:[{“t”:“eq”,“v”:“toggle”,“vt”:“str”},{“t”:“eq”,“v”:“arrow_right_click”,“vt”:“str”},{“t”:“eq”,“v”:“arrow_left_click”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_up_click”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_down_click”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_up_hold”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_up_release”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_down_hold”,“vt”:“str”},{“t”:“eq”,“v”:“brightness_down_release”,“vt”:“str”},{“t”:“eq”,“v”:“arrow_right_release”,“vt”:“str”},{“t”:“eq”,“v”:“arrow_left_release”,“vt”:“str”}],“checkall”:“true”,“repair”:false,“outputs”:11,“x”:350,“y”:390,“wires”:[[“1ca5f47f.03ee5c”],[“8ecffc87.3259a”],[“55f55d58.d6bb54”],[“985a53ca.f50c2”],[“61b92a1b.088854”],[“41d080e7.13669”,“ae8ff38a.544aa”],[“91759de8.b98cf”],[“41d080e7.13669”,“e22c7081.0a47c”],[“91759de8.b98cf”],[“12db6c47.dd0874”],[“12db6c47.dd0874”]]},{“id”:“1ca5f47f.03ee5c”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Toggle1”,“func”:“var device = flow.get(“activeDevice”);\nmsg.payload={“service”:“toggle”, “entity_id”:device};\nreturn msg;\n\n\n\n”,“outputs”:1,“noerr”:0,“x”:870,“y”:270,“wires”:[[“a570b72c.a34f58”,“2a9106bb.3e2afa”]]},{“id”:“61b92a1b.088854”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Brightness DOWN 50”,“func”:“var device = flow.get(“activeDevice”);\nvar currentState = device + “_brightness”;\nvar current = flow.get(currentState);\n\n\nvar brightness = current - 50;\nbrightness = 256\n\nif(brightness > 255){\n brightness = 100;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\n\n\nif(brightness < 0){\n brightness = 0;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\nelse{\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\n”,“outputs”:1,“noerr”:0,“x”:910,“y”:345,“wires”:[[“2a9106bb.3e2afa”,“c799c691.721ab8”]]},{“id”:“985a53ca.f50c2”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Brightness UP 50”,“func”:“var device = flow.get(“activeDevice”);\nvar currentState = device + “_brightness”;\nvar current = flow.get(currentState);\n\nvar brightness = current + 50;\nbrightness = 256\n\nif(brightness > 255){\n brightness = 255;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\n\nif(brightness < 0){\n brightness = 0;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\nelse{\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\n\n”,“outputs”:1,“noerr”:0,“x”:900,“y”:315,“wires”:[[“2a9106bb.3e2afa”,“c799c691.721ab8”]]},{“id”:“55f55d58.d6bb54”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“toggle devices --”,“func”:“var devices = global.get(“ZigbeeLights”);\n\nvar x = devices.length - 1;\nvar count = flow.get(“count”);\nvar y = isNaN(count);\n\nif(y === true ){\n count = 0;\n flow.set(“count”, count);\n}\n\nvar currentDevice = devices[count];\ncount–;\n\nif(count < 0){\n count = x;\n}\n\nflow.set(“count”, count);\nflow.set(“activeDevice”, currentDevice);\nreturn msg;”,“outputs”:1,“noerr”:0,“x”:545,“y”:270,“wires”:[[]]},{“id”:“8ecffc87.3259a”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“toggle devices ++”,“func”:“var devices = global.get(“ZigbeeLights”);\n\nvar x = devices.length - 1;\nvar count = flow.get(“count”);\nvar y = isNaN(count);\n\nif(y === true ){\n count = 0;\n flow.set(“count”, count);\n}\n\nvar currentDevice = devices[count];\ncount++;\n\nif(count > x){\n count = 0;\n}\n\nflow.set(“count”, count);\nflow.set(“activeDevice”, currentDevice);\n\nreturn msg;”,“outputs”:1,“noerr”:0,“x”:555,“y”:225,“wires”:[[]]},{“id”:“675b4312.f8843c”,“type”:“delay”,“z”:“ba081bb8.8696c8”,“name”:"",“pauseType”:“delay”,“timeout”:“500”,“timeoutUnits”:“milliseconds”,“rate”:“1”,“nbRateUnits”:“1”,“rateUnits”:“second”,“randomFirst”:“1”,“randomLast”:“5”,“randomUnits”:“seconds”,“drop”:false,“x”:1060,“y”:510,“wires”:[[“4c460d0c.6a34f4”]]},{“id”:“cd5e817.5e4948”,“type”:“delay”,“z”:“ba081bb8.8696c8”,“name”:"",“pauseType”:“delay”,“timeout”:“500”,“timeoutUnits”:“milliseconds”,“rate”:“1”,“nbRateUnits”:“1”,“rateUnits”:“second”,“randomFirst”:“1”,“randomLast”:“5”,“randomUnits”:“seconds”,“drop”:false,“x”:1060,“y”:540,“wires”:[[“2bb124ce.f9522c”]]},{“id”:“ae8ff38a.544aa”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Brightness up”,“func”:“var device = flow.get(“activeDevice”);\nvar currentState = device + “_brightness”;\nvar current = flow.get(currentState);\n\nvar brightness = current + 30;\n\n\nif(brightness > 255 || msg.feeedback == 255){\n brightness = 255;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n msg.feedback = {\n “brightness”: brightness\n };\n return msg;\n}\n\nif(brightness < 0 || msg.feeedback === 0){\n brightness = 0;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n msg.feedback = {\n “brightness”: brightness\n };\n return msg;\n}\nelse{\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n msg.feedback = {\n “brightness”: brightness\n };\n return msg;\n}\n”,“outputs”:1,“noerr”:0,“x”:890,“y”:390,“wires”:[[“2480aefe.53c362”,“2a9106bb.3e2afa”]]},{“id”:“e22c7081.0a47c”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Brightness down”,“func”:“var device = flow.get(“activeDevice”);\nvar currentState = device + “_brightness”;\nvar current = flow.get(currentState);\n\n\nvar brightness = current - 30;\n\n\nif(brightness > 255 || msg.feeedback == 255){\n brightness = 255;\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\n\nif(brightness < 0|| msg.feeedback === 0){\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n return msg;\n}\nelse{\n msg.payload = {“service”:“turn_on”, “entity_id”:device, “brightness”: brightness};\n msg.feedback = {\n “brightness”: brightness\n };\n return msg;\n}”,“outputs”:1,“noerr”:0,“x”:900,“y”:420,“wires”:[[“167aace4.04f2e3”,“2a9106bb.3e2afa”]]},{“id”:“2bb124ce.f9522c”,“type”:“switch”,“z”:“ba081bb8.8696c8”,“name”:“HoldUP”,“property”:“press”,“propertyType”:“flow”,“rules”:[{“t”:“true”}],“checkall”:“true”,“repair”:false,“outputs”:1,“x”:1215,“y”:540,“wires”:[[“ae8ff38a.544aa”]]},{“id”:“4c460d0c.6a34f4”,“type”:“switch”,“z”:“ba081bb8.8696c8”,“name”:“HoldUP”,“property”:“press”,“propertyType”:“flow”,“rules”:[{“t”:“true”}],“checkall”:“true”,“repair”:false,“outputs”:1,“x”:1215,“y”:510,“wires”:[[“e22c7081.0a47c”]]},{“id”:“167aace4.04f2e3”,“type”:“change”,“z”:“ba081bb8.8696c8”,“name”:“msg.feedback”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“feedback”,“tot”:“msg”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:890,“y”:510,“wires”:[[“675b4312.f8843c”]]},{“id”:“2480aefe.53c362”,“type”:“change”,“z”:“ba081bb8.8696c8”,“name”:“msg.feedback”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“feedback”,“tot”:“msg”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:890,“y”:540,“wires”:[[“cd5e817.5e4948”]]},{“id”:“41d080e7.13669”,“type”:“change”,“z”:“ba081bb8.8696c8”,“name”:“start”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“true”,“tot”:“bool”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:515,“y”:525,“wires”:[[“cee3023c.be0b8”]]},{“id”:“91759de8.b98cf”,“type”:“change”,“z”:“ba081bb8.8696c8”,“name”:“stop”,“rules”:[{“t”:“set”,“p”:“payload”,“pt”:“msg”,“to”:“false”,“tot”:“bool”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:515,“y”:570,“wires”:[[“cee3023c.be0b8”]]},{“id”:“cee3023c.be0b8”,“type”:“change”,“z”:“ba081bb8.8696c8”,“name”:“Hold UP”,“rules”:[{“t”:“set”,“p”:“press”,“pt”:“flow”,“to”:“payload”,“tot”:“msg”}],“action”:"",“property”:"",“from”:"",“to”:"",“reg”:false,“x”:675,“y”:540,“wires”:[[]]},{“id”:“12db6c47.dd0874”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Fav Devices”,“func”:“var x = msg.payload.action;\n\nif(x === “arrow_right_release”){\n flow.set(“activeDevice”, “light.led_kitchen”); \n}\n\nif(x === “arrow_left_release”){\n flow.set(“activeDevice”, “light.led_hallway”); \n}\nreturn msg;”,“outputs”:1,“noerr”:0,“x”:535,“y”:465,“wires”:[[]]},{“id”:“ae7b13d8.753ac”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Set devices”,“func”:“var x = msg.payload;\nglobal.set(“ZigbeeLights”, x);\n”,“outputs”:1,“noerr”:0,“x”:370,“y”:60,“wires”:[[]]},{“id”:“d8e27aae.6083f8”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:“Yeelight Devices”,“topic”:"",“payload”:"[“light.led_kitchen”,“light.led_hallway”]",“payloadType”:“json”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:200,“y”:60,“wires”:[[“ae7b13d8.753ac”]]},{“id”:“a570b72c.a34f58”,“type”:“api-call-service”,“z”:“ba081bb8.8696c8”,“name”:"",“server”:“710f5d9e.1377e4”,“version”:1,“debugenabled”:false,“service_domain”:“homeassistant”,“service”:"",“entityId”:"{{payload.entity_id}}",“data”:"",“dataType”:“json”,“mergecontext”:"",“output_location”:"",“output_location_type”:“none”,“mustacheAltTags”:false,“x”:1330,“y”:220,“wires”:[[]]},{“id”:“980b76b6.fb0358”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:"",“topic”:"",“payload”:“toggle”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:90,“y”:380,“wires”:[[“7029c8e8.0d1b28”]]},{“id”:“ccc7c1de.f2e77”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:"",“topic”:"",“payload”:“arrow_right_release”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:110,“y”:440,“wires”:[[“7029c8e8.0d1b28”]]},{“id”:“2b2fc3ca.47789c”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:"",“topic”:"",“payload”:“arrow_right_click”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:160,“y”:520,“wires”:[[“7029c8e8.0d1b28”]]},{“id”:“2a9106bb.3e2afa”,“type”:“api-call-service”,“z”:“ba081bb8.8696c8”,“name”:"",“server”:“710f5d9e.1377e4”,“version”:1,“debugenabled”:false,“service_domain”:“homeassistant”,“service”:"",“entityId”:"{{payload.entity_id}}",“data”:"{“brightness”:{{payload.brightness}}}",“dataType”:“json”,“mergecontext”:"",“output_location”:"",“output_location_type”:“none”,“mustacheAltTags”:false,“x”:1330,“y”:360,“wires”:[[]]},{“id”:“cdf3ce46.088c9”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Save Value”,“func”:“var state = msg.payload.state;\nvar brightness = msg.payload.brightness;\n\nflow.set(“light.led_hallway_brightness”, brightness);\nflow.set(“light.led_hallway_state”, state);”,“outputs”:1,“noerr”:0,“x”:390,“y”:125,“wires”:[[]]},{“id”:“3d26682e.f1fa98”,“type”:“function”,“z”:“ba081bb8.8696c8”,“name”:“Save Value”,“func”:“var state = msg.payload.state;\nvar brightness = msg.payload.brightness;\n\nflow.set(“light.led_kitchen_brightness”, brightness);\nflow.set(“light.led_kitchen_state”, state);\n”,“outputs”:1,“noerr”:0,“x”:380,“y”:185,“wires”:[[]]},{“id”:“e6d1402c.226b”,“type”:“api-current-state”,“z”:“ba081bb8.8696c8”,“name”:"",“server”:“710f5d9e.1377e4”,“version”:1,“outputs”:1,“halt_if”:"",“halt_if_type”:“str”,“halt_if_compare”:“is”,“override_topic”:false,“entity_id”:“light.led_hallway”,“state_type”:“num”,“state_location”:“payload”,“override_payload”:“msg”,“entity_location”:“data”,“override_data”:“msg”,“blockInputOverrides”:false,“x”:160,“y”:125,“wires”:[[“cdf3ce46.088c9”]]},{“id”:“6f21d36a.3e0b7c”,“type”:“api-current-state”,“z”:“ba081bb8.8696c8”,“name”:"",“server”:“710f5d9e.1377e4”,“version”:1,“outputs”:1,“halt_if”:"",“halt_if_type”:“str”,“halt_if_compare”:“is”,“override_topic”:false,“entity_id”:“light.led_kitchen”,“state_type”:“num”,“state_location”:“payload”,“override_payload”:“msg”,“entity_location”:“data”,“override_data”:“msg”,“blockInputOverrides”:false,“x”:160,“y”:185,“wires”:[[“3d26682e.f1fa98”]]},{“id”:“7c4a407a.1a2d7”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:"",“topic”:"",“payload”:“brightness_up_click”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:150,“y”:620,“wires”:[[“7029c8e8.0d1b28”]]},{“id”:“c799c691.721ab8”,“type”:“debug”,“z”:“ba081bb8.8696c8”,“name”:"",“active”:true,“tosidebar”:true,“console”:false,“tostatus”:false,“complete”:“false”,“x”:1370,“y”:440,“wires”:[]},{“id”:“c711eabf.7ab5c8”,“type”:“inject”,“z”:“ba081bb8.8696c8”,“name”:"",“topic”:"",“payload”:“brightness_down_click”,“payloadType”:“str”,“repeat”:"",“crontab”:"",“once”:false,“onceDelay”:0.1,“x”:140,“y”:660,“wires”:[[“7029c8e8.0d1b28”]]},{“id”:“710f5d9e.1377e4”,“type”:“server”,“z”:"",“name”:“Home Assistant”,“addon”:true}]

maybe you can use the brightness_step or brightness_step_pct parameters with light.turn_on service. makes it less complicated.

If you don’t need to go back to the current brightness later, then the previous answer looks like the best option.

However, if you need the original state for something later in your flow just write it to the msg (not the payload but msg.whatever), or to the flow or global context before you change it and reference it again when you need it.

https://nodered.org/docs/user-guide/context

If it’s something other than the state you want to keep then write all the entity info into a message, flow or global context variable and pick out the bit you need (the debug node is you friend to find and copy the right path from the message)

New Flow:

  1. Add a state change for the light you want to watch.
  2. Connect it to a Change node.
  3. In the Change node, use a single SET rule. Select global instead of msg. In the first field, add a custom name for your global variable with or without a path.

For example SET global.lights.brightness -or- SET global.bananas (whatever you want here).

In the next line (the “to” line) select msg, then enter this:

data.new_state.attributes.brightness

Now you might ask, “how did I know where to get that information?”

Simple. Hook up a debug node to your state change node and set the debug node to entire msg object. Trigger it, and look at the debug data. The path to your entity’s data is in that message, and you can use it.

What you’re doing is taking that value (Brightness) and storing it in a global variable.

In my example, I stored the brightness value in global.status.brightness because I have a lot of information stored as an array under the global variable status. So this node just adds another pairing to the data set which can be referred to in any other nodes.

You can then use a switch node to run a test on the value of the variable, and you can use any other nodes to extract that value for calculating whatever you want.

[{"id":"60a867db.0233a8","type":"server-state-changed","z":"dc677967.105608","name":"","server":"c7cbc76b.fd06a8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.workshop","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":160,"y":320,"wires":[["6bbb924d.9764dc"]]},{"id":"337a2684.0b1cfa","type":"debug","z":"dc677967.105608","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":670,"y":340,"wires":[]},{"id":"6bbb924d.9764dc","type":"change","z":"dc677967.105608","name":"","rules":[{"t":"set","p":"status.brightness","pt":"global","to":"data.new_state.attributes.brightness","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":400,"wires":[["337a2684.0b1cfa"]]},{"id":"c7cbc76b.fd06a8","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Seems unnecessarily complicated

Storing a value to a global variable is extremely simple and it allows you to use it anywhere else you’d like.

It’s the same thing you did in your Edit Current State node, just a different way. Either way, both methods require at least two nodes as the ECS node needs input.

I changed the state node to a poll state, so I can get updated on the brightness level.
In the debug node it shows me the info for the brightness.


What I have noticed is that when I say for a variable
brightness= msg.payload.data.attributes.brightness; return msg.payload it gives me an error “TypeError: Cannot read property ‘atributes’ of undefined”

If I leave it to save all msg.payload, when I call the variable in the function, and I attache say msg.payload=variable; return msg; in the debug note, it shows only the topic and payload

I managed to make it work.
The problem was when saving the brightness data.
Instead of brightness=msg.payload.data.attributes.brightness , I should have written brightness=msg.data.attributes.brightness.
And the input is from a poll state node.
Could a poll node overload home assistant?

1 Like

Yup…remove payload from the string. I don’t know about overloading. I’ve done this a bit differently. I have an inject node with an interval that sends a periodic heartbeat, and then I route that heartbeat through whatever flow I want to trigger actions instead of polling each entity.

I use the “link in” and “link out” nodes to connect flows between tabs and route signals.