Skip to content

Instantly share code, notes, and snippets.

@holnburger
Created March 18, 2023 21:56
Show Gist options
  • Save holnburger/413edcdcc62daddf7d3942b4140d27cd to your computer and use it in GitHub Desktop.
Save holnburger/413edcdcc62daddf7d3942b4140d27cd to your computer and use it in GitHub Desktop.
Node Red export for Home Assistant + Alexa Actionable Notifications + ChatGPT full conversation mode
[
{
"id": "12370a346f7d923d",
"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": 1540,
"wires": [
[
"9fca74bbf28ee1c8"
]
]
},
{
"id": "9fca74bbf28ee1c8",
"type": "function",
"z": "98f3bced.07a83",
"name": "format data",
"func": "const 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\nmsg.payload = {\n model: \"gpt-3.5-turbo\",\n messages: chatGPT,\n max_tokens: 800\n}\n\n\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 330,
"y": 1540,
"wires": [
[
"348872297180e97d"
]
]
},
{
"id": "348872297180e97d",
"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": 520,
"y": 1540,
"wires": [
[
"74fe0221467e59c3"
]
]
},
{
"id": "74fe0221467e59c3",
"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": 620,
"y": 1580,
"wires": [
[
"06f768257c0db3c0"
]
]
},
{
"id": "06f768257c0db3c0",
"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\": \"media_player.wohnzimmer_dot\" }",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 920,
"y": 1580,
"wires": [
[
"6f8f0d31d76ab5e7"
]
]
},
{
"id": "6f8f0d31d76ab5e7",
"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\": \"media_player.wohnzimmer_dot\"}",
"dataType": "jsonata",
"mergeContext": "",
"mustacheAltTags": false,
"outputProperties": [],
"queue": "none",
"x": 1120,
"y": 1580,
"wires": [
[]
]
},
{
"id": "d58724e9ee465e39",
"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": 610,
"y": 1720,
"wires": [
[
"04b27c4dc2aee29e"
]
]
},
{
"id": "04b27c4dc2aee29e",
"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": 860,
"y": 1720,
"wires": [
[
"5f384e2a1a669b0a"
]
]
},
{
"id": "5f384e2a1a669b0a",
"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": 1020,
"y": 1720,
"wires": [
[],
[
"13bfdd1356e7afba"
]
]
},
{
"id": "13bfdd1356e7afba",
"type": "function",
"z": "98f3bced.07a83",
"name": "format data",
"func": "const 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": 1030,
"y": 1760,
"wires": [
[
"7874d3baab2a204a"
]
]
},
{
"id": "7874d3baab2a204a",
"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": 1190,
"y": 1760,
"wires": [
[
"4253ea5acb0527c7"
]
]
},
{
"id": "4253ea5acb0527c7",
"type": "function",
"z": "98f3bced.07a83",
"name": "format output and save response",
"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\nmsg.payload = gptAnswer\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1420,
"y": 1760,
"wires": [
[
"06f768257c0db3c0"
]
]
},
{
"id": "c13b4a7893634e46",
"type": "comment",
"z": "98f3bced.07a83",
"name": "Insert your API Key in the Bearer Auth",
"info": "",
"x": 590,
"y": 1500,
"wires": []
},
{
"id": "d2b8944c8906e6b2",
"type": "comment",
"z": "98f3bced.07a83",
"name": "Insert specific event_id",
"info": "",
"x": 1120,
"y": 1540,
"wires": []
},
{
"id": "f48f8ddc8737724b",
"type": "comment",
"z": "98f3bced.07a83",
"name": "reuse event_id",
"info": "",
"x": 860,
"y": 1680,
"wires": []
},
{
"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