Skip to content

Instantly share code, notes, and snippets.

@holnburger
Created March 18, 2023 22:00
Show Gist options
  • Save holnburger/1a042b7d94e2d72ca592542c20a4c528 to your computer and use it in GitHub Desktop.
Save holnburger/1a042b7d94e2d72ca592542c20a4c528 to your computer and use it in GitHub Desktop.
Node Red export for Home Assistant + Alexa Actionable Notifications + ChatGPT – updates from the last Alexa Device being called
[
{
"id": "f77de3f3f717c5c3",
"type": "ha-button",
"z": "98f3bced.07a83",
"name": "Friday",
"version": 0,
"debugenabled": false,
"outputs": 1,
"entityConfig": "9d435f6632fb9b75",
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "entityState"
},
{
"property": "topic",
"propertyType": "msg",
"value": "",
"valueType": "triggerId"
},
{
"property": "data",
"propertyType": "msg",
"value": "",
"valueType": "entity"
}
],
"x": 170,
"y": 2000,
"wires": [
[
"9b1169dc378666e1"
]
]
},
{
"id": "4f4af0ce64f57057",
"type": "function",
"z": "98f3bced.07a83",
"name": "format data and save last used alexa",
"func": "// save GPT data\nconst system_message = `You are Friday, an artificial intelligence like the one in the Iron Man movies. You are used to automate Joe's apartment. You are always a bit funny and ironic, but always a help.`\n\nconst chatGPT = [\n { \"role\": \"system\", \"content\": system_message},\n { \"role\": \"user\", \"content\": \"Hello Friday.\" }\n]\n\nflow.set(\"chathistory\", chatGPT)\n\n// save last used alexa\nconst ha = global.get('homeassistant').homeAssistant.states;\nconst last_alexa = ha['sensor.last_alexa'].state\n\nflow.set(\"last_alexa\", last_alexa)\n\nmsg.last_alexa = last_alexa\n\nmsg.payload = {\n model: \"gpt-3.5-turbo\",\n messages: chatGPT,\n max_tokens: 800\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 690,
"y": 2000,
"wires": [
[
"a9f1968fd54e41f0"
]
]
},
{
"id": "c76378a2cdb7f42c",
"type": "function",
"z": "98f3bced.07a83",
"name": "format output and save response",
"func": "const gpt_answer = msg.payload.choices[0].message.content.replace(/['\"]+/g, '')\n\n// save response to flow variable\nconst chat_history = flow.get(\"chathistory\")\n\nchat_history.push({\n role: \"assistant\",\n content: gpt_answer\n})\n\nflow.set(\"chathistory\", chat_history)\n\n\nmsg.payload = gpt_answer\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1000,
"y": 2040,
"wires": [
[
"1784a8585978c273"
]
]
},
{
"id": "1784a8585978c273",
"type": "api-call-service",
"z": "98f3bced.07a83",
"name": "notify via Alexa",
"server": "e78c2b0.2b5c2d8",
"version": 5,
"debugenabled": false,
"domain": "notify",
"service": "alexa_media",
"areaId": [],
"deviceId": [],
"entityId": [],
"data": "{ \"message\": payload, \"target\": last_alexa }",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1300,
"y": 2040,
"wires": [
[
"eb6edfa97134f46c"
]
]
},
{
"id": "eb6edfa97134f46c",
"type": "api-call-service",
"z": "98f3bced.07a83",
"name": "Actionable Notification",
"server": "e78c2b0.2b5c2d8",
"version": 5,
"debugenabled": false,
"domain": "script",
"service": "activate_alexa_actionable_notification",
"areaId": [],
"deviceId": [],
"entityId": [],
"data": "{ \"text\": \" \", \"event_id\": \"friday\", \"alexa_device\": last_alexa }",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1500,
"y": 2040,
"wires": [
[]
]
},
{
"id": "46e4589afa5de7ad",
"type": "server-events",
"z": "98f3bced.07a83",
"name": "Alexa Actionable Event listener",
"server": "e78c2b0.2b5c2d8",
"version": 1,
"event_type": "alexa_actionable_notification",
"exposeToHomeAssistant": false,
"haConfig": [
{
"property": "name",
"value": ""
},
{
"property": "icon",
"value": ""
}
],
"waitForRunning": true,
"outputProperties": [
{
"property": "payload",
"propertyType": "msg",
"value": "",
"valueType": "eventData"
},
{
"property": "topic",
"propertyType": "msg",
"value": "$outputData(\"eventData\").event_type",
"valueType": "jsonata"
}
],
"x": 670,
"y": 2160,
"wires": [
[
"6177635ed8cba9a9"
]
]
},
{
"id": "6177635ed8cba9a9",
"type": "switch",
"z": "98f3bced.07a83",
"name": "check event_id",
"property": "payload.event.event_id",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "friday",
"vt": "str"
}
],
"checkall": "true",
"repair": false,
"outputs": 1,
"x": 920,
"y": 2160,
"wires": [
[
"6e5e336ec06d6132"
]
]
},
{
"id": "6e5e336ec06d6132",
"type": "switch",
"z": "98f3bced.07a83",
"name": "answers",
"property": "payload.event.event_response_type",
"propertyType": "msg",
"rules": [
{
"t": "eq",
"v": "ResponseNone",
"vt": "str"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 1080,
"y": 2160,
"wires": [
[],
[
"6d83b7f6673a778b"
]
]
},
{
"id": "6d83b7f6673a778b",
"type": "function",
"z": "98f3bced.07a83",
"name": "format data",
"func": "let chat_history = flow.get(\"chathistory\")\n\nconst user_response = msg.payload.event.event_response\n\nchat_history.push({ \n role: \"user\", \n content: user_response }\n)\n\nflow.set(\"chathistory\", chat_history)\n\nmsg.payload = {\n model: \"gpt-3.5-turbo\",\n messages: chat_history,\n max_tokens: 800\n}\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1090,
"y": 2200,
"wires": [
[
"0bcb3d71a17855e1"
]
]
},
{
"id": "0bcb3d71a17855e1",
"type": "http request",
"z": "98f3bced.07a83",
"name": "",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "https://api.openai.com/v1/chat/completions",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "bearer",
"senderr": false,
"headers": [],
"x": 1250,
"y": 2200,
"wires": [
[
"1cf3909c4fc6af11"
]
]
},
{
"id": "1cf3909c4fc6af11",
"type": "function",
"z": "98f3bced.07a83",
"name": "format output and save response + last alexa used",
"func": "const gptAnswer = msg.payload.choices[0].message.content\n\nconst chat_history = flow.get(\"chathistory\")\n\nchat_history.push({role: \"assistant\", content: gptAnswer})\n\nflow.set(\"chathistory\", chat_history)\n\n// get last used alexa\nmsg.last_alexa = flow.get(\"last_alexa\")\n\nmsg.payload = gptAnswer\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1530,
"y": 2200,
"wires": [
[
"1784a8585978c273"
]
]
},
{
"id": "107bbc38bda35a00",
"type": "comment",
"z": "98f3bced.07a83",
"name": "Insert your API Key in the Bearer Auth",
"info": "",
"x": 970,
"y": 1960,
"wires": []
},
{
"id": "f2c7fd8cab6609ad",
"type": "comment",
"z": "98f3bced.07a83",
"name": "Insert specific event_id",
"info": "",
"x": 1500,
"y": 2000,
"wires": []
},
{
"id": "9379550634b05aea",
"type": "comment",
"z": "98f3bced.07a83",
"name": "reuse event_id",
"info": "",
"x": 920,
"y": 2120,
"wires": []
},
{
"id": "9b1169dc378666e1",
"type": "api-call-service",
"z": "98f3bced.07a83",
"name": "update last called alexa",
"server": "e78c2b0.2b5c2d8",
"version": 5,
"debugenabled": false,
"domain": "alexa_media",
"service": "update_last_called",
"areaId": [],
"deviceId": [],
"entityId": [],
"data": "",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 370,
"y": 2000,
"wires": [
[
"4f4af0ce64f57057"
]
]
},
{
"id": "a9f1968fd54e41f0",
"type": "http request",
"z": "98f3bced.07a83",
"name": "openai request",
"method": "POST",
"ret": "obj",
"paytoqs": "ignore",
"url": "https://api.openai.com/v1/chat/completions",
"tls": "",
"persist": false,
"proxy": "",
"insecureHTTPParser": false,
"authType": "bearer",
"senderr": false,
"headers": [],
"x": 940,
"y": 2000,
"wires": [
[
"c76378a2cdb7f42c"
]
]
},
{
"id": "9d435f6632fb9b75",
"type": "ha-entity-config",
"server": "e78c2b0.2b5c2d8",
"name": "Friday",
"version": 2,
"haConfig": [
{
"property": "name",
"value": "Friday"
},
{
"property": "icon",
"value": "mdi:robot"
},
{
"property": "device_class",
"value": ""
}
],
"entityType": "button"
},
{
"id": "e78c2b0.2b5c2d8",
"type": "server",
"name": "Home Assistant",
"version": 2,
"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