Skip to content

Instantly share code, notes, and snippets.

@nygma2004
Created December 6, 2016 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nygma2004/073af07bb59e358873bc0dc83c595420 to your computer and use it in GitHub Desktop.
Save nygma2004/073af07bb59e358873bc0dc83c595420 to your computer and use it in GitHub Desktop.
Monitor printer toner in Node Red and add toner to Evernote shopping list via IFTT

In this video I explain my Node Red flow which gets diagnostic information from a network enabled printer to determine if the toner is running low. And add toner to the shopping list in Evernote via IFTT.

I have built this flow for my black and white printer, so the logic is limited to a single toner. For color machines the flow needs to be enhanced in a few places to make sure all 4 toners are read and taken into account when information is extracted, and the low toner level is identified.

There is a video in which I explain the logic I implemented in this flow: https://youtu.be/0EGoyn-llTY The first 11:27 minutes are about SNMP and how to determine the OIDs to get toner values. If you are familiar with that, just skip right into 11:27.

[{"id":"9887d014.bb2fd","type":"tab","label":"Printer SNMP"},{"id":"cb2ac8c8.b57af8","type":"inject","z":"9887d014.bb2fd","name":"","topic":"","payload":"","payloadType":"date","repeat":"3600","crontab":"","once":false,"x":114,"y":107,"wires":[["4b950c50.5bd6b4","f412be4f.b2183","741f337a.407dac","d7048962.395918"]]},{"id":"4b950c50.5bd6b4","type":"exec","z":"9887d014.bb2fd","command":"snmpwalk -v 2c -c public 192.168.1.55 1.3.6.1.2.1.43.11.1.1.8.1.1","addpay":true,"append":"","useSpawn":"","timer":"","name":"Toner Max","x":310,"y":102.5,"wires":[["218ba435.dd600c"],[],[]]},{"id":"75011218.dca99c","type":"join","z":"9887d014.bb2fd","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","timeout":"20","count":"4","x":692,"y":151,"wires":[["96c8d671.480228"]]},{"id":"218ba435.dd600c","type":"function","z":"9887d014.bb2fd","name":"Get details","func":"msg.topic = \"toner_max\";\nmsg.payload = parseInt(msg.payload.slice(msg.payload.indexOf(\":\")+2,1000).trim());\nreturn msg;","outputs":1,"noerr":0,"x":498,"y":89,"wires":[["75011218.dca99c"]]},{"id":"cce4aa3f.7892c8","type":"debug","z":"9887d014.bb2fd","name":"","active":false,"console":"false","complete":"false","x":420,"y":441,"wires":[]},{"id":"f412be4f.b2183","type":"exec","z":"9887d014.bb2fd","command":"snmpwalk -v 2c -c public 192.168.1.55 1.3.6.1.2.1.43.11.1.1.9.1.1","addpay":true,"append":"","useSpawn":"","timer":"","name":"Toner Level","x":310,"y":164,"wires":[["23b8edcf.95d812"],[],[]]},{"id":"23b8edcf.95d812","type":"function","z":"9887d014.bb2fd","name":"Get details","func":"msg.topic = \"toner_level\";\nmsg.payload = parseInt(msg.payload.slice(msg.payload.indexOf(\":\")+2,1000).trim());\nreturn msg;","outputs":1,"noerr":0,"x":498,"y":150.5,"wires":[["75011218.dca99c"]]},{"id":"741f337a.407dac","type":"exec","z":"9887d014.bb2fd","command":"snmpwalk -v 2c -c public 192.168.1.55 1.3.6.1.2.1.43.10.2.1.4.1.1","addpay":true,"append":"","useSpawn":"","timer":"","name":"Page count","x":311,"y":223,"wires":[["c0b0f756.c27f08"],[],[]]},{"id":"c0b0f756.c27f08","type":"function","z":"9887d014.bb2fd","name":"Get details","func":"msg.topic = \"page_count\";\nmsg.payload = parseInt(msg.payload.slice(msg.payload.indexOf(\":\")+2,1000).trim());\nreturn msg;","outputs":1,"noerr":0,"x":499,"y":209.5,"wires":[["75011218.dca99c"]]},{"id":"96c8d671.480228","type":"function","z":"9887d014.bb2fd","name":"Calculation logic","func":"// Treshold toner level before notification is sent\nvar treshold = 10;\n\nmsg.payload.notification=false;\nif (msg.payload!==\"\" || msg.payload!==undefined) {\n if (isNaN(msg.payload.toner_max) || isNaN(msg.payload.toner_level) || msg.payload.toner_max===null || msg.payload.toner_level===null) {\n // No valid values returned, e.g. printer is turned off\n msg.payload.toner_percentage=0;\n msg.payload.valid=-1;\n } else {\n if (msg.payload.toner_max<0 || msg.payload.toner_level<0) {\n // No measurable toner levels are returned\n msg.payload.toner_percentage=\"unknown\";\n msg.payload.valid=0;\n } else {\n // There are proper values returned\n msg.payload.toner_percentage=Math.floor(msg.payload.toner_level/msg.payload.toner_max*100,0);\n msg.payload.valid=1;\n msg.payload.value1=\"Black toner\";\n \n // Check if the toner level is below treshold\n if (msg.payload.toner_percentage<treshold) {\n // Check if notification was sent already\n if (!global.get(\"tonernotification\") || global.get(\"tonernotification\")===null ) {\n // Flag for notification and set the global variable\n msg.payload.notification=true;\n global.set(\"tonernotification\",true);\n }\n }\n // This piece resets the notification if the toner got replaced and the\n // toner percentage is up again\n if (msg.payload.toner_percentage>treshold+10) {\n if (global.get(\"tonernotification\")) {\n global.set(\"tonernotification\",false);\n }\n }\n }\n }\n} else {\n // Something went completely sideways\n msg.payload.toner_percentage=0;\n msg.payload.valid=-2;\n}\n\n// Add formatted timestamp\nvar now = new Date();\nvar yyyy = now.getFullYear();\nvar mm = now.getMonth() < 9 ? \"0\" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based\nvar dd = now.getDate() < 10 ? \"0\" + now.getDate() : now.getDate();\nvar hh = now.getHours() < 10 ? \"0\" + now.getHours() : now.getHours();\nvar mmm = now.getMinutes() < 10 ? \"0\" + now.getMinutes() : now.getMinutes();\nvar ss = now.getSeconds() < 10 ? \"0\" + now.getSeconds() : now.getSeconds();\nmsg.payload.timestamp = dd + \".\" + mm + \".\" + yyyy + \" \" + hh + \":\" + mmm + \":\" + ss;\nreturn msg;","outputs":1,"noerr":0,"x":218,"y":477,"wires":[["6d70b356.613e6c","cce4aa3f.7892c8","571e7c.072d2184"]]},{"id":"cfdc51f3.12666","type":"comment","z":"9887d014.bb2fd","name":"Get values with SNMP","info":"","x":274,"y":42,"wires":[]},{"id":"9aa36433.0119f8","type":"comment","z":"9887d014.bb2fd","name":"Extract data","info":"","x":494,"y":42,"wires":[]},{"id":"6d70b356.613e6c","type":"switch","z":"9887d014.bb2fd","name":"Check valid state","property":"payload.valid","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","outputs":2,"x":629,"y":479,"wires":[["43ab660a.4e5768","46f5c727.137388"],["43ab660a.4e5768","5d27bdc5.c15d74","46f5c727.137388"]]},{"id":"43ab660a.4e5768","type":"ui_text","z":"9887d014.bb2fd","group":"4e5d44f3.79666c","order":0,"width":0,"height":0,"name":"Page Count","label":"Page Count","format":"{{msg.payload.page_count}}","layout":"row-spread","x":865,"y":478,"wires":[]},{"id":"5d27bdc5.c15d74","type":"ui_text","z":"9887d014.bb2fd","group":"4e5d44f3.79666c","order":0,"width":0,"height":0,"name":"Toner level","label":"Toner","format":"{{msg.payload.page_percentage}} %","layout":"row-spread","x":864,"y":525,"wires":[]},{"id":"8e97d74a.7f1be8","type":"comment","z":"9887d014.bb2fd","name":"UI Update","info":"","x":619,"y":432,"wires":[]},{"id":"571e7c.072d2184","type":"switch","z":"9887d014.bb2fd","name":"Check notification flag","property":"payload.notification","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","outputs":1,"x":419,"y":669,"wires":[["ebade306.e4951"]]},{"id":"fa2d6713.29c6d8","type":"function","z":"9887d014.bb2fd","name":"Test data","func":"msg.payload= {toner_max: 100, toner_level :5, printer_name:\"RICOH SP 213SFNw (828e56)\"};\n\nreturn msg;","outputs":1,"noerr":0,"x":866,"y":285,"wires":[["96c8d671.480228"]]},{"id":"307f1f5c.3b95c","type":"inject","z":"9887d014.bb2fd","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":98,"y":745,"wires":[["20afa329.00522c"]]},{"id":"20afa329.00522c","type":"function","z":"9887d014.bb2fd","name":"Reset notification","func":"global.set(\"tonernotification\",false);\nreturn msg;","outputs":1,"noerr":0,"x":315,"y":744,"wires":[[]]},{"id":"ebade306.e4951","type":"function","z":"9887d014.bb2fd","name":"Format values","func":"return {\n payload: {\n value1 : msg.payload.value1,\n value2 : msg.payload.printer_name,\n value3 : \"\"\n }\n};","outputs":1,"noerr":0,"x":683,"y":668,"wires":[["5b2cfaa.cc70104","e1e4d546.98f5f8"]]},{"id":"d7048962.395918","type":"exec","z":"9887d014.bb2fd","command":"snmpwalk -v 2c -c public 192.168.1.55 1.3.6.1.2.1.43.5.1.1.16.1","addpay":true,"append":"","useSpawn":"","timer":"","name":"Page count","x":311,"y":280,"wires":[["cf4e59bd.5cb898"],[],[]]},{"id":"cf4e59bd.5cb898","type":"function","z":"9887d014.bb2fd","name":"Get details","func":"msg.topic = \"printer_name\";\nmsg.payload = msg.payload.slice(msg.payload.indexOf(\": \")+3,msg.payload.length-2).trim();\nreturn msg;","outputs":1,"noerr":0,"x":499,"y":266.5,"wires":[["75011218.dca99c"]]},{"id":"5b2cfaa.cc70104","type":"debug","z":"9887d014.bb2fd","name":"","active":false,"console":"false","complete":"false","x":866,"y":727,"wires":[]},{"id":"e1e4d546.98f5f8","type":"http request","z":"9887d014.bb2fd","name":"IFTTT Call","method":"POST","ret":"txt","url":"https://maker.ifttt.com/trigger/Toner/with/key/<your key>","tls":"","x":900,"y":668,"wires":[[]]},{"id":"e6164128.da8a4","type":"inject","z":"9887d014.bb2fd","name":"Test","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":707,"y":285,"wires":[["fa2d6713.29c6d8"]]},{"id":"46f5c727.137388","type":"ui_text","z":"9887d014.bb2fd","group":"4e5d44f3.79666c","order":0,"width":0,"height":0,"name":"Last update","label":"Last update: ","format":"{{msg.payload.timestamp}}","layout":"row-right","x":864,"y":569,"wires":[]},{"id":"4e5d44f3.79666c","type":"ui_group","z":"","name":"Printer","tab":"156af96f.f8fd27","order":5,"disp":true,"width":"6"},{"id":"156af96f.f8fd27","type":"ui_tab","z":"","name":"Home","icon":"home","order":"1"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment