I like using function nodes and built in nodes as it reduces your dependency on 3rd party nodes which may stop being supported.
This is a modification of a timer I built. It uses a flow variable flow.chargeTime
(how long you want to charge the toothbrush for) and an inject node that will start the timer every 72 hours. The stop node can be removed for your purposes, it is used to stop the timer partway through and sends 'chargeDisable'
. The function sends a message 'chargeEnable'
when started, and 'chargeDisable'
when stopped.
The 'Start every 72 hours'
inject node could be modified to send a payload every day, and the function modified to increment a count when it receives a message. When this value reaches 3, it would start the timer and reset, otherwise it would do nothing. This would allow you to have better control of when the charger is turned on and off - at the moment the beginning of the 3 day cycle will be dependent on when you deploy the flow.
[
{
"id": "72140aba.c30d64",
"type": "inject",
"z": "59fcda25.edd454",
"name": "Start every 72 hours",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "00 22 * * *",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "true",
"payloadType": "bool",
"x": 520,
"y": 120,
"wires": [
[
"6e6703ab.87e13c"
]
]
},
{
"id": "30b0c638.4abdfa",
"type": "inject",
"z": "59fcda25.edd454",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "stop",
"payloadType": "str",
"x": 470,
"y": 180,
"wires": [
[
"6e6703ab.87e13c"
]
]
},
{
"id": "ff53b61c.471938",
"type": "inject",
"z": "59fcda25.edd454",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "5",
"payloadType": "num",
"x": 470,
"y": 60,
"wires": [
[
"4ec48c94.eaf824"
]
]
},
{
"id": "4ec48c94.eaf824",
"type": "change",
"z": "59fcda25.edd454",
"name": "chargeTime (s)",
"rules": [
{
"t": "set",
"p": "chargeTime",
"pt": "flow",
"to": "payload",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 660,
"y": 60,
"wires": [
[]
]
},
{
"id": "6e6703ab.87e13c",
"type": "function",
"z": "59fcda25.edd454",
"name": "",
"func": "if(msg.payload !== true && msg.payload !== 'stop') {\n return;\n}\n\nvar timerEnable;\n\nfunction timer() {\n context.set(\"timerValue\", (context.get(\"timerValue\") - 1));\n node.status({fill:\"yellow\",shape:\"dot\",text:\"Charge Time Remaining: \" +context.get(\"timerValue\")}); \n if (context.get(\"timerValue\") === 0) {\n msg.payload = \"chargeDisable\"\n node.status({fill:\"green\",shape:\"dot\",text:\"Charging finished\"}); \n node.send(msg);\n clearInterval(context.timer);\n context.set(\"timerRunning\", false)\n return;\n }\n}\n\nif (msg.payload === \"stop\") {\n timerEnable = false;\n}\nelse {\n timerEnable = true;\n}\n\nif (context.get(\"timerRunning\") === false && timerEnable === true) { //Start\n context.set(\"timerValue\", flow.get(\"timeout\"));\n node.status({fill:\"blue\",shape:\"dot\",text:\"Charge Time Remaining: \" + context.get(\"timerValue\")});\n context.timer = setInterval(timer,1000);\n context.set(\"timerRunning\", true);\n \n msg.payload = 'chargeEnable';\n node.send(msg);\n}\n\nelse if (context.get(\"timerRunning\") === true && timerEnable === true) { //Reset\n context.set(\"timerValue\", flow.get(\"timeout\"));\n //msg.payload = 'timer reset';\n node.status({fill:\"blue\",shape:\"ring\",text:\"Charge Time Remaining: \" + context.get(\"timerValue\")});\n context.set(\"timerRunning\", true);\n}\n\nelse if (timerEnable === false) { //Stop\n //msg.payload = 'timer stop';\n clearInterval(context.timer);\n node.status({fill:\"red\",shape:\"ring\",text:\"Stopped\"});\n context.set(\"timerRunning\", false);\n msg.payload = 'chargeDisable';\n node.send(msg);\n}\n\nreturn;",
"outputs": 1,
"noerr": 0,
"initialize": "context.set(\"timerRunning\", false);",
"finalize": "",
"x": 640,
"y": 180,
"wires": [
[
"3e2c38c1.05ad18"
]
]
},
{
"id": "3e2c38c1.05ad18",
"type": "debug",
"z": "59fcda25.edd454",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 810,
"y": 180,
"wires": []
}
]