Skip to content

All the necessary scripts and config that are needed to create a ChatGPT panel in Home Assistant

Notifications You must be signed in to change notification settings

vv020633/Chat-GPT-HA-Panel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chat-GPT-HA-Panel

Prerequisite knowledge

Prerequisite requirements

  • You'll need to have Node Red running and a location on your host machine where you access files that are stored persistantly
  • Likewise you'll need to have Home Assistant running on your device and a location on your host machine where you access files that are stored persistantly
  • A viable method of transferring files from the Node-Red file directory to the Home Assistant file directory. In my case, Node-Red and Home Assistant are hosted on the same machine, so this can be done using a simple bash script
  • An OpenAi account

Environment

Just to paint the picture of how my system is setup, I'm running Ubuntu Server on a Raspberry Pi 4. Home Assistant and Node-Red are being ran as docker containers via docker compose; bind mounts are created for both of these containers as a means of moving files between the two containers.

Process

  1. With Node Red up and Running, you'll need to install the necessary nodes for this workflow:

nodes

  1. Import the nodered_chatgpt_flow.json from this repository into Node-Red. It should produce the workflow shown below Workflow For this workflow to succeed, you'll need to provide your own API Key and Organzation ID as input into the ChatGPT node, and you'll need a relevant input text helper to kick off the flow input_helper

What this flow essentially does is awaits for a state change on the text input helper-> passes the input into ChatGPT -> passes the response from ChatGPT into a function which formats the response into valid JSON -> outputs the JSON to /data/json/chat_gpt_response.json in the Node-Red container

  1. Ensure that the directory that Node-Red outputs the JSON to exists and ensure that you have created a directory for your Home Assistant instance from which you can store and retrieve these JSON files. In my case, its the "/data/json" directory which is mounted on the host at "/home/ubuntu/docker/homeassistant_mariadb/nodered/data/json/".

mkdir /home/ubuntu/docker/homeassistant_mariadb/nodered/data/json/

Likewise, I've created a location for the Home Assistant container where I store my API call; in the docker container this location is "/config/api_calls" which is mounted to the "/home/ubuntu/docker/homeassistant_mariadb/config/api_calls/" directory on my host.

mkdir /home/ubuntu/docker/homeassistant_mariadb/config/api_calls/

  1. Navigate to the directory that the "move_gpt_answer.sh" shell script from this repository is located on your local machine. In my case, I've got the files from this repo stored in a local directory at "/home/ubuntu/github_repos/chatgpt_ha_panel". You'll want to ensure that this file belongs to the same user or group that your Home Assistant and Node Red instances belong to in order to avoid permission errors.
  2. Make "move_gpt_answer.sh" executable

chmod +x move_gpt_answer.sh

This is a very short bash script that runs indefinitely. Every half second this script checks if the JSON file exists in the Node-Red directory and if the file exists, it will be moved to the Home Assistant api_calls directory:

bash_script

Ensure that you change the directory's in the bash script to point to the relevant directories on your own machine.

  1. You can run the script in the background by using this command:

./move_gpt_answer.sh &

but to ensure that this script continues to run outside of just this session and on every reboot, a quick addition to the cronfile can solve this. Use the below command to open up the cronfile

crontab -e

Enter the following to the bottom of the cronfile to call this script on every reboot

@reboot /home/your_shell_script_directory/move_gpt_answer.sh

Of course replacing the directory name with that of your own.

  1. reboot your device so that the above cronjob will run

  2. At this point, all that's left is to display the data from the Home Assistant api_calls location within Home Assistant using the command line sensor. The following snippets will need to be added to your Home Assistant configuration.yaml file:

configuration_yaml

The allow_list_external_dirs is used to point allow Home Assistant to use the API calls folder (located at "/config/api_calls" in my docker instance) and the command line sensor is used to call a cat command on the chat_gpt_response.json file which is now accesssible from my Home Assistant Docker instance. This will call this command every second to check for the json file as well as store the response in tha attributes of this sensor (the state/value has a 255 character limit). Bear in mind that the directories I've used here are the docker directories that are mounted to the directories on the host, so the directories that you're using and method of achieveing the same result would differ on a supervised system.

  1. Restart your Home Assistant instance/configuration and if all has gone well, you should now be able to view the response from chatgpt in your Home Assistant Developer Tools (States):

gpt_response

  1. To actually display the content within Home Assistant, I've placed the text_input helper within an "entities" card, and the gpt_json_response sensor into a markdown card. Both cards are contained within a vertical stack card. My exact configuration for this card is stored in this repo within the "panel.yaml" file (minus any css styling) panel config

Within the "content" of the markdown card, you can simply use a template to display the attributes of the json response. ENJOY!

About

All the necessary scripts and config that are needed to create a ChatGPT panel in Home Assistant

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages