Who's home? Telegram command with Home-Assistant

Who's home ?

When you send "/presence" to your Telegram Bot, he will ask your Home-Assistant server for the devices currently at home, and send you the list with their friendly names.

Nodes used

[{"id":"53235f2a.f66198","type":"tab","label":"Presence flow","disabled":false,"info":""},{"id":"5e875faa.a6216","type":"api-current-state","z":"53235f2a.f66198","name":"Device list","server":"97033d39.2965b","halt_if":"","override_topic":true,"override_payload":true,"entity_id":"group.all_devices","x":347.4285583496094,"y":471.71429443359375,"wires":[["6277c071.af7228"]]},{"id":"6277c071.af7228","type":"function","z":"53235f2a.f66198","name":"Init lists","func":"var devices = msg.data.attributes.entity_id;\nvar i = devices.length;\n\nglobal.set('devicesHome', []);\nglobal.set('i', i);\n\nreturn {'payload': devices};","outputs":1,"noerr":0,"x":497.4285583496094,"y":471.71429443359375,"wires":[["72048cac.92e41c"]]},{"id":"72048cac.92e41c","type":"splitter","z":"53235f2a.f66198","name":"for devices","property":"payload","x":649.4285888671875,"y":471.71429443359375,"wires":[["d9e0c5bb.03c6c8"]]},{"id":"d9e0c5bb.03c6c8","type":"function","z":"53235f2a.f66198","name":"get entity","func":"payload = {'entity_id': msg.payload}\ni = global.get('i');\ni = i -1;\ni = global.set('i', i);\nreturn {'payload': payload};","outputs":1,"noerr":0,"x":799.4285888671875,"y":471.71429443359375,"wires":[["b6d667a1.1a1d18"]]},{"id":"b6d667a1.1a1d18","type":"api-current-state","z":"53235f2a.f66198","name":"home?","server":"97033d39.2965b","halt_if":"","override_topic":false,"override_payload":false,"entity_id":"sun.sun","x":920.4285888671875,"y":471.71429443359375,"wires":[["fb6661fb.5d6558"]]},{"id":"fb6661fb.5d6558","type":"function","z":"53235f2a.f66198","name":"get name","func":"if (msg.payload == 'home'){\n    name = msg.data.attributes.friendly_name;\n    list = global.get('devicesHome');\n    list.push(name);\n    global.set('devicesHome', list)\n}\ni = global.get('i');\nreturn {'payload': {'i': i}};","outputs":1,"noerr":0,"x":1044.4287109375,"y":471.1141662597656,"wires":[["862b0d65.6785d8"]]},{"id":"578bdf03.07d18","type":"telegrambot-command","z":"53235f2a.f66198","bot":"b5f42a51.1edcd8","command":"/presence","commandType":"str","commandCase":false,"x":167.42855834960938,"y":471.71429443359375,"wires":[["5e875faa.a6216"]]},{"id":"5a1ad1ec.6f81","type":"telegrambot-payload","z":"53235f2a.f66198","name":"Send text","bot":"b5f42a51.1edcd8","chatId":"","sendMethod":"sendMessage","payload":"","x":1446.28564453125,"y":473.2856750488281,"wires":[[]]},{"id":"862b0d65.6785d8","type":"switch","z":"53235f2a.f66198","name":"i=0?","property":"payload.i","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":1185.7355346679688,"y":470.55230712890625,"wires":[["2337adbb.8f184a"]]},{"id":"2337adbb.8f184a","type":"function","z":"53235f2a.f66198","name":"build text","func":"devices = global.get('devicesHome');\nif (devices.length > 0){\n    var text = 'Devices currently at home:\\n\\n' + devices.join('\\n');\n} else {\n    var text = 'Nobody\\'s home right now.';\n}\n\nreturn {'payload': {'text': text}};","outputs":1,"noerr":0,"x":1315.735595703125,"y":471.91900634765625,"wires":[["5a1ad1ec.6f81"]]},{"id":"97033d39.2965b","type":"server","z":"","name":"HomeAssistant","url":"","pass":""},{"id":"b5f42a51.1edcd8","type":"telegrambot-config","z":"","botname":"TelegramBot","usernames":"","chatIds":"","pollInterval":"300"}]

Flow Info

Created 7 years, 2 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x4)
  • switch (x1)
Other
  • api-current-state (x2)
  • server (x1)
  • splitter (x1)
  • tab (x1)
  • telegrambot-command (x1)
  • telegrambot-config (x1)
  • telegrambot-payload (x1)

Tags

  • telegram
  • home
  • homeassistant
  • home-assistant
  • presence
  • bot
  • command
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option