I’d like to sum the value of a sensor, in this case power usage while another value is true in this case, a person is home. I can’t figure out how I’d go about doing this. Any help would be greatly appreciated.
I am doing something similar in node red for my daily water usage. Save the daily and monthly values in a flow/global variable. The second flow resets the count each month.
[{"id":"e40f7f3f900d8a59","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"9d07b7eb473d9da8","type":"api-call-service","z":"e40f7f3f900d8a59","name":"","server":"","version":5,"debugenabled":false,"domain":"notify","service":"mobile_app_iphone_2","areaId":[],"deviceId":[],"entityId":[],"data":"{\"message\":\"We used {{payload.0}} gallons of water yesterday and {{payload.1}} gallons this month.\",\"title\":\"Daily Water Usage\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1300,"y":240,"wires":[[]]},{"id":"b61e027d9a9cea4c","type":"delay","z":"e40f7f3f900d8a59","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":1058,"y":237,"wires":[["9d07b7eb473d9da8"]]},{"id":"fe225b6f2dfe8fde","type":"change","z":"e40f7f3f900d8a59","name":"sum daily/mon values","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum([$$.payload , $flowContext(\"MonthGal\")])","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":240,"wires":[["3ad75db7d5b830cd"]]},{"id":"4fb8524461518a95","type":"function","z":"e40f7f3f900d8a59","name":"daily gallons variable","func":"msg.payload = Number(msg.payload);\n\nflow.set('DailyGallons',msg.payload);\nvar x = flow.get('DailyGallons');\nmsg.payload = x;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":160,"wires":[["224019d6323bbc56"]]},{"id":"224019d6323bbc56","type":"switch","z":"e40f7f3f900d8a59","name":"flow daily gal","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"DailyGallons","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":690,"y":160,"wires":[["fe225b6f2dfe8fde","3322badf9f1f64e3"]]},{"id":"3ad75db7d5b830cd","type":"change","z":"e40f7f3f900d8a59","name":"Month total","rules":[{"t":"set","p":"MonthGal","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":240,"wires":[["bf16e30ad2c1cc95"]]},{"id":"bf16e30ad2c1cc95","type":"join","z":"e40f7f3f900d8a59","name":"daily/monthly combined","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"payload","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":848,"y":237,"wires":[["b61e027d9a9cea4c"]]},{"id":"3322badf9f1f64e3","type":"change","z":"e40f7f3f900d8a59","name":"","rules":[{"t":"set","p":"DailyGallons","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":160,"wires":[["bf16e30ad2c1cc95"]]},{"id":"d3c477ed43799509","type":"change","z":"e40f7f3f900d8a59","name":"Reset Monthly Gal","rules":[{"t":"set","p":"MonthGal","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1228,"y":437,"wires":[["aca65525e71c03be"]]},{"id":"73cdcf1841a8a9dc","type":"inject","z":"e40f7f3f900d8a59","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 01 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":408,"y":457,"wires":[["59151418f9ee43ef"]]},{"id":"59151418f9ee43ef","type":"function","z":"e40f7f3f900d8a59","name":"Time Events","func":"var stamp = msg.payload;\n\nfunction pretty(num) {\n if (num < 10) {\n return \"0\" + num;\n }\n return \"\" + num;\n}\nvar updated = false;\nvar o_second = null;\nvar o_minute = null;\nvar o_hour = null;\nvar o_date = null;\nvar o_month = null;\nvar o_year = null;\nvar timeupdate = new Date(stamp);\n\nvar currenttime = global.get(\"currenttime\") || {second:0,minute:0,hour:0,date:0,month:0,year:0};\n\nif (currenttime.second !== timeupdate.getSeconds()) {\n currenttime.second = timeupdate.getSeconds();\n updated = true;\n o_second = {\n topic: \"TimeEventSecond\",\n payload: currenttime.second,\n timestamp: stamp\n };\n}\nif (currenttime.minute !== timeupdate.getMinutes()) {\n currenttime.minute = timeupdate.getMinutes();\n o_minute = {\n topic: \"TimeEventMinute\",\n payload: currenttime.minute,\n timestamp: stamp\n };\n}\nif (currenttime.hour !== timeupdate.getHours()) {\n currenttime.hour = timeupdate.getHours();\n o_hour = {\n topic: \"TimeEventHour\",\n payload: currenttime.hour,\n timestamp: stamp\n };\n}\nif (currenttime.date !== timeupdate.getDate()) {\n currenttime.date = timeupdate.getDate();\n o_date = {\n topic: \"TimeEventDate\",\n payload: currenttime.date,\n timestamp: stamp\n };\n}\nif (currenttime.month !== timeupdate.getMonth() + 1) {\n currenttime.month = timeupdate.getMonth() + 1;\n o_month = {\n topic: \"TimeEventMonth\",\n payload: currenttime.month,\n timestamp: stamp\n };\n}\nif (currenttime.year !== timeupdate.getFullYear()) {\n currenttime.year = timeupdate.getFullYear();\n o_year = {\n topic: \"TimeEventYear\",\n payload: currenttime.year,\n timestamp: stamp\n };\n}\nif (updated) {\n currenttime.timestamp = stamp;\n // can hold prettyfied date-time values in currenttime if needed \n /*\n var displaytime = pretty(currenttime.hour) + \":\" + pretty(currenttime.minute) + \":\" + pretty(currenttime.second);\n currenttime.displaytime = displaytime;\n currenttime.displaydate = pretty(currenttime.date) + \".\" + pretty(currenttime.month);\n */\n global.set(\"currenttime\", currenttime);\n \n \n return [o_second, o_minute, o_hour, o_date, o_month, o_year];\n \n}","outputs":6,"noerr":0,"initialize":"","finalize":"","libs":[],"x":608,"y":457,"wires":[["3348247ae4e2927f"],["214c7c046cd2acdc"],["b5bcc610c6afc48e"],["5756ef2d523cbe3c"],["700dfb94e1efc65a"],["22dcfddcca089b6a"]]},{"id":"3348247ae4e2927f","type":"link out","z":"e40f7f3f900d8a59","name":"te-second","mode":"link","links":[],"x":738.9999847412109,"y":359.00000381469727,"wires":[]},{"id":"214c7c046cd2acdc","type":"link out","z":"e40f7f3f900d8a59","name":"te-minute","mode":"link","links":["23a5decbca4e54f6","6f8b1f68edb4c414"],"x":738.9999847412109,"y":399.00000381469727,"wires":[]},{"id":"b5bcc610c6afc48e","type":"link out","z":"e40f7f3f900d8a59","name":"te-hour","mode":"link","links":[],"x":738.9999847412109,"y":439.00000381469727,"wires":[]},{"id":"5756ef2d523cbe3c","type":"link out","z":"e40f7f3f900d8a59","name":"te-date","mode":"link","links":[],"x":738.9999847412109,"y":479.00000381469727,"wires":[]},{"id":"700dfb94e1efc65a","type":"link out","z":"e40f7f3f900d8a59","name":"te-month","mode":"link","links":["7b781deefa15bcd5"],"x":738.9999847412109,"y":519.0000038146973,"wires":[]},{"id":"22dcfddcca089b6a","type":"link out","z":"e40f7f3f900d8a59","name":"te-year","mode":"link","links":[],"x":738.9999847412109,"y":559.0000038146973,"wires":[]},{"id":"7b781deefa15bcd5","type":"link in","z":"e40f7f3f900d8a59","name":"listen minute","links":["700dfb94e1efc65a"],"x":827,"y":440,"wires":[["b8ca08fb51848a8f"]]},{"id":"aca65525e71c03be","type":"debug","z":"e40f7f3f900d8a59","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1400,"y":440,"wires":[]},{"id":"b8ca08fb51848a8f","type":"switch","z":"e40f7f3f900d8a59","name":"2nd day of each month","property":"currenttime.date","propertyType":"global","rules":[{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":1008,"y":437,"wires":[["d3c477ed43799509"]]},{"id":"be8defcff5034c5d","type":"inject","z":"e40f7f3f900d8a59","name":"daily sensor value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":210,"y":160,"wires":[["4fb8524461518a95"]]},{"id":"be7ccab0510604e4","type":"inject","z":"e40f7f3f900d8a59","name":"true or false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"str","x":110,"y":60,"wires":[["a36c01f551c18612"]]},{"id":"e8422b1ab866418f","type":"function","z":"e40f7f3f900d8a59","name":"true false variable","func":"flow.set('TrueFalse',msg.payload);\nvar x = flow.get('TrueFalse');\nmsg.payload = x;\n\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":60,"wires":[["4fb8524461518a95"]]},{"id":"a36c01f551c18612","type":"switch","z":"e40f7f3f900d8a59","name":"If True","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":270,"y":60,"wires":[["e8422b1ab866418f"]]}]