Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
座りっぱなし検知アラート
[ { "id": "f6f2187d.f17ca8", "type": "tab", "label": "Swarippa", "disabled": false, "info": "", "env": [] }, { "id": "19e47bd1eea2e757", "type": "http request", "z": "f6f2187d.f17ca8", "name": "Slack Webhook - elzup", "method": "POST", "ret": "txt", "paytoqs": "ignore", "url": "${SLACK_URL}", "tls": "", "persist": false, "proxy": "", "authType": "", "senderr": false, "x": 1050, "y": 1180, "wires": [ [] ] }, { "id": "803d03164af5eb1a", "type": "change", "z": "f6f2187d.f17ca8", "name": "", "rules": [ { "t": "set", "p": "payload", "pt": "msg", "to": "{\"text\":\"動いて!\",\"icon_emoji\":\":chair:\",\"username\":\"座りっぱ執事\"}", "tot": "json" } ], "action": "", "property": "", "from": "", "to": "", "reg": false, "x": 820, "y": 1180, "wires": [ [ "19e47bd1eea2e757", "29d5897e04face4f" ] ] }, { "id": "29d5897e04face4f", "type": "debug", "z": "f6f2187d.f17ca8", "name": "debug log", "active": true, "tosidebar": true, "console": false, "tostatus": false, "complete": "payload", "targetType": "msg", "statusVal": "", "statusType": "auto", "x": 1000, "y": 1220, "wires": [] }, { "id": "a0a7801fd8422523", "type": "http request", "z": "f6f2187d.f17ca8", "name": "Switchbot Motion", "method": "GET", "ret": "obj", "paytoqs": "body", "url": "https://api.switch-bot.com/v1.0/devices/{{device}}/status", "tls": "", "persist": false, "proxy": "", "authType": "", "senderr": false, "x": 650, "y": 1000, "wires": [ [ "f74542ee844a5573" ] ] }, { "id": "f17231e1cc5bfcb7", "type": "inject", "z": "f6f2187d.f17ca8",
"name": "定期実行",
"props": [],
"repeat": "10",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 420,
"y": 880,
"wires": [
[
"6d7fcdd0da968b1e"
]
]
},
{
"id": "6d7fcdd0da968b1e",
"type": "function",
"z": "f6f2187d.f17ca8",
"name": "build Switchbot API",
"func": "msg.headers = {}\nmsg.headers['content-type'] = 'application/json'\nmsg.headers['authorization'] = env.get(\"SWITCHBOT_TOKEN\")\nmsg.device = env.get('MONITOR_DEVICE_ID')\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 450,
"y": 1000,
"wires": [
[
"a0a7801fd8422523"
]
]
},
{
"id": "f74542ee844a5573",
"type": "change",
"z": "f6f2187d.f17ca8",
"name": "moveDetected",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "payload.body.moveDetected",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 840,
"y": 1000,
"wires": [
[
"ae88fe997f60ef3e"
]
]
},
{
"id": "ae88fe997f60ef3e",
"type": "function",
"z": "f6f2187d.f17ca8",
"name": "check notice",
"func": "\nconst SUSPEND_DETECT_TIME = 30 * 60 * 1000\n\n\nconst isSuspend = (now, lastMoved, suspendDetectTime, detect) => {\n if (detect) return false\n const suspendTime = now - lastMoved\n\n return suspendTime >= suspendDetectTime\n}\n\n// if not detected some time (SUSPEND_DETECT_TIME), once do notice\nconst reducer = (\n { noticed, lastMoved },\n detected,\n now\n) => {\n if (detected) {\n return { state: { lastMoved: now, noticed }, doNotice: false }\n }\n const suspend = isSuspend(now, lastMoved, SUSPEND_DETECT_TIME, detected)\n const doNotice = !noticed && suspend\n return { state: { noticed: doNotice || noticed, lastMoved }, doNotice }\n}\n\nconst initialState = {\n lastMoved: 0,\n noticed: true,\n}\n\n\nconst state = flow.get(\"suwarippa-state\") || initialState\n\nconst detected = msg.payload\nconst now = +new Date()\nconst { state: newState, doNotice } = reducer(state, detected, now)\nflow.set(\"suwarippa-state\", newState)\n\nmsg.payload = Number(doNotice)\nmsg.debug = { newState, doNotice }\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 430,
"y": 1140,
"wires": [
[
"5658ec6172d6c57c"
]
]
},
{
"id": "5658ec6172d6c57c",
"type": "switch",
"z": "f6f2187d.f17ca8",
"name": "is do notice",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "1",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 630,
"y": 1180,
"wires": [
[
"803d03164af5eb1a"
]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment