Condition based on light attribute

Hello, I’m new to Node Red, hoping someone can give me some guidance with a problem I’m having. I want Node Red to look at the attributes for one of my lights before moving on with the flow. For instance, I’m trying to create an automation that looks at the brightness of one of my lights and if it’s lets say set to a brightness of 125 it halts. I had been using the “Current State” api but it appears that it only looks at the state (I.E. On or Off) instead of any of the other attributes.

Most HA nodes output more than one thing checkout the docs for each node to see their outputs or use a debug node set to output the whole message object.

[{"id":"ae1f13fb.09c9","type":"inject","z":"f656078a.752de8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":186,"y":528,"wires":[["eb06da2f.d03998"]]},{"id":"52c41792.2ae0c8","type":"debug","z":"f656078a.752de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":742,"y":528,"wires":[]},{"id":"eb06da2f.d03998","type":"api-current-state","z":"f656078a.752de8","name":"","server":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.light1","state_type":"str","state_location":"","override_payload":"none","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":382,"y":528,"wires":[["401e1f1b.76672"]]},{"id":"401e1f1b.76672","type":"switch","z":"f656078a.752de8","name":"","property":"data.attributes.brightness","propertyType":"msg","rules":[{"t":"neq","v":"125","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":578,"y":528,"wires":[["52c41792.2ae0c8"]]}]

image

[{"id":"ae1f13fb.09c9","type":"inject","z":"f656078a.752de8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":186,"y":528,"wires":[["eb06da2f.d03998"]]},{"id":"52c41792.2ae0c8","type":"debug","z":"f656078a.752de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":614,"y":528,"wires":[]},{"id":"eb06da2f.d03998","type":"api-current-state","z":"f656078a.752de8","name":"","server":"","version":1,"outputs":2,"halt_if":"$entity().attributes.brightness != 125","halt_if_type":"jsonata","halt_if_compare":"jsonata","override_topic":false,"entity_id":"light.light1","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":382,"y":528,"wires":[["52c41792.2ae0c8"],[]]}]
1 Like

Not sure how this helps me. I tried what you suggested and I see that the entity is outputting the attributes in a msg object but I still don’t get how to make a flow respond to just that attribute

He told you. If you copy paste the first example, the flow will only continue if the light brightness is not 125. What do you mean by halt? what should halt?

Ok, I’m even more confused now. Where do i copy/paste that example? Do i enter it into the Current State node as JSON data?

by copy and paste I meant import to your NR instance kermit’s first example. If you chain those two nodes in your flow, it’ll only continue if brightness is different than 125

Sorry, this is way over my head. I can’t make any sense of the example @Kermit posted.

Post the flow you’re trying to achieve, explain what you’re trying to achieve and I’ll do it for you. Post the code

1 Like

I think you might want to head over to the getting started guide of Node RED. The debug node shows you what the message looks like. You can clearly see that brightness: 204 is in the message data which means you can use it in a switch node, a change node or any other type of node you desire. Tbh that’s kind of Node RED 101 so if you’re not familiar with how to work with messages that’s going to be a problem since Node RED is entirely about taking actions on messages as they move from one node to another.

Here’s a cookbook example from their guide on routing a message based on one of its properties which is exactly what you’re trying to do. If Kermit’s example isn’t making sense to you then perhaps you can start from that instead and add in a current state node.

Also for future reference, you can export any chunk of nodes/flows from the Node RED editor. Those exports are in JSON format. So when you see a block of compressed JSON in a discussion/document about Node RED you can import it and then see and try it out for yourself. Here’s the guide on importing/exporting flows

1 Like

Thank you! After your post it was kind of like an “ah ha” moment for me. Once I understood what you guys were talking about by import Kermit’s example it all started making sense. I see now how he was able to utilize the brightness attribute in the node. Essentially what I am trying to do is have one of my lights dim automatically based on sunset times, however I want three conditions to be meet before that can happen. 1. I must be home 2. the light must already be turned on and 3. the light can’t already be at a certain brightness (I have a scene called movie mode that dims the light very low, don’t want this sunset timer triggering and adjusting the brightness while that scene is active.)

This is what my flow looks like. I think this will do the trick, just have to wait for sunset tomorrow to see if it triggers properly now.

[{"id":"4d9f1598.2b8fec","type":"tab","label":"WLED Dimmer","disabled":false,"info":""},{"id":"2c4c55a2.29615a","type":"switch","z":"4d9f1598.2b8fec","name":"","property":"topic","propertyType":"flow","rules":[{"t":"eq","v":"sunsetStart","vt":"str"},{"t":"eq","v":"dusk","vt":"str"},{"t":"eq","v":"night","vt":"str"}],"checkall":"true","repair":false,"outputs":3,"x":210,"y":260,"wires":[["e9de35d2.467d9"],["85f147e7.ea2b6"],["b9b9f37a.e1ea6"]]},{"id":"85f147e7.ea2b6","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is Mike home?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"person.mike","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":400,"y":260,"wires":[["34b243c4.bbc434"],[]]},{"id":"1613f780.b3cae9","type":"api-call-service","z":"4d9f1598.2b8fec","name":"Set 50% brightness","server":"951131b4.8fdea","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\"entity_id\":\"light.wled\",\"brightness\":128}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":260,"wires":[[]]},{"id":"41d9650e.0b2024","type":"api-call-service","z":"4d9f1598.2b8fec","name":"Set 30% brightness","server":"951131b4.8fdea","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.wled","data":"{\"entity_id\":\"light.wled\",\"brightness\":76}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":320,"wires":[["210394f6.96bd3c"]]},{"id":"d4e9ee9.86f6b9","type":"sun events","z":"4d9f1598.2b8fec","testmode":true,"verbose":true,"topic":"","name":"","x":60,"y":260,"wires":[["2c4c55a2.29615a"]]},{"id":"210394f6.96bd3c","type":"api-call-service","z":"4d9f1598.2b8fec","name":"WLED Nightlight","server":"c882c791.7422d8","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"switch.wled_nightlight","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1160,"y":320,"wires":[[]]},{"id":"7784b828.473d48","type":"api-call-service","z":"4d9f1598.2b8fec","name":"Set 80% brightness","server":"951131b4.8fdea","version":1,"debugenabled":true,"service_domain":"wled","service":"preset","entityId":"light.wled","data":"{\"entity_id\":\"light.wled\",\"preset\":10}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":940,"y":200,"wires":[[]]},{"id":"e977fa02.91576","type":"inject","z":"4d9f1598.2b8fec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":560,"y":900,"wires":[["fe84fc68.babc98"]]},{"id":"e5964f23.a86648","type":"debug","z":"4d9f1598.2b8fec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":988,"y":900,"wires":[]},{"id":"fe84fc68.babc98","type":"api-current-state","z":"4d9f1598.2b8fec","name":"","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"$entity().attributes.brightness != 125","halt_if_type":"jsonata","halt_if_compare":"jsonata","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":756,"y":900,"wires":[["e5964f23.a86648"],[]]},{"id":"e9de35d2.467d9","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is Mike home?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"person.mike","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":400,"y":200,"wires":[["28d4d65d.80cea2"],[]]},{"id":"b9b9f37a.e1ea6","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is Mike home?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"home","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"person.mike","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":400,"y":320,"wires":[["8aa6889d.e86438"],[]]},{"id":"28d4d65d.80cea2","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is the WLED on / Brightness set?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"$entity().attributes.brightness != 5","halt_if_type":"jsonata","halt_if_compare":"jsonata","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":660,"y":200,"wires":[["7784b828.473d48"],[]]},{"id":"34b243c4.bbc434","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is the WLED on / Brightness set?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"$entity().attributes.brightness != 5","halt_if_type":"jsonata","halt_if_compare":"jsonata","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":660,"y":260,"wires":[["1613f780.b3cae9"],[]]},{"id":"8aa6889d.e86438","type":"api-current-state","z":"4d9f1598.2b8fec","name":"Is the WLED on / Brightness set?","server":"c882c791.7422d8","version":1,"outputs":2,"halt_if":"$entity().attributes.brightness != 5","halt_if_type":"jsonata","halt_if_compare":"jsonata","override_topic":false,"entity_id":"light.wled","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":660,"y":320,"wires":[["41d9650e.0b2024"],[]]},{"id":"c882c791.7422d8","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false},{"id":"951131b4.8fdea","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":false}]


Great! Yea its pretty nice once you get the hang of it.

You don’t actually have to wait for the sun to change to test it though. The inject not only starts flows, it can start a flow from a specific point with a specific message (doesn’t have to be a timestamp). So you can use it inject the sunset topics in and test the rest of your flow without waiting for that particular time of day like this:
Screen Shot 2021-01-14 at 9.32.44 AM

Although on the topic of the flow, It seems like all your “Is Mike home?” and “Is the WLED on /Brightness set?” nodes are identical. Wouldn’t it be easier to change it to be like this instead of having those duplicates:

Thanks for the tip about the inject node. When i set that up the flow seems to be working as intended, so thanks very much for your help with that.

Your way of organizing the flow does seem much more streamlined. I moved the switch node to the back of the flow like you have it. Took me a couple minutes of tinkering with it to realize i had to change the property value from msg.payload to msg.topic before everything with the flow worked as intended.

1 Like