Newbie question - using a global variable as brightness?

Hi, sorry, this is me being a very bad coder - help please.
How can I do “brightness_pct”:morning ?
I’ve setup a function node

global.set("morning","100");
global.set("afternoon","90");
global.set("evening","70");
global.set("night","1");
return msg;

but I don’t know how to call it within my service node :frowning:
I find lots of examples on calling those in functions to output msg but not in functions.

I dont know the straight forward method but i have a work arround. You can try this if nobody gives any info. Just try this. It should do the trick.

[{"id":"302576ec.1a904a","type":"function","z":"51045e07.bbf87","name":"","func":"msg.payload = flow.get(\"morning\")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":170,"y":80,"wires":[["f3fa296.0c68fd8"]]},{"id":"f3fa296.0c68fd8","type":"api-call-service","z":"51045e07.bbf87","name":"","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wasdeconzgroup","data":"{\"brightness_pct\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":350,"y":80,"wires":[[]]},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]
1 Like

Thank you so much @sheminasalam . This works. I made a writeup version with example below.
It does:
Sensor to detect if day/afternoon/etc
Set brightness to specific % during those phases
Reuse for any flows where you want to call lights.
(My case is a cat walking at night and triggering sensors - I want to save on energy)

[{"id":"7fef90a1.ec014","type":"tab","label":"Set brightness according to day time","disabled":false,"info":""},{"id":"6df601b2.d507b","type":"debug","z":"7fef90a1.ec014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":420,"wires":[]},{"id":"44f6bc48.529104","type":"api-call-service","z":"7fef90a1.ec014","name":"Light ON","server":"a0eea9da.0fe1d8","version":1,"debugenabled":true,"service_domain":"light","service":"turn_on","entityId":"light.wasdeconzgroup","data":"{\"brightness_pct\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":560,"y":420,"wires":[["6df601b2.d507b"]]},{"id":"302576ec.1a904a","type":"function","z":"7fef90a1.ec014","name":"Set brightness","func":"msg.payload = global.get(\"dayphasebrightness\")\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":420,"wires":[["44f6bc48.529104"]]},{"id":"81af8b92.df1098","type":"inject","z":"7fef90a1.ec014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":420,"wires":[["302576ec.1a904a"]]},{"id":"164f62c2.0e411d","type":"server-state-changed","z":"7fef90a1.ec014","name":"Day Phase","server":"a0eea9da.0fe1d8","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.day_phase","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":180,"wires":[["77c6b46c.f86eec"]]},{"id":"77c6b46c.f86eec","type":"switch","z":"7fef90a1.ec014","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Morning","vt":"str"},{"t":"eq","v":"Day","vt":"str"},{"t":"eq","v":"Evening","vt":"str"},{"t":"eq","v":"Night","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":410,"y":180,"wires":[["4580a4df.a1ad7c"],["dc8bb1e8.b17ff"],["373cae69.2cd632"],["a7a4e698.7f74a8"]]},{"id":"4580a4df.a1ad7c","type":"function","z":"7fef90a1.ec014","name":"morning:100","func":"global.set(\"dayphasebrightness\",100);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":670,"y":140,"wires":[[]]},{"id":"dc8bb1e8.b17ff","type":"function","z":"7fef90a1.ec014","name":"day: 90","func":"global.set(\"dayphasebrightness\",90);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":180,"wires":[[]]},{"id":"373cae69.2cd632","type":"function","z":"7fef90a1.ec014","name":"Evening: 70","func":"global.set(\"dayphasebrightness\",70);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":670,"y":220,"wires":[[]]},{"id":"a7a4e698.7f74a8","type":"function","z":"7fef90a1.ec014","name":"night: 1","func":"global.set(\"dayphasebrightness\",1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":260,"wires":[[]]},{"id":"88e2252.c0566d8","type":"comment","z":"7fef90a1.ec014","name":"Create a sensor (click for code)","info":"#add this to your configuration.yaml\n#reload \"template\" entities and groups\n#click out and back to node-red\n  - platform: template\n    sensors:\n       day_phase:\n        friendly_name: 'Day Phase'\n#        entity_id: sensor.time\n        value_template: >\n          {% if now() > now().replace(hour=5).replace(minute=0).replace(second=0) and\n            now() < now().replace(hour=7).replace(minute=0).replace(second=0) %}\n              Morning\n          {% elif states('sun.sun') == 'above_horizon' or\n            now() > now().replace(hour=5).replace(minute=0).replace(second=0) and\n            now() < now().replace(hour=12).replace(minute=0).replace(second=0) %}\n              Day\n          {% elif now() < now().replace(hour=21).replace(minute=45).replace(second=0) and\n            now() > now().replace(hour=5).replace(minute=0).replace(second=0) %}\n              Evening\n          {% else %}\n              Night\n          {% endif %}\n        icon_template: >\n          {% set phase = states('sensor.day_phase') %}\n          {% if phase == 'Morning' %} mdi:weather-sunset-up\n          {% elif phase =='Day' %} mdi:weather-sunny\n          {% elif phase == 'Evening' %} mdi:weather-sunset-down\n          {% else %} mdi:weather-night\n          {% endif %}","x":230,"y":100,"wires":[]},{"id":"c2b9642a.52e3a8","type":"comment","z":"7fef90a1.ec014","name":"This sets your brightness","info":"","x":710,"y":100,"wires":[]},{"id":"672001e8.5376a","type":"comment","z":"7fef90a1.ec014","name":"Example of use","info":"Of course, replace \"timestamp\" with a button, sensor or else to trigger it","x":180,"y":340,"wires":[]},{"id":"b86b83ce.90ea6","type":"comment","z":"7fef90a1.ec014","name":"Universal","info":"No need to change this - you can reuse in all cases","x":340,"y":380,"wires":[]},{"id":"d8fcc9e0.60b0b8","type":"comment","z":"7fef90a1.ec014","name":"Example code under \"data\"","info":"{\"brightness_pct\":\"{{payload}}\"}","x":610,"y":380,"wires":[]},{"id":"2ea1b154.a492ae","type":"comment","z":"7fef90a1.ec014","name":"Delete if not needed","info":"You don't need this one unless you want to debug","x":850,"y":380,"wires":[]},{"id":"f0254a2a.68c458","type":"comment","z":"7fef90a1.ec014","name":"This is a global code to set the brightness wherever you want to use it.  Only set this once, it can be used across all your flows since it's a \"global\" parameter","info":"Why do I use this?\nI have a cat... and lots of sensors and lights.\nI want to set the brighness to almost nothing during the night since the cat constantly triggers my sensors.","x":550,"y":60,"wires":[]},{"id":"a0eea9da.0fe1d8","type":"server","name":"Home Assistant New","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

This is a good example for using the Merge Context field.

[{"id":"6df601b2.d507b","type":"debug","z":"7fef90a1.ec014","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":810,"y":420,"wires":[]},{"id":"44f6bc48.529104","type":"api-call-service","z":"7fef90a1.ec014","name":"Light ON","server":"","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.wasdeconzgroup","data":"","dataType":"json","mergecontext":"dayPhaseDefaults","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":560,"y":420,"wires":[["6df601b2.d507b"]]},{"id":"81af8b92.df1098","type":"inject","z":"7fef90a1.ec014","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":420,"wires":[["44f6bc48.529104"]]},{"id":"164f62c2.0e411d","type":"server-state-changed","z":"7fef90a1.ec014","name":"Day Phase","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.day_phase","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":160,"y":180,"wires":[["77c6b46c.f86eec"]]},{"id":"77c6b46c.f86eec","type":"switch","z":"7fef90a1.ec014","name":"Switch","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Morning","vt":"str"},{"t":"eq","v":"Day","vt":"str"},{"t":"eq","v":"Evening","vt":"str"},{"t":"eq","v":"Night","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":410,"y":180,"wires":[["5ff712f4.b0024c"],["e61f6c07.cfe0c"],["207b006d.64e9"],["40a9890e.5a6328"]]},{"id":"88e2252.c0566d8","type":"comment","z":"7fef90a1.ec014","name":"Create a sensor (click for code)","info":"#add this to your configuration.yaml\n#reload \"template\" entities and groups\n#click out and back to node-red\n  - platform: template\n    sensors:\n       day_phase:\n        friendly_name: 'Day Phase'\n#        entity_id: sensor.time\n        value_template: >\n          {% if now() > now().replace(hour=5).replace(minute=0).replace(second=0) and\n            now() < now().replace(hour=7).replace(minute=0).replace(second=0) %}\n              Morning\n          {% elif states('sun.sun') == 'above_horizon' or\n            now() > now().replace(hour=5).replace(minute=0).replace(second=0) and\n            now() < now().replace(hour=12).replace(minute=0).replace(second=0) %}\n              Day\n          {% elif now() < now().replace(hour=21).replace(minute=45).replace(second=0) and\n            now() > now().replace(hour=5).replace(minute=0).replace(second=0) %}\n              Evening\n          {% else %}\n              Night\n          {% endif %}\n        icon_template: >\n          {% set phase = states('sensor.day_phase') %}\n          {% if phase == 'Morning' %} mdi:weather-sunset-up\n          {% elif phase =='Day' %} mdi:weather-sunny\n          {% elif phase == 'Evening' %} mdi:weather-sunset-down\n          {% else %} mdi:weather-night\n          {% endif %}","x":230,"y":100,"wires":[]},{"id":"c2b9642a.52e3a8","type":"comment","z":"7fef90a1.ec014","name":"This sets your brightness","info":"","x":710,"y":100,"wires":[]},{"id":"672001e8.5376a","type":"comment","z":"7fef90a1.ec014","name":"Example of use","info":"Of course, replace \"timestamp\" with a button, sensor or else to trigger it","x":180,"y":340,"wires":[]},{"id":"d8fcc9e0.60b0b8","type":"comment","z":"7fef90a1.ec014","name":"Example code under \"data\"","info":"{\"brightness_pct\":\"{{payload}}\"}","x":610,"y":380,"wires":[]},{"id":"2ea1b154.a492ae","type":"comment","z":"7fef90a1.ec014","name":"Delete if not needed","info":"You don't need this one unless you want to debug","x":850,"y":380,"wires":[]},{"id":"f0254a2a.68c458","type":"comment","z":"7fef90a1.ec014","name":"This is a global code to set the brightness wherever you want to use it.  Only set this once, it can be used across all your flows since it's a \"global\" parameter","info":"Why do I use this?\nI have a cat... and lots of sensors and lights.\nI want to set the brighness to almost nothing during the night since the cat constantly triggers my sensors.","x":550,"y":60,"wires":[]},{"id":"5ff712f4.b0024c","type":"change","z":"7fef90a1.ec014","name":"morning: 100","rules":[{"t":"set","p":"dayPhaseDefaults","pt":"global","to":"{\"brightness_pct\": 100}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":662,"y":144,"wires":[[]]},{"id":"e61f6c07.cfe0c","type":"change","z":"7fef90a1.ec014","name":"day: 90","rules":[{"t":"set","p":"dayPhaseDefaults","pt":"global","to":"{\"brightness_pct\": 90}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":652,"y":176,"wires":[[]]},{"id":"207b006d.64e9","type":"change","z":"7fef90a1.ec014","name":"evening: 70","rules":[{"t":"set","p":"dayPhaseDefaults","pt":"global","to":"{\"brightness_pct\": 70}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":662,"y":208,"wires":[[]]},{"id":"40a9890e.5a6328","type":"change","z":"7fef90a1.ec014","name":"night: 1","rules":[{"t":"set","p":"dayPhaseDefaults","pt":"global","to":"{\"brightness_pct\": 1}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":652,"y":240,"wires":[[]]}]

https://zachowj.github.io/node-red-contrib-home-assistant-websocket/node/call-service.html#merging

2 Likes

Thanks! I had search a bit that route as I suspected this made sense but coudn’t find the info! It works great! And simplifies the flow. Thanks

old thread but found this extremely useful, thank you very much for writing it out and even including the sensor code, really appreciate it. also big thank you to @Kermit for showing the merge context awesome to take this up a notch. you have both ruined my weekend reworking some flows