Sleep Timer

I'm using this flow to turn of my TV and hifi equipment after some minutes. Simple sleep timer.

first output: sends "off" as payload after given amount of minutes second output: time left in minutes

[
    {
        "id": "76faf37d.b2cc6c",
        "type": "function",
        "z": "8df3d247.bbc66",
        "name": "Sleep Timer",
        "func": "\nvar value  = context.get('value') || 60000;\n\nif (msg.topic == \"value\")\n{\n  value = msg.payload * 60000;\n  context.set('value',value);  \n}\n\nelse if (msg.topic == \"start\")\n{\n\nvar timer = setTimeout(function(){msg.payload = \"off\";node.send([msg,{ payload: \"Ende\" }]);var countdownstop  = context.get('countdown');clearInterval(countdownstop)},value);\ncontext.set('timer',timer); \n\nvar count = value/60000;\n//count = count -1 ;\nmsg.payload=count;\nnode.send([null,msg]);\nvar countdown = setInterval(function() {count=count-1;msg.payload=count; node.send([null,msg]) }, 60000);\ncontext.set('countdown',countdown);\n\n}\n\nelse if (msg.topic == \"stop\")\n{\nvar timerstop  = context.get('timer')\nclearTimeout(timerstop);\n\nvar countdownstop  = context.get('countdown')\nclearInterval(countdownstop);\n\n}\n\n\n",
        "outputs": "2",
        "noerr": 0,
        "x": 1270,
        "y": 1060,
        "wires": [
            [
                "7f025f32.b44ac"
            ],
            [
                "a2ec4700.673f38"
            ]
        ]
    },
    {
        "id": "9b2e787.97d9388",
        "type": "ui_button",
        "z": "8df3d247.bbc66",
        "name": "",
        "group": "4e53f268.aaddbc",
        "order": 3,
        "width": "3",
        "height": "1",
        "label": "Start",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "start",
        "x": 1030,
        "y": 1040,
        "wires": [
            [
                "76faf37d.b2cc6c"
            ]
        ]
    },
    {
        "id": "a8bcf5bc.3524f8",
        "type": "ui_numeric",
        "z": "8df3d247.bbc66",
        "name": "",
        "label": "Sleep Timer in Minuten",
        "group": "4e53f268.aaddbc",
        "order": 2,
        "width": "0",
        "height": "0",
        "passthru": true,
        "topic": "value",
        "format": "{{value}}",
        "min": "1",
        "max": "300",
        "step": 1,
        "x": 1070,
        "y": 1120,
        "wires": [
            [
                "76faf37d.b2cc6c"
            ]
        ]
    },
    {
        "id": "ccb1395f.76f848",
        "type": "ui_button",
        "z": "8df3d247.bbc66",
        "name": "",
        "group": "4e53f268.aaddbc",
        "order": 4,
        "width": "3",
        "height": "1",
        "label": "Stop",
        "color": "",
        "bgcolor": "",
        "icon": "",
        "payload": "",
        "payloadType": "str",
        "topic": "stop",
        "x": 1030,
        "y": 1080,
        "wires": [
            [
                "76faf37d.b2cc6c"
            ]
        ]
    },
    {
        "id": "a2ec4700.673f38",
        "type": "ui_text",
        "z": "8df3d247.bbc66",
        "group": "4e53f268.aaddbc",
        "order": 5,
        "width": 0,
        "height": 0,
        "name": "",
        "label": "Restzeit",
        "format": "{{msg.payload}} Minuten",
        "layout": "row-spread",
        "x": 1440,
        "y": 1080,
        "wires": []
    },
    {
        "id": "7f025f32.b44ac",
        "type": "debug",
        "z": "8df3d247.bbc66",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 1450,
        "y": 1020,
        "wires": []
    },
    {
        "id": "4e53f268.aaddbc",
        "type": "ui_group",
        "z": "",
        "name": "Steckdosen",
        "tab": "95d17d8a.8e1d8",
        "disp": true,
        "width": "6"
    },
    {
        "id": "95d17d8a.8e1d8",
        "type": "ui_tab",
        "z": "",
        "name": "Schlafzimmer",
        "icon": "fa-bed"
    }
]
somebuddy87

Flow Info

created 3 months ago

Node Types

Core
  • debug (x1)
  • function (x1)
Other
  • ui_button (x2)
  • ui_group (x1)
  • ui_numeric (x1)
  • ui_tab (x1)
  • ui_text (x1)

Tags

  • timer
  • sleeptimer
  • sleep
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option