Skip to content

Instantly share code, notes, and snippets.

@toneomgomg
Created January 23, 2022 14:45
Show Gist options
  • Save toneomgomg/d38f87973edf03c74465a907eaa7292a to your computer and use it in GitHub Desktop.
Save toneomgomg/d38f87973edf03c74465a907eaa7292a to your computer and use it in GitHub Desktop.
Node-RED/Home Assistant: Flow to count number of people at home.
[
{
"id": "bcba552587aca961",
"type": "tab",
"label": "People counter",
"disabled": false,
"info": "",
"env": []
},
{
"id": "14c2c444afcdb504",
"type": "server-state-changed",
"z": "bcba552587aca961",
"name": "",
"server": "2dc0325926925490",
"version": 3,
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"entityidfilter": "^person\\..*$",
"entityidfiltertype": "regex",
"outputinitially": true,
"state_type": "str",
"haltifstate": "",
"halt_if_type": "str",
"halt_if_compare": "is",
"outputs": 1,
"output_only_on_state_change": true,
"for": 0,
"forType": "num",
"forUnits": "minutes",
"ignorePrevStateNull": false,
"ignorePrevStateUnknown": false,
"ignorePrevStateUnavailable": false,
"ignoreCurrentStateUnknown": false,
"ignoreCurrentStateUnavailable": false,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
}
],
"x": 170,
"y": 120,
"wires": [
[
"ca1d8b9a03fc52f0"
]
]
},
{
"id": "ca1d8b9a03fc52f0",
"type": "function",
"z": "bcba552587aca961",
"name": "Count people at home",
"func": "// get array of people at home\nlet people_home = context.get('people_home') || [];\n\n// Add person to array\nif (msg.payload === 'home' && !people_home.includes(msg.topic)) {\n people_home.push(msg.topic);\n}\n\n// Remove person from array\nif (msg.payload !== 'home' && people_home.includes(msg.topic)) {\n people_home = people_home.filter(item => item !== msg.topic)\n}\n\n// Store array\ncontext.set('people_home', people_home);\n\n// Pass on length of array\nreturn { payload: people_home.length };",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 460,
"y": 120,
"wires": [
[
"b8e6d0ce4272796d"
]
]
},
{
"id": "b8e6d0ce4272796d",
"type": "ha-entity",
"z": "bcba552587aca961",
"name": "sensor.number_of_people_home",
"server": "2dc0325926925490",
"version": 2,
"debugenabled": false,
"outputs": 1,
"entityType": "sensor",
"config": [
{
"property": "name",
"value": "number_of_people_home"
},
{
"property": "device_class",
"value": ""
},
{
"property": "icon",
"value": "mdi:person"
},
{
"property": "unit_of_measurement",
"value": ""
},
{
"property": "state_class",
"value": ""
},
{
"property": "last_reset",
"value": ""
}
],
"state": "payload",
"stateType": "msg",
"attributes": [],
"resend": true,
"outputLocation": "payload",
"outputLocationType": "none",
"inputOverride": "allow",
"outputOnStateChange": false,
"outputPayload": "$entity().state ? \"on\": \"off\"",
"outputPayloadType": "jsonata",
"x": 760,
"y": 120,
"wires": [
[]
]
},
{
"id": "2dc0325926925490",
"type": "server",
"name": "Home Assistant: Development Server",
"version": 2,
"addon": false,
"rejectUnauthorizedCerts": true,
"ha_boolean": "y|yes|true|on|home|open",
"connectionDelay": true,
"cacheJson": true,
"heartbeat": false,
"heartbeatInterval": "30"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment