Aqara T1 Relays in NodeRED

enter image description here Take the advantage of ZigBee and Aqara T1 switches (review) in your own automation setup. Thanks to cc2531 (flash guide), I'm able to recreate functions from Aqara app AND add power metering to no-neutral version of the Aqara T1. This project should work just fine for other relays from Sonoff, Shelly etc (as long as they can use MQTT)

Features:

  • dashboad – control main lights
  • Power monitoring – control bedside lamp
  • Link Quality – when the projector is in use
  • Grafana & InfluxDB integration –It's optional, but if you want fancy graphs
  • Alexa, Google Assistant integration

Requirements

  • node-red-dashboard
  • node-red-contrib-influxdb
  • node-red-alexa-home-skill (Alexa integration)
  • node-red-contrib-smartnora

Relays with ZigBee2MQTT or MQTT support

Settings

  • Setup Node - Configure On Start section. Define if relay supports power metering (power.enabled), add load info for switches without the power meter and set the price of the elcricity as global.set("PowerCost", 0) per kWh.

    More about me:

If you want to get the latest updates to this project you can follow me via your preferred social media:

And if you feeling like buying me a coffee or supporting me in a more continuous way:

I hope you have enjoyed the project!

[{"id":"993c2aad.77b1c8","type":"tab","label":"Aqara T1 Relays in NodeRED","disabled":false,"info":"\n![enter image description here](https://notenoughtech.com/wp-content/uploads/2021/04/thumbnail-3.jpg)\nTake the advantage of ZigBee and Aqara T1 switches (review) in your own automation setup. Thanks to cc2531 (flash guide), I'm able to recreate functions from Aqara app AND add power metering to no-neutral version of the Aqara T1. This project should work just fine for other relays from Sonoff, Shelly etc (as long as they can use MQTT)\n\n - [Complete instructions](https://notenoughtech.com/home-automation/how-to-use-aqara-t1-with-zigbee2mqtt/)\n \n![](https://notenoughtech.com/wp-content/uploads/2021/04/2021-04-30-17_15_20-Node-RED-Dashboard.jpg)\n\n**Features**:\n-    **dashboad**  – control main lights\n-   **Power monitoring**  – control bedside lamp\n-   **Link Quality**  – when the projector is in use\n-   **Grafana & InfluxDB integration** –It's optional, but if you want fancy graphs\n-   **Alexa**,  **Google Assistant**  integration\n\n## Requirements\n\n - node-red-dashboard\n - node-red-contrib-influxdb\n - node-red-alexa-home-skill (Alexa integration)\n - node-red-contrib-smartnora\n\nRelays with ZigBee2MQTT or MQTT support\n\n - Aqara T1 L&N ([AmazonUK](https://amzn.to/32pMTij), [AmazonUS](https://amzn.to/3tdOovC))\n - Aqara T1 no-neutral ([AmazonUK](https://amzn.to/32pMTij), [AmazonUS](https://amzn.to/3tdOovC))\n - [Shelly 1](https://notenoughtech.com/review/shelly-1-brilliant-software-flawed-hardware/) \n - [Shelly 1L](https://notenoughtech.com/home-automation/shelly-1l/) \n - [Sonoff ZBMini](https://notenoughtech.com/review/sonoff-zbmini-is-in-town/) \n - [Zemismart ZB Switch](https://s.click.aliexpress.com/e/_d8Cx7wT) \n\n# Settings\n\n - **Setup Node** - Configure On Start section. Define if relay supports power metering (power.enabled), add load info for switches without the power meter and set the price of the elcricity as global.set(\"PowerCost\", 0) per kWh.\n \n ## More about me:\n\nIf you want to get the latest updates to this project you can follow me via your preferred social media:\n\n-   [Facebook](https://www.facebook.com/NotEnoughTECH/)\n-   [Twitter](https://twitter.com/NotEnoughTECH)\n-   [Instagram](https://www.instagram.com/notenoughtech/)\n-   [YouTube](https://www.youtube.com/user/Polepositionpage)\n\nAnd if you feeling like buying me a coffee or supporting me in a more continuous way:\n\n-   [Paypal](https://www.paypal.me/notenoughtech)\n-   [Patreon](https://www.patreon.com/NotEnoughTECH)\n\nI hope you have enjoyed the project!\n\n "},{"id":"2282b3ea.59a6ac","type":"group","z":"993c2aad.77b1c8","name":"Voice assistant","style":{"stroke":"#6f2fa0","fill":"#bfc7d7","label":true,"color":"#000000"},"nodes":["7c336bf7.6ad694","2e8f01f6.1de86e","9287f640.98a458","97e299fa.db3de8","8a4bc11e.5a7a8","7172e90a.2d07d8","3d2c27.7358a3da"],"x":254,"y":379,"w":702,"h":142},{"id":"91a6b82.064e448","type":"group","z":"993c2aad.77b1c8","name":"GUI","style":{"stroke":"#addb7b","fill":"#e3f3d3","label":true},"nodes":["8ef13c2a.0fb3d","45083ce2.319054","6e5cdd29.84f134","ed4fd857.029678","c4064e44.9f7cc","e570ac40.57462","8a630468.3c3db8","e021bcfa.14a74","cf4c0aa4.bb26a8","b776f277.527f","432f4117.bde13","da769d07.76c22","912c1542.dbf488"],"x":994,"y":339,"w":818,"h":608},{"id":"8a630468.3c3db8","type":"group","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"Grafana","style":{"label":true,"stroke":"#3f5787","fill":"#bfdbef"},"nodes":["63608926.69aac8","5c6d2a0f.139b34","42f5b859.dbc1b8","8c537c68.eab78","d957ee14.01636","dc686a05.a37c58","14f54562.c486db","170c9fb8.60d37"],"x":1034,"y":759,"w":752,"h":162},{"id":"e021bcfa.14a74","type":"group","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"Cost and Usage stats","style":{"stroke":"#0070c0","fill":"#ffffbf","label":true},"nodes":["fa7d5173.8a095","f921df01.4646a","a123ac7f.407fd","22decc00.397474","bf7f6d5f.dfe35"],"x":1034,"y":599,"w":612,"h":142},{"id":"935cd51b.a45788","type":"group","z":"993c2aad.77b1c8","name":"Power Stats reset","style":{"label":true,"stroke":"#ff0000","fill":"#ffdf7f","label-position":"n","color":"#000000"},"nodes":["7bf2381d.c8d7e8","5df4be4f.14bed","82dd507f.33f42","6e61a964.6273d8","1d9afc9d.96eb43","a563736d.0dfa","736fa72.1801958","df25ca85.b3c388"],"x":254,"y":699,"w":672,"h":242},{"id":"d0f987dd.6afe88","type":"group","z":"993c2aad.77b1c8","name":"Grafana and InfluxDB","style":{"stroke":"#000000","fill":"#bfbfbf","label":true,"label-position":"n","color":"#ffffff"},"nodes":["9e8d95e3.19ee88","653c84f2.0ad9dc","3f57b056.0ebf3"],"x":1034,"y":159,"w":752,"h":82},{"id":"f5b41b90.e56e98","type":"group","z":"993c2aad.77b1c8","name":"Power calculations","style":{"stroke":"#ff0000","fill":"#bfc7d7","label":true,"label-position":"n","color":"#000000"},"nodes":["6842befa.f83ba","985e2fc8.f8e0d"],"x":254,"y":559,"w":692,"h":82},{"id":"ddfbded4.59178","type":"mqtt in","z":"993c2aad.77b1c8","name":"","topic":"zigbee2mqtt/Aqara_T1-LN","qos":"2","datatype":"json","broker":"6e117ba5.8026e4","nl":false,"rap":true,"rh":0,"x":370,"y":200,"wires":[["be203fac.0a74b","9efae3cd.72f83","55ed627c.1af14c","d2c4a2fe.2fbfb","fef61f17.f3c5a"]]},{"id":"be203fac.0a74b","type":"function","z":"993c2aad.77b1c8","name":"Setup","func":"var aqara =  flow.get(\"aqaraT1-LN\");\nvar current = msg.payload;\nvar load\n\nfunction round4(value){\n    return Number.parseFloat(value).toPrecision(5);\n}\n\n\n\naqara.linkquality = msg.payload.linkquality;\nif(aqara.isTempEnabled === true){\n    aqara.device_temperature = msg.payload.device_temperature;\n}\n\naqara.state = msg.payload.state;\n\n// fake power meter?\nif(aqara.power.enabled === true){\n    if(isNaN(msg.payload.power) === true){\n        msg.payload.power = 0;\n    }\n    load = msg.payload.power /6;\n    aqara.power.total = msg.payload.energy;\n    aqara.power.current = msg.payload.power;\n}\nelse{\n    var x = flow.get(\"AqaraT1load\");\n    load = x.current/(60 /x.frequency);\n    aqara.power.total = aqara.power.total;\n    aqara.power.current = x.current;\n}\n\n\nflow.set(\"aqaraT1-LN\", aqara);\nreturn msg;","outputs":1,"noerr":0,"initialize":"//Setup\n\n//setting up energy cost\n//uncommend to enable lines as some of the options here are not used by me from the same menu\n//price in currency per 1kWh\n//var cost = 0.001   \n//global.set(\"PowerCost, cost\")\n\n \n// For devices without a power meter please set the current value, leave freqency at 60 sec unless you have a reason to change it\n\nvar current = 9  // consumption in Watts\nvar load = {\"current\": current, \"frequency\" : 60};\nvar y = flow.get(\"AqaraT1load\");\nif (y === undefined){\n    flow.set(\"AqaraT1load\", load);    \n}  \n\n\n\n// For devices with the power meter enabled\nvar aqara = {   \"device_temperature\":null,\n                \"isTempEnabled\": true,  // set this to false if the temperature isnt supported\n                \"energy\":0,\n                \"linkquality\":null,\n                \"power\":{\n                    \"enabled\": true,     //set this to true for power meter and false for no power meter\n                    \"current\": 0,\n                    \"hour\": [],\n                    \"day\":[],\n                    \"yesterday\": [],\n                    \"week\": [],\n                    \"month\": [],\n                    \"year\" : [],\n                    \"total\": 0\n                },\n                \"state\":null\n            };\n            \nvar x = flow.get(\"aqaraT1-LN\");\nif (x === undefined){\n    flow.set(\"aqaraT1-LN\", aqara);  \n}  \n\n//publicly available url for the picture:\nvar url = \"https://www.powerplanetonline.com/cdnassets/xiaomi_aqara_rele_t1_con_neutro_01_l.jpg\";\nflow.set(\"pictureURL\", url);\n\n","finalize":"","libs":[],"x":630,"y":160,"wires":[[]]},{"id":"7bf2381d.c8d7e8","type":"inject","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"24 hour","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":800,"wires":[["a563736d.0dfa","736fa72.1801958","df25ca85.b3c388"]]},{"id":"5df4be4f.14bed","type":"inject","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"hour","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":350,"y":740,"wires":[["1d9afc9d.96eb43"]]},{"id":"82dd507f.33f42","type":"inject","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset Total","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":900,"wires":[["6e61a964.6273d8"]]},{"id":"6e61a964.6273d8","type":"function","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset all","func":"var aqara =  flow.get(\"aqaraT1-LN\");\n\naqara.power = {\"enabled\": true,\"current\":0,\"hour\":[],\"day\":[], \"yesterday\":[],\"week\":[],\"month\":[],\"year\":[],\"total\":0};\nflow.set(\"aqaraT1-LN\",aqara);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":780,"y":900,"wires":[[]]},{"id":"1d9afc9d.96eb43","type":"function","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset every hour","func":"var aqara =  flow.get(\"aqaraT1-LN\");\nvar freq = flow.get(\"AqaraT1load\");\n\nfunction add(accumulator, a) {\n            return accumulator + a}\n\n// add consumption per minute together\nvar x = aqara.power.hour;\nlet hourtotal = x.reduce(add)/freq.frequency;\n\n//pass hour use to a daily use in the first position\nvar y = aqara.power.day;\ny.unshift(hourtotal);\n\n//clean up - and update day value with new hour\naqara.power.hour = [];\naqara.power.day = y;\n\n\nflow.set(\"aqaraT1-LN\",aqara);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":740,"wires":[[]]},{"id":"a563736d.0dfa","type":"function","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset every 24h","func":"var aqara =  flow.get(\"aqaraT1-LN\");\n\nfunction add(accumulator, a) {\n            return accumulator + a}\n\naqara.power.yesterday = [];\n// add up all hourly values together\nvar x = aqara.power.day;\nlet daytotal = x.reduce(add);\n\n//and pass it as a full day \"daytotal\" to a weekly entry, remove 7th element to keep consecutive 7 day\nvar y = aqara.power.week;\ny.unshift(daytotal);\nif(y.length > 7){y.pop();}\n\n//add complete day as 1st element to monthly array, array resets on the 1st of evety month\nvar z = aqara.power.month;\nz.unshift(daytotal);\n\n//clean up  and update the data\naqara.power.day = [];\naqara.power.week = y;\naqara.power.month = z;\n\n//set completed day as yesterday\naqara.power.yesterday = z;\n\nflow.set(\"aqaraT1-LN\",aqara);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":780,"wires":[[]]},{"id":"736fa72.1801958","type":"function","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset every month","func":"var aqara =  flow.get(\"aqaraT1-LN\");\n\nfunction add(accumulator, a) {\n            return accumulator + a}\n\n//this check occurs every day at midnight so to make it one a month on the 1st:\nvar date = new Date();\nvar isItNewMonth = date.getDate();\n\nif(isItNewMonth ===1){\n    // add consumption of the month entries\n    var x = aqara.power.month;\n    let monthtotal = x.reduce(add);\n    \n    //pass the data to yearly use\n    var y = aqara.power.year;\n    y.unshift(monthtotal);\n    \n    \n    //clean up \n    aqara.power.month = [];\n    aqara.power.year = y;\n    \n    flow.set(\"aqaraT1-LN\",aqara);\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":810,"y":820,"wires":[[]]},{"id":"df25ca85.b3c388","type":"function","z":"993c2aad.77b1c8","g":"935cd51b.a45788","name":"Reset year","func":"var aqara =  flow.get(\"aqaraT1-LN\");\nvar currentyear = context.get(\"year\");\n\nfunction add(accumulator, a) {\n            return accumulator + a}\n\n//this check occurs every day at midnight so to make it one each Year:\nvar date = new Date();\nvar year = date.getFullYear();\n\nif(year > currentyear){\n    context.set(\"year\",year);\n    var x = aqara.power.year;\n    let yeartotal = x.reduce(add);\n    \n    //cleanup\n    aqara.power.year = [];\n    \n    flow.set(\"aqaraT1-LN\",aqara);\n    return msg;  \n    \n}\n\n","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\n\nvar date = new Date();\nvar year = date.getFullYear();\n\nvar y = context.get(\"year\");\n\nif (y === undefined){\n    context.set(\"year\", year);\n}  ","finalize":"","libs":[],"x":790,"y":860,"wires":[[]]},{"id":"8ef13c2a.0fb3d","type":"ui_text","z":"993c2aad.77b1c8","g":"91a6b82.064e448","group":"1a45cdd4.2db222","order":2,"width":4,"height":1,"name":"Aqara T1","label":"<b>Aqara T1 Switch</b>","format":"{{msg.payload}}","layout":"row-center","x":1300,"y":420,"wires":[]},{"id":"45083ce2.319054","type":"ui_template","z":"993c2aad.77b1c8","g":"91a6b82.064e448","group":"1a45cdd4.2db222","name":"Picture","order":1,"width":3,"height":3,"format":"<div>\n    \n    <img src=\"{{msg.payload}}\" alt=\"Aqara T1\" style=\"width:130px;height:130px;\">\n    \n</div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":1300,"y":380,"wires":[[]]},{"id":"6e5cdd29.84f134","type":"ui_button","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"Toggle Button","group":"1a45cdd4.2db222","order":7,"width":7,"height":1,"passthru":false,"label":"{{msg.label}}","tooltip":"Toggle Power","color":"","bgcolor":"{{msg.colour}}","icon":"","payload":"TOGGLE","payloadType":"str","topic":"topic","topicType":"msg","x":1160,"y":500,"wires":[["e570ac40.57462"]]},{"id":"fa7d5173.8a095","type":"ui_switch","z":"993c2aad.77b1c8","g":"e021bcfa.14a74","name":"","label":"kWh/GBP","tooltip":"","group":"1a45cdd4.2db222","order":10,"width":3,"height":1,"passthru":true,"decouple":"false","topic":"topic","topicType":"msg","style":"","onvalue":"true","onvalueType":"bool","onicon":"","oncolor":"","offvalue":"false","offvalueType":"bool","officon":"","offcolor":"","animate":false,"x":1140,"y":640,"wires":[["f921df01.4646a","a123ac7f.407fd"]]},{"id":"ed4fd857.029678","type":"ui_template","z":"993c2aad.77b1c8","g":"91a6b82.064e448","group":"1a45cdd4.2db222","name":"Load, Temp and Power brief","order":4,"width":4,"height":2,"format":"<div ng-bind-html=\"msg.payload.load\"></div>\n<div ng-bind-html=\"msg.payload.temp\"></div>\n<div ng-bind-html=\"msg.payload.total\"></div>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":1200,"y":540,"wires":[[]]},{"id":"c4064e44.9f7cc","type":"ui_template","z":"993c2aad.77b1c8","g":"91a6b82.064e448","group":"1a45cdd4.2db222","name":"Range Icon","order":3,"width":2,"height":2,"format":"<ng-md-icon icon={{msg.icon}} style={{msg.colour}} size=\"50\"></ng-md-icon>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":1150,"y":460,"wires":[[]]},{"id":"42f5b859.dbc1b8","type":"ui_button","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Day","group":"1a45cdd4.2db222","order":13,"width":3,"height":1,"passthru":false,"label":"Day","tooltip":"Toggle Power","color":"","bgcolor":"","icon":"","payload":"day","payloadType":"str","topic":"topic","topicType":"msg","x":1110,"y":800,"wires":[["8c537c68.eab78"]]},{"id":"5c6d2a0f.139b34","type":"ui_button","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Week","group":"1a45cdd4.2db222","order":14,"width":3,"height":1,"passthru":false,"label":"Week","tooltip":"Toggle Power","color":"","bgcolor":"","icon":"","payload":"week","payloadType":"str","topic":"topic","topicType":"msg","x":1110,"y":840,"wires":[["d957ee14.01636"]]},{"id":"63608926.69aac8","type":"ui_button","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Month","group":"1a45cdd4.2db222","order":15,"width":3,"height":1,"passthru":false,"label":"Month","tooltip":"Toggle Power","color":"","bgcolor":"","icon":"","payload":"month","payloadType":"str","topic":"topic","topicType":"msg","x":1110,"y":880,"wires":[["dc686a05.a37c58"]]},{"id":"9efae3cd.72f83","type":"function","z":"993c2aad.77b1c8","name":"Range Format","func":"var range = msg.payload.linkquality;\n\n\n\n//strong  130+\nif(range >= 130){\n    msg.icon = \"signal_wifi_3_bar\";\n    msg.colour = \"fill: green\";\n}\n//good 90-129\nif(range >= 90 && range < 130){\n    msg.icon = \"signal_wifi_2_bar\";\n    msg.colour = \"fill: blue\";\n}\n//medium  50-89\nif(range >= 50 && range < 90){\n    msg.icon = \"signal_wifi_1_bar\";\n    msg.colour = \"fill: orange\";\n}\n//low  below <49\nif(range < 50){\n    msg.icon = \"signal_wifi_0_bar\";\n    msg.colour = \"fill: red\";\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":240,"wires":[["17fba4cd.34274b"]]},{"id":"55ed627c.1af14c","type":"function","z":"993c2aad.77b1c8","name":"Button Format","func":"var aqara  = flow.get(\"aqaraT1-LN\");\nvar button = aqara.state;\n\n//update button\nif(button === \"ON\"){\n    msg.label = \"TURN OFF\";\n    msg.colour = \"green\";\n}\nif(button === \"OFF\"){\n    msg.label = \"TURN ON\";\n    msg.colour = \"red\";\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":660,"y":280,"wires":[["231f6462.63b21c"]]},{"id":"f921df01.4646a","type":"change","z":"993c2aad.77b1c8","g":"e021bcfa.14a74","name":"Set Chart display mode","rules":[{"t":"set","p":"inCurrency","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1370,"y":640,"wires":[[]]},{"id":"e570ac40.57462","type":"mqtt out","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","topic":"zigbee2mqtt/Aqara_T1-LN/set","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"6e117ba5.8026e4","x":1550,"y":500,"wires":[]},{"id":"d2c4a2fe.2fbfb","type":"function","z":"993c2aad.77b1c8","name":"Status format","func":"var aqara = flow.get(\"aqaraT1-LN\");\nvar aqaraLoad = flow.get(\"AqaraT1load\");\n\nif(aqara.power.enabled === true){\n    if(msg.payload.energy === undefined){msg.payload.energy = 0;}\n    if(aqara.isTempEnabled === false){msg.payload.device_temperature = \"n/a\";}\n    \n    msg.payload =  {\"load\" : \"Load: \"+ msg.payload.power + \"W\",\n                    \"temp\": \"Temperature: \" + msg.payload.device_temperature +\"℃\",\n                    \"total\": \"Total energy: \" + msg.payload.energy + \"kWh\"\n    };\n    \n}\nelse{\n    if(msg.payload.state === \"OFF\"){aqaraLoad. current = 0;}\n    if(aqara.isTempEnabled === false){msg.payload.device_temperature = \"n/a\";}\n    msg.payload =  {\"load\" : \"Load : \" + aqaraLoad.current + \"W\" ,\n                    \"temp\": \"Temperature: \" + msg.payload.device_temperature +\"℃\",\n                    \"total\": \"Total energy: \" + aqara.power.total + \"kWh\"};\n}\n    \nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":650,"y":320,"wires":[["33922a2a.226686"]]},{"id":"9e8d95e3.19ee88","type":"function","z":"993c2aad.77b1c8","g":"d0f987dd.6afe88","name":"Save to InfluxDB","func":"var aqara = flow.get(\"aqaraT1-LN\");\nvar state = msg.payload.state;\nvar x = 0;\n\nif(state === \"ON\"){\n    x = 1;\n}\n\nif(aqara.isTempEnabled === false){msg.payload.device_temperature = 0;}\n\nmsg.measurement  = \"AqaraT1\";\nmsg.payload = [{\n            temperature: msg.payload.device_temperature,\n            power: msg.payload.power,\n            state: x\n},\n            \n    {location: \"office\"}\n];\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":200,"wires":[["653c84f2.0ad9dc"]]},{"id":"653c84f2.0ad9dc","type":"influxdb out","z":"993c2aad.77b1c8","g":"d0f987dd.6afe88","influxdb":"6a97c1ce.7814a","name":"","measurement":"","precision":"s","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1640,"y":200,"wires":[]},{"id":"170c9fb8.60d37","type":"ui_template","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","group":"1a45cdd4.2db222","name":"Display Grafana","order":12,"width":9,"height":5,"format":"<iframe id=\"power\" width=\"475px\" height=\"300px\" align=\"center\"></iframe>        \n\t\t<script>\n\t\t(function(scope) {            \n\t\t\tscope.$watch('msg', function(msg) {                \n\t\t\t\tvar x = document.getElementById('power');\n\t\t\t\tx.setAttribute('src', msg.payload); \n\t\t\t\t});\n\t\t})(scope);\n\t\t</script>\n\n\n\n\n\n","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":1680,"y":840,"wires":[[]]},{"id":"8c537c68.eab78","type":"function","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Today","func":"msg.fromdate = \"now/d\";\nmsg.enddate = \"now\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":800,"wires":[["14f54562.c486db"]]},{"id":"14f54562.c486db","type":"function","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Compose URL - ADD Grafana info","func":"var rooms = flow.get(\"rooms\");\n\n//Grafana embeded panel info\nvar url = \"http://home.lan:3000/d-solo/5iSeixgRz/dash?orgId=1\";\nvar panelID = 2;\n\n\nmsg.payload = url + \"&refresh=1m&theme=light&from=\"+ msg.fromdate + \"&to=\"+ msg.enddate + \"&panelId=\"+ panelID;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1440,"y":840,"wires":[["170c9fb8.60d37"]]},{"id":"d957ee14.01636","type":"function","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Week","func":"var p_30d  = 1000*60*60*24*30 ; //30 Days\nvar p_7d  = 1000*60*60*24*7 ; //7 Days\nvar p_1d   =  1000*60*60*24 ; // 1 Day\nvar p_hr = 1000*60*60; //1 Hour\n\nvar d = new Date();\nvar current = d.getTime();\nvar today0h = d.setHours(0,0,0,0); //Timestamp midnight\n\nmsg.fromdate = today0h - p_7d\nmsg.enddate = today0h\nreturn msg\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":840,"wires":[["14f54562.c486db"]]},{"id":"dc686a05.a37c58","type":"function","z":"993c2aad.77b1c8","g":"8a630468.3c3db8","name":"Month","func":"var p_30d  = 1000*60*60*24*30 ; //30 Days\nvar p_7d  = 1000*60*60*24*7 ; //7 Days\nvar p_1d   =  1000*60*60*24 ; // 1 Day\nvar p_hr = 1000*60*60; //1 Hour\n\nvar d = new Date();\nvar current = d.getTime();\nvar today0h = d.setHours(0,0,0,0); //Timestamp midnight\n\nmsg.fromdate = today0h - p_30d\nmsg.enddate = today0h\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":880,"wires":[["14f54562.c486db"]]},{"id":"a123ac7f.407fd","type":"function","z":"993c2aad.77b1c8","g":"e021bcfa.14a74","name":"Process Power info","func":"var value = flow.get(\"inCurrency\");\nvar aqara = flow.get(\"aqaraT1-LN\");\nvar price = global.get(\"PowerCost\");\n\nfunction add(accumulator, a) {\n            return accumulator + a}\n\n//hour\nvar hour = aqara.power.hour;\n\n\n//day calculations, saved hourly\nvar day = aqara.power.day;\nif(day[0] > 0){\nvar dayWatt = (day.reduce(add) + hour.reduce(add))/1000;\nvar dayCost = (day.reduce(add) + hour.reduce(add))/1000 * price;\n}\n\n//yesterday\nvar yesterday = aqara.power.yesterday;\nif(yesterday[0] > 0){\n    var yesterdayWatt = yesterday.reduce(add)/1000;\n    var yesterdayCost = yesterday.reduce(add)/1000 * price;\n}\n\n//week\nvar week = aqara.power.week;\nif(week[0] > 0){\n    var weekWatt = (week.reduce(add) + day.reduce(add))/1000;\n    var weekCost = (week.reduce(add) + day.reduce(add))/1000 * price;\n}\n//month\nvar month = aqara.power.month;\nif(month[0] > 0){\n    var monthWatt = (month.reduce(add) + day.reduce(add))/1000;\n    var monthCost = (month.reduce(add) + day.reduce(add))/1000 * price;\n}\n//year\nvar year = aqara.power.year;\nif(year[0] > 0){\n    var yearWatt = (year.reduce(add) + month.reduce(add) + day.reduce(add))/1000;\n    var yearCost = (year.reduce(add) + month.reduce(add) + day.reduce(add))/1000 * price;\n}\n\n//total\n\nvar total = aqara.power.total;\nif(total > 0){\n    var totalWatt = total;\n    var totalCost = total * price;\n\n}\n\n\n\n//calculate cost\nif(value === true){\n    if(dayCost === undefined){dayCost = 0}\n    if(yesterdayCost === undefined){yesterdayCost = 0}\n    if(weekCost === undefined){weekCost = 0}\n    if(monthCost === undefined){monthCost = 0}\n    if(yearCost === undefined){yearCost = 0}\n    if(totalCost === undefined){totalCost = 0}\n    \n    msg.payload =  {\"day\" : \"Today: £\"+ dayCost.toFixed(2),\n                    \"yesterday\" : \"Yesterday: £\"+ yesterdayCost.toFixed(2),\n                    \"week\":  \"Last 7 days: £\"+ weekCost.toFixed(2),\n                    \"month\": \"Month: £\"+ monthCost.toFixed(2),\n                    \"year\" : \"Year: £\"+ yearCost.toFixed(2),\n                    \"total\" : \"Total: £\"+ totalCost.toFixed(2)};\n}\n//calculate wattage\nelse{\n    if(dayWatt === undefined){dayWatt = 0}\n    if(yesterdayWatt === undefined){yesterdayWatt = 0}\n    if(weekWatt === undefined){weekWatt = 0}\n    if(monthWatt === undefined){monthWatt = 0}\n    if(yearWatt === undefined){yearWatt = 0}\n    if(totalWatt === undefined){totalWatt = 0}\n    \n    msg.payload =  {\"day\" :  \"Today: \"+ dayWatt.toFixed(2) + \" kWh\",\n                    \"yesterday\" : \"Yesterday: \"+ yesterdayWatt.toFixed(2) +\"kWh\",\n                    \"week\":  \"Last 7 days: \"+ weekWatt.toFixed(2) + \" kWh\",\n                    \"month\":  \"Month: \"+ monthWatt.toFixed(2) + \" kWh\",\n                    \"year\" :  \"Year: \"+ yearWatt.toFixed(2) + \" kWh\",\n                    \"total\" : \"Total: \"+ totalWatt.toFixed(2)+ \" kWh\"};\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1350,"y":700,"wires":[["22decc00.397474"]]},{"id":"22decc00.397474","type":"ui_template","z":"993c2aad.77b1c8","g":"e021bcfa.14a74","group":"1a45cdd4.2db222","name":"Cost and Usage","order":11,"width":9,"height":2,"format":"<table class=\"stats\">\n<tbody>\n<tr>\n<td>\n    <div ng-bind-html=\"msg.payload.day\"></div>\n    <div ng-bind-html=\"msg.payload.yesterday\"></div>\n    <div ng-bind-html=\"msg.payload.week\"></div>\n    \n</td>\n<td>\n    <div ng-bind-html=\"msg.payload.month\"></div>\n    <div ng-bind-html=\"msg.payload.year\"></div>\n    <div ng-bind-html=\"msg.payload.total\"></div>\n</td>\n</tr>\n</tbody>\n</table>\n\n<style>\ntable.stats {\n  width: 100%;\n  text-align: center;\n}\ntable.stats td, table.stats th {\n  padding: 3px 2px;\n}\ntable.stats tbody td {\n  font-size: 16px;\n  font-weight: bold;\n}\ntable.stats tfoot td {\n  font-size: 14px;\n}\ntable.stats tfoot .links {\n  text-align: right;\n}\ntable.stats tfoot .links a{\n  display: inline-block;\n  background: #1C6EA4;\n  color: #FFFFFF;\n  padding: 2px 8px;\n  border-radius: 5px;\n}\n</style>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":1540,"y":700,"wires":[[]]},{"id":"bf7f6d5f.dfe35","type":"inject","z":"993c2aad.77b1c8","g":"e021bcfa.14a74","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1140,"y":700,"wires":[["a123ac7f.407fd"]]},{"id":"6842befa.f83ba","type":"inject","z":"993c2aad.77b1c8","g":"f5b41b90.e56e98","name":"Every Minute","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"60","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":380,"y":600,"wires":[["985e2fc8.f8e0d"]]},{"id":"985e2fc8.f8e0d","type":"function","z":"993c2aad.77b1c8","g":"f5b41b90.e56e98","name":"Calculate","func":"var aqara = flow.get(\"aqaraT1-LN\");\nvar load = aqara.power.current;\n\nvar x = aqara.power.hour;\n\nif(aqara.state === \"ON\"){\n   x.unshift(load);\n}\nelse{\n    x.unshift(0);\n}\n\naqara.power.hour = x;\n\nflow.set(\"aqaraT1-LN\", aqara)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":860,"y":600,"wires":[[]]},{"id":"649a616a.0106","type":"comment","z":"993c2aad.77b1c8","name":"Setup node - check \"On Start\"","info":"","x":700,"y":120,"wires":[]},{"id":"17fba4cd.34274b","type":"link out","z":"993c2aad.77b1c8","name":"","links":["cf4c0aa4.bb26a8"],"x":935,"y":240,"wires":[]},{"id":"231f6462.63b21c","type":"link out","z":"993c2aad.77b1c8","name":"","links":["b776f277.527f"],"x":935,"y":280,"wires":[]},{"id":"33922a2a.226686","type":"link out","z":"993c2aad.77b1c8","name":"","links":["432f4117.bde13"],"x":935,"y":320,"wires":[]},{"id":"cf4c0aa4.bb26a8","type":"link in","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","links":["17fba4cd.34274b"],"x":1035,"y":460,"wires":[["c4064e44.9f7cc"]]},{"id":"b776f277.527f","type":"link in","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","links":["231f6462.63b21c"],"x":1035,"y":500,"wires":[["6e5cdd29.84f134"]]},{"id":"432f4117.bde13","type":"link in","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","links":["33922a2a.226686"],"x":1035,"y":540,"wires":[["ed4fd857.029678"]]},{"id":"7c336bf7.6ad694","type":"alexa-home","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","conf":"241b578d.91d228","device":"120046","acknoledge":true,"name":"Entry Lamp","topic":"","x":350,"y":480,"wires":[["9287f640.98a458"]]},{"id":"2e8f01f6.1de86e","type":"noraf-light","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","devicename":"Light","lightcolor":false,"brightnesscontrol":false,"commandonlycolor":false,"turnonwhenbrightnesschanges":false,"passthru":false,"statepayload":true,"brightnessoverride":"","roomhint":"Office","name":"","colortype":"hsv","nora":"9022313b.c75f1","topic":"","onvalue":"ON","onvalueType":"str","offvalue":"OFF","offvalueType":"str","temperaturemin":"2700","temperaturemax":"5500","twofactor":"off","twofactorpin":"","x":590,"y":420,"wires":[["97e299fa.db3de8"]]},{"id":"9287f640.98a458","type":"function","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","name":"Format response","func":"var command = msg.payload;\n\nif(command === true){msg.payload = \"ON\";}\nif(command === false){msg.payload = \"OFF\";}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":770,"y":480,"wires":[["8a4bc11e.5a7a8"]]},{"id":"da769d07.76c22","type":"link in","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","links":["8a4bc11e.5a7a8","97e299fa.db3de8"],"x":1255,"y":460,"wires":[["e570ac40.57462"]]},{"id":"97e299fa.db3de8","type":"link out","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","name":"","links":["da769d07.76c22"],"x":915,"y":420,"wires":[]},{"id":"8a4bc11e.5a7a8","type":"link out","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","name":"","links":["da769d07.76c22"],"x":915,"y":480,"wires":[]},{"id":"7172e90a.2d07d8","type":"function","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","name":"Format update","func":"var command = msg.payload.state\n\nif(command === \"ON\"){msg.payload = true;}\nif(command === \"OFF\"){msg.payload = false;}\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":420,"y":420,"wires":[["2e8f01f6.1de86e"]]},{"id":"fef61f17.f3c5a","type":"link out","z":"993c2aad.77b1c8","name":"","links":["3d2c27.7358a3da","3f57b056.0ebf3"],"x":935,"y":200,"wires":[]},{"id":"3d2c27.7358a3da","type":"link in","z":"993c2aad.77b1c8","g":"2282b3ea.59a6ac","name":"","links":["fef61f17.f3c5a"],"x":295,"y":420,"wires":[["7172e90a.2d07d8"]]},{"id":"3f57b056.0ebf3","type":"link in","z":"993c2aad.77b1c8","g":"d0f987dd.6afe88","name":"","links":["fef61f17.f3c5a"],"x":1075,"y":200,"wires":[["9e8d95e3.19ee88"]]},{"id":"912c1542.dbf488","type":"inject","z":"993c2aad.77b1c8","g":"91a6b82.064e448","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"pictureURL","payloadType":"flow","x":1140,"y":380,"wires":[["45083ce2.319054"]]},{"id":"6e117ba5.8026e4","type":"mqtt-broker","name":"MQTT","broker":"home.lan","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"1a45cdd4.2db222","type":"ui_group","name":"Aqara Devices","tab":"93952cd4.53347","order":1,"disp":true,"width":"9","collapse":false},{"id":"6a97c1ce.7814a","type":"influxdb","hostname":"127.0.0.1","port":"8086","protocol":"http","database":"homeautomation","name":"Home Automation DB","usetls":false,"tls":"","influxdbVersion":"1.x","url":"http://localhost:8086","rejectUnauthorized":true},{"id":"241b578d.91d228","type":"alexa-home-conf","username":"quintaar"},{"id":"9022313b.c75f1","type":"noraf-config","name":"Google Home","group":"","twofactor":"off","twofactorpin":"","localexecution":true,"structure":"Home"},{"id":"93952cd4.53347","type":"ui_tab","name":"Aqara","icon":" indeterminate_check_box","disabled":false,"hidden":false}]

Flow Info

Created 6 days ago
Rating: 5 1

Node Types

Core
  • change (x1)
  • comment (x1)
  • function (x18)
  • inject (x6)
  • link in (x6)
  • link out (x6)
  • mqtt in (x1)
  • mqtt out (x1)
  • mqtt-broker (x1)
  • tab (x1)
Other

Tags

  • aqara
  • relay
  • power
  • meter
  • dashboard
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option