There has been a lot of talk about ChatGPT and areas of use for the solution. It is known that this solution does not have updated entirely datasets, but there is still a lot of fun that you can use the system for.
One of the things I’ve seen some people do is integrate ChatGPT into the Home Assistant to vary how the system says “hello” to you.
This solution uses Node-Red to obtain the information from ChatGPT. You can test the solution for free using the ChatGPT API. By signing up, you get $20 in credit here.
Start with the following steps to get started (I assume that you have Node-Red installed).
- Create an OpenAI account: Go to the OpenAI website, create an account, and obtain an API key.
- Create an input_text helper. In Home Assistant, create an input_text helper to store the output of ChatGPT. To do this, go to Settings -> Device & Services -> Helpers -> Create Helper (I named it gpt_greeting)
- Create a flow in Node-RED. Import the code below into Node-RED to create the flow.
- Remember to insert the API key that you have got from the OpenAI website. This is done on the node called “Structure JSON payload”
- Update nodes. Update the two nodes called “Get forecast from HA/Weaterflow” and “Get temperature from HA/Weatherflow” with the entities from your setup.
This flow will run once every hour between 05.00 -> 22.00 every day.
[{"id":"b9a019aebcf64bc6","type":"api-call-service","z":"e3a6f8cf.b7df28","name":"Set input_text in Home Assistant","server":"7d84d6a9.3d98f8","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":["input_text.gpt_greeting"],"data":"{ \"value\": $lookup(msg, \"payload\") }","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":690,"y":920,"wires":[["7ae8fd53df9a6221"]]},{"id":"7cac5f5e4fb70243","type":"function","z":"e3a6f8cf.b7df28","name":"Parse output and clean it up","func":"if (!msg.payload || !msg.payload.choices || !msg.payload.choices.length) return \"How can I help you today?\"\nlet text = msg.payload.choices[0].text\ntext = text.replace(/[\\n\\r]/g, '')\nreturn { payload: text }","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":820,"wires":[["b9a019aebcf64bc6","7ae8fd53df9a6221"]]},{"id":"087db1bc495ce965","type":"http request","z":"e3a6f8cf.b7df28","name":"OpenAI API Request","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.openai.com/v1/completions","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":700,"y":700,"wires":[["7cac5f5e4fb70243"]]},{"id":"d0d76e774a87b871","type":"function","z":"e3a6f8cf.b7df28","name":"Structure JSON payload","func":"const { forecast, temperature } = msg\nconst time = new Date().toLocaleTimeString('nb-NO')\nconst date = new Date().toLocaleDateString('nb-NO')\n\nconst prompt = `Act as a personal assistant for a member of a household. You should have a strong sense of humor and talk conversationally. The current time is ${time}, and the current date is ${date}. The weather forecast is ${forecast} and the current temperature is ${temperature} degrees Celsius. Greet me in a friendly way, commenting on the weather, and sometimes making a joke, in one or two sentences. `\n\nmsg.headers = {\n \"Content-Type\": \"application/json\",\n \"Authorization\": \"Bearer YOUR-OPEN-API-KEY\"\n}\n\nmsg.payload = JSON.stringify({\n \"model\": \"text-davinci-003\",\n \"prompt\": prompt,\n \"temperature\": 1,\n \"max_tokens\": 512,\n \"top_p\": 1,\n \"frequency_penalty\": 0,\n \"presence_penalty\": 0\n})\nmsg.method = \"POST\"\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":640,"wires":[["087db1bc495ce965"]]},{"id":"8870d25ab0913af2","type":"api-current-state","z":"e3a6f8cf.b7df28","name":"Get temperature from HA/Weatherflow","server":"7d84d6a9.3d98f8","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.utetemperatur","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"temperature","propertyType":"msg","value":"","valueType":"entityState"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":310,"y":640,"wires":[["d0d76e774a87b871"]]},{"id":"bf9a5b14053d4ec4","type":"api-current-state","z":"e3a6f8cf.b7df28","name":"Get forecast from HA/Weatherflow","server":"7d84d6a9.3d98f8","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"weather.home_hourly","state_type":"str","blockInputOverrides":true,"outputProperties":[{"property":"forecast","propertyType":"msg","value":"","valueType":"entityState"},{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":300,"y":560,"wires":[["8870d25ab0913af2"]]},{"id":"772e271b13a39eb4","type":"inject","z":"e3a6f8cf.b7df28","name":"Begin workflow and run every hour","props":[],"repeat":"","crontab":"0 5-21 * * *","once":false,"onceDelay":0.1,"topic":"","x":280,"y":480,"wires":[["bf9a5b14053d4ec4"]]},{"id":"7d84d6a9.3d98f8","type":"server","name":"Home Assistant"}]
The result:
Good morning! Yikes, it’s cold out there today! At least it’s not a “snow” problem – looks like we’re getting both snow and rain 😉
Open ChatGPT
Note – I have not created all this myself. I do not know who created the different nodes/flows, so I’m not sure who to credit. Sorry 🙁