Skip to content

Instantly share code, notes, and snippets.

@notenoughtech
Created October 22, 2023 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notenoughtech/3834d22d7065f54859a8b66dc9326525 to your computer and use it in GitHub Desktop.
Save notenoughtech/3834d22d7065f54859a8b66dc9326525 to your computer and use it in GitHub Desktop.
Sonoff Presence Sensor
[
{
"id": "f4e11ed845dda79d",
"type": "group",
"z": "adddefbcf5d062bb",
"name": "Sonoff Presence Dashboard",
"style": {
"label": true,
"fill": "#ffffbf",
"label-position": "n",
"color": "#000000"
},
"nodes": [
"2a8dcf0d9914edf0",
"878cf33ac29f2c76",
"245b9f86a1d07c7c",
"35fe412184eab7dd",
"bc64412d9b0fc4fd",
"48360d364bf5475c",
"2065f3a980db78a0",
"258f680ec6b60ac9",
"f979f561f91c5351"
],
"x": 54,
"y": 33,
"w": 1032,
"h": 248
},
{
"id": "2a8dcf0d9914edf0",
"type": "mqtt in",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Sonoff Presence Sensor",
"topic": "z2m/Sonoff Presence Sensor",
"qos": "0",
"datatype": "json",
"broker": "539ac536f0c6226d",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 190,
"y": 220,
"wires": [
[
"245b9f86a1d07c7c"
]
]
},
{
"id": "878cf33ac29f2c76",
"type": "ui_button",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "",
"group": "615e11044bcac806",
"order": 0,
"width": "5",
"height": "1",
"passthru": false,
"label": "{{duration}}",
"tooltip": "Sonoff Presence Sensor",
"color": "white",
"bgcolor": "{{background}}",
"className": "",
"icon": "{{icon}}",
"payload": "",
"payloadType": "str",
"topic": "topic",
"topicType": "msg",
"x": 1010,
"y": 220,
"wires": [
[]
]
},
{
"id": "245b9f86a1d07c7c",
"type": "switch",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "",
"property": "payload.occupancy",
"propertyType": "msg",
"rules": [
{
"t": "false"
},
{
"t": "true"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 450,
"y": 220,
"wires": [
[
"35fe412184eab7dd"
],
[
"48360d364bf5475c"
]
]
},
{
"id": "35fe412184eab7dd",
"type": "function",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Absent duration",
"func": "var last_state = flow.get(\"sonoff_presence_sensor.last_state\");\nvar date = Date.now();\nvar state = msg.payload.occupancy;\n\n\nif(last_state !== state){\n flow.set(\"sonoff_presence_sensor.absent\", date);\n flow.set(\"sonoff_presence_sensor.last_state\", state);\n }\n\n\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 200,
"wires": [
[
"bc64412d9b0fc4fd"
]
]
},
{
"id": "bc64412d9b0fc4fd",
"type": "function",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Display",
"func": "var date = new Date();\n\nvar time = flow.get(\"sonoff_presence_sensor\");\nvar last_state = time.last_state;\n\nfunction msToTime(duration) {\n var milliseconds = Math.floor((duration % 1000) / 100),\n seconds = Math.floor((duration / 1000) % 60),\n minutes = Math.floor((duration / (1000 * 60)) % 60),\n hours = Math.floor((duration / (1000 * 60 * 60)) % 24);\n\n hours = (hours < 10) ? \"0\" + hours : hours;\n minutes = (minutes < 10) ? \"0\" + minutes : minutes;\n seconds = (seconds < 10) ? \"0\" + seconds : seconds;\n\n return hours + \":\" + minutes + \":\" + seconds;\n}\n\nfunction CalcTime(timeNow, timeStored){\n var t = timeNow - timeStored\n // change to radable\n return msToTime(t);\n}\n\nif (last_state === true){ \n msg.duration = CalcTime(date, time.present);\n msg.icon = \"mi-person_check\";\n msg.background = \"green\";\n}\nif (last_state === false){\n msg.duration = CalcTime(date, time.absent);\n msg.icon = \"mi-no_accounts\";\n msg.background = \"blue\";\n}\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 860,
"y": 220,
"wires": [
[
"878cf33ac29f2c76"
]
]
},
{
"id": "48360d364bf5475c",
"type": "function",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Present duration",
"func": "var last_state = flow.get(\"sonoff_presence_sensor.last_state\");\nvar date = Date.now();\nvar state = msg.payload.occupancy;\n\nif(last_state !== state){\n flow.set(\"sonoff_presence_sensor.present\", date);\n flow.set(\"sonoff_presence_sensor.last_state\", state);\n\n};\n\n\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 240,
"wires": [
[
"bc64412d9b0fc4fd"
]
]
},
{
"id": "2065f3a980db78a0",
"type": "inject",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Update Interval",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "1",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 640,
"y": 160,
"wires": [
[
"bc64412d9b0fc4fd"
]
]
},
{
"id": "258f680ec6b60ac9",
"type": "group",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "Dev Testing",
"style": {
"label": true,
"fill": "#ffffff",
"label-position": "n",
"color": "#ff0000"
},
"nodes": [
"a92466e1ed9b6887",
"63afb0c415d394f4",
"9eb16fde173d6c13",
"f6363f96bd816c55"
],
"x": 94,
"y": 59,
"w": 242,
"h": 122
},
{
"id": "a92466e1ed9b6887",
"type": "inject",
"z": "adddefbcf5d062bb",
"g": "258f680ec6b60ac9",
"name": "Absent",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"occupancy\": false}",
"payloadType": "json",
"x": 190,
"y": 100,
"wires": [
[
"9eb16fde173d6c13"
]
]
},
{
"id": "63afb0c415d394f4",
"type": "inject",
"z": "adddefbcf5d062bb",
"g": "258f680ec6b60ac9",
"name": "present",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "{\"occupancy\": true}",
"payloadType": "json",
"x": 190,
"y": 140,
"wires": [
[
"f6363f96bd816c55"
]
]
},
{
"id": "9eb16fde173d6c13",
"type": "link out",
"z": "adddefbcf5d062bb",
"g": "258f680ec6b60ac9",
"name": "link out 19",
"mode": "link",
"links": [
"f979f561f91c5351"
],
"x": 295,
"y": 100,
"wires": []
},
{
"id": "f6363f96bd816c55",
"type": "link out",
"z": "adddefbcf5d062bb",
"g": "258f680ec6b60ac9",
"name": "link out 20",
"mode": "link",
"links": [
"f979f561f91c5351"
],
"x": 295,
"y": 140,
"wires": []
},
{
"id": "f979f561f91c5351",
"type": "link in",
"z": "adddefbcf5d062bb",
"g": "f4e11ed845dda79d",
"name": "link in 26",
"links": [
"f6363f96bd816c55",
"9eb16fde173d6c13"
],
"x": 365,
"y": 200,
"wires": [
[
"245b9f86a1d07c7c"
]
]
},
{
"id": "539ac536f0c6226d",
"type": "mqtt-broker",
"name": "MQTT (home.lan)",
"broker": "home.lan",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"userProps": "",
"sessionExpiry": ""
},
{
"id": "615e11044bcac806",
"type": "ui_group",
"name": "Living Room",
"tab": "d4f71d4d745ebed0",
"order": 1,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "d4f71d4d745ebed0",
"type": "ui_tab",
"name": "Home2",
"icon": "dashboard",
"disabled": false,
"hidden": false
}
]
@sabamimi
Copy link

Thanks for sharing the flow !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment