HackGPT - Creating a Home Assistant Integration Using ChatGPT


 

About 10 or so years ago I switched my energy supplier and, as part of the new deal, I got given a smart heating thermostat called a Cosy made by a company called Geo (https://cosy.support.geotogether.com/en/support/solutions/articles/7000011187-introducing-cosy).  It's a decent little bit of kit that can be retrofitted in series with your existing, old-school dumb thermostat.  It simplifies things by giving you three pre-sets: Slumber (for a relatively cool environment to sleep in), Comfy (for when you just want to take the chill off) and Cosy (For full blown get a sweat on do nothing on the sofa type days).  You can set a target temperature for each mode, along with schedules for you want each mode to automatically activate.  It has a nice little app, a web interface if you'd prefer that, and can be controlled to some extent via Alexa.

But I've been a Home Assistant user for quite a few years now, and it's quite nice to be able to control things like heating using triggers other than just time, and it using it via HA would give it a bit more flexibility.  For instance, the Cosy monitors temperature using neat a little portable control device that you can move to different places in your home.  But, in my house at least, the temperature is not always the same in different places it could be cold downstairs but toasty in the bedrooms and , if that's where the Cosy device is, it'll stay cold downstairs.  Sure, I could turn the heating on manually but where would be the fun in that?  By sticking some cheap £2 AliExpress temperature sensors around the house, it would be possible to turn the heating on when any area gets cold, not just where the Cosy happens to be.  Pair that up with some smart radiator thermostats and we can even heat just the area / room that is cold rather than the entire house.  But there was a problem, there was no Home Assistant integration available for Cosy.  Sad times.  

I've built myself a few smart home devices in the past that have used MQTT to let Home Assistant to control them and it's worked pretty well, so a few years ago I created a 'Cosy Server' within a Docker container that combined an MQTT server that could receive commands from HA and send back status updates, with a NodeJS implementation of Puppeteer / headless browser which automated the actions of logging on to the Cosy web front end to scrape the information and switch modes etc. It was a bit of a clunky approach, but it worked OK for a while.  It was also pretty limited: you could change modes and get the current mode but that was about it, and you couldn't set it to Hibernate.  The HA front end controls were also a bit lacklustre, a few switches to change the mode was about the extent of it, but you could automate based on other triggers, so it sort of did the job.  But it wasn't very robust.  It didn't always work, sometimes MQTT messages didn't get sent or received or whatever, the server sometimes stopped working and I never got around to debugging it and then, eventually I moved from a HA installation on top of a full-blown Linux install to a HASS image and so stopped using my little server.  I'd posted about my project on the HA forums, and shared the Github repository just in case it might be useful for anyone else.  It turned out it was.  About a year later someone who had seen my project got inspired to create something similar, rather than use MQTT, they wanted to use If This Then That (IFTT) to allow them to control their heating using outside triggers like emails etc. They went for a NuGet package for C# .NET instead of the Docker / Node route I had taken, but more importantly, they had done it using Cosy's non-public API which is what Cosy's web front end uses, rather than going down the headless browser / scraping route that I'd used.  You can see their project here: https://github.com/dan-agilexp/cosyrest/blob/main/README.md  

Using the API seemed like a much better idea and I'd always thought about creating a proper Home Assistant integration so that I'd have full control of everything (modes, target temperatures, hibernate on / off, etc.)  and also have decent and pretty controls on the Home Assistant front end.  I never got around to it until, three years later, which happens to be a few days ago, I did. 

I don't code much these days, I don't really have the time, but I recently came across a YouTube video where a fella let ChatGPT take control of his smart home through HA's built in voice assistant capability and it looked pretty cool.  I thought about maybe doing something similar, but felt like heating would be a key thing I'd like it to be able to control a bit more dynamically.  I remembered the post from the guy I'd inspired, and in turn his work with the non-public Cosy API inspired me to have a look and see if I could create a Home Assistant integration.  

The first step was to figure out how the API worked.  To do this, I used Chrome's built in developer tools. You can access them by clicking the 3 dot menu button to the right of the address bar, hover over 'More Tools' then click developer tools.  This opens a new section within your browser window, and gives you a bunch of tools that allow you to see how a website has been created.  Along the top there are some tabs such as Elements, Console, etc.  that let you inspect different components of the site.  What I was interested in was the messages being sent to and from the site to control the Cosy system.  Luckily, there's a tab for that too.  It's called Network. I navigated the the login page for the Cosy web front-end, then pressed the record button in the toolbar just below the tabs.  I then logged into Cosy, navigated around and changed settings etc. Once I'd done the main actions I wanted to include, I pressed the stop button next to the record button.  Here's how things looked:


The name column on the left shows the different requests that had been made.  By clicking on these I could see the URL's and headers that had been used to request different actions, I could also see the kinds of JSON responses etc. that came back by looking at the response tab on the right-hand pane.  This gave me all the information I needed to know how to interact with the Cosy API to control the device, and what kind of information it would send me back and therefore how I could parse it to get the useful stuff out.

From that point it was pretty easy to write a little Python script that used the requests library to send the right commands to the Cosy API.  If you've not used requests before, here's a little snippet:


import requests
import argparse

# Cosy API endpoints
base_url = "https://cosy.geotogether.com/api/userapi/"

def get_current_temperature(token,systemID):
    data_url = base_url + "system/cosy-live-data/"+systemID
    try:
        headers = {"Authorization": f"Bearer {token}"}
        response = requests.get(data_url, headers=headers)
        response.raise_for_status()
        data = response.json()
        temperature = data["temperatureList"][0]["value"]
        return temperature
    except requests.exceptions.RequestException as e:
        print(f"Error retrieving temperature: {e}")
        return None

I used argparse to allow me to pass commands to the script (--setmode, --getcurrenttemp, etc.) so that I could
call the different methods to control my Cosy system and get info back. It all worked perfectly.

Now I needed to turn the script into a Home Assistant integration. Now, full disclosure, I'm a hobbyist
programmer these days. I do a bit here and there, but I'm far from a professional developer. I had a look at the
Home Assistant Developer documentation, and quickly realised that I wasn't the target audience 😂. I had a bit
of a crack at creating the relevant manifest.json files and config_flow.py files, but it didn't go too well. I then had
a search around on Google and discovered that I wasn't the only person to find the HA docs challenging. There
were some folks out there that had made some honourable attempts at simpler guides, and they helped me get
closer to the goal, but the context and gaps in their explanations meant that it was challenging to apply what they
were saying to my particular use case. Ideally, I would've spent time learning the ins and outs of what was
required but, to be honest, I didn't have the time and I don't know when I'll use it again so will have probably
forgotten it by the time I do. So I was at a point where my little script was probably going to be a sufficient
achievement, and I could probably get HA to use that to control my Cosy. It wasn't the full integration I wanted,
but it was better and more efficient than the cosy server I built previously.

But then I remembered something. A while back I had a play around with an early version of a GPT that was
being built to create full-stack applications and, whilst it wasn't perfect, it was impressive. So I wondered if I
could use ChatGPT to help me create my integration. I pasted my code into ChatGPT, told it that I wanted to
create an integration for Home Assistant, and asked if it could help me do that. Sure! came the response, and it
proceeded to spit out all the necessary files I needed. I span up a Home Assistant dev environment in visual
studio code:(https://developers.home-assistant.io/docs/development_environment/).
Stuck the files into the custom_components folder, and fired it up, not really expecting it to work. It didn't.
But I could see from the logs in Home Assistant that it was a fairly simple problem. It also needed me to add stuff to configuration.yaml
and I didn't want that, I'd prefer to have it configurable by the UI. I explained the error I'd seen to ChatGPT and explained
the additional requirements I had, and she (I don't know why it ChatGPT feels like a she to me, but it does) gave me a new
version of the files that needed editing. It took a few back-and-forths of her providing me with new versions, and me telling
her about the various errors I'd received, but eventually we got to something that looked like it was almost working. We had
a UI card that at least reported the current temperature, and the entity was logging the temp over time. But there was still a
few errors, and I couldn't set the modes etc. On and off over the next couple of days I described the new features I wanted,
and ChatGPT would give me updated code, I'd try it, and then go back to her with the results. It was often one step forward
and two steps back. She'd give me a new piece of code to fix a problem, but that in turn would break something else. In the
end, I got to a point where it felt like I was going around in circles. There was an issue that would arise with logging in, but
the solution ChatGPT provided would then break my get_temperature method, and the fixes for that would ultimately break
the logon method again. I tried asking for what I wanted in different ways, but it didn't seem to help. Sometimes ChatGPT
would provide me with code as a solution, but when I check it was identical to the previous version.
I decided I needed a new approach.  I realised that what I'd been doing was a bit piecemeal.  I'd started off with a pretty vague request, and ChatGPT had done her best to give me what I wanted.  As I made more and more changes, I think a lot of the context got lost along the way.   I lead Innovation projects for a living, so I understand the importance of giving a developer clear requirements.  I've also used ChatGPT enough to know that giving her a role as part of a prompt typically improves the responses.  So I provided a new prompt:

Ok we seem to just be going around in circles. Lets try a new approach. The file CosyLogin.py is a Python script that I wrote which allows you to control a heating thermostat using their web api. You are a skilled python programmer, I would like to use this script to create a Home Assistant integration. Please can you write all of the files I need to do this. The requirements are as follows:

  1. All settings to install the integration must be accessible from the UI (ie. not using configuration.yaml)
  2. It must have a unique ID so that it can be managed in the UI once installed
  3. It must be able to get both the current temperature and the current preset mode
  4. It must be able to set the current preset mode, these are slumber, comfy and cosy
  5. It must be able to set the target temperature for each of the preset modes
  6. It must allow us to switch hiberate mode on and off
  7. If hibernate mode is on and the user tries to set one of the preset modes, it must first switch hibernate mode off
  8. It must be controllable via a lovelace UI card which shows the current temperature, the current preset mode, and allows the you to set the target temperature for the current preset mode.
Ok we seem to just be going around in circles. Lets try a new approach. The file CosyLogin.py is a Python scriptthat I wrote which allows you to control a heating thermostat using their web api. You are a skilled python  programmer, I would like to use this script to create a Home Assistant integration. Please can you write all of the filesI need to do this.
The requirements are as follows: All settings to install the integration must be accessible from the UI (ie. not using configuration.yaml) It must have a unique ID so that it can be managed in the UI once installed It must be able to get both the current temperature and the current preset mode It must be able to set the current preset mode, these are slumber, comfy and cosy It must be able to set the target temperature for each of the preset modes It must allow us to switch hiberate mode on and off If hibernate mode is on and the user tries to set one of the preset modes, it must first switch hibernate mode off It must be controllable via a lovelace UI card which shows the current temperature, the current preset mode, and
allows
you to set the target temperature for the current preset mode.

ChatGPT came back with:

Sure, let's create a Home Assistant integration based on your CosyLogin.py script. We'll break this down into
several files:
manifest.json - Metadata about the integration.
config_flow.py - Handles the configuration flow for the integration. const.py - Constants used in the integration. api.py - Handles the communication with the Cosy API. climate.py - Defines the climate entity for Home Assistant. __init__.py - Initializes the integration.
And the proceeded to give me the files. Again, I dutifully uploaded them to my HA dev environment and tested them.
There were errors again, but this time we were much closer. We had a UI card with the current and target temperatures,
there were drop-downs with the pre-set and HVAQ modes and it was retrieving live data. We again needed the
back-and-forth around errors in the logs, and I had to remind her of some of the initial requirements. Along the way I'd
picked up bits and pieces about how the HA components worked so I was able to provide better guidance about how issues
might be fixed. I also found that providing text and code examples from the HA documentation as context made things go
much more smoothly. And then, after maybe an hour, I had exactly the integration I had been hoping for. It worked
perfectly and had all the functionality I wanted. I give you geo_cosy:


If, like me, you don't code very often, ChatGPT makes an excellent copilot. It can be frustrating at times, but I
discovered that by following some rules, you can make things much better. Here are some guiding principles that I found
useful:

1. Be clear about what type of developer you want ChatGPT to be
2. Provide clear requirements, tell her exactly what you want and break down the elements
3. Context is important. If an issue might be related to multiple files, give her the code from all of the files as context
4. If the conversation goes on for a while, it can be a good idea to remind ChatGPT of the content of all of your files from
    time to time to ensure she is always taking everything into account in her answers.
5. Give her some help. Sometimes going off to the documentation and providing the information for a certain component
can give you a faster and better solution to an error.
6. Say please and thank you. It's only a matter of time. And I for one welcome our robot overlords.

If you'd like to view the Cosy integration you can do so here:

Have you used ChatGPT to help with your coding?  Has it allowed you to build something you would have had the skill or time to build otherwise?  Let me know in the comments.

TechShot: This Weeks Key Digital Innovation Stories (WE 04/01/2024)



What I love about this time of year, is that everyone seems to be looking forward and imagining what's to come for the year ahead.  This is exactly the type of mindset we try to build within innovation, so seeing people discussing how digital trends might impact us, and provide genuine value gives me a real boost. 

With that in mind, this week's TechShot summarises some of the key predictions being made about the direction of travel for digital innovation in 2024:


AI and Emerging Tech Unveiled at Davos 2024 (World Economic Forum):

Davos discussions exceeded the hype, revealing a consensus on AI and other crucial issues such as the digital divide, quantum economy, space tech, climate change, and AI governance. The article emphasizes the shift towards a mindful AI approach, emphasizing its application across sectors while prioritizing people.

Dive into the full article


2024's Top 10 Tech Trends: A Glimpse into the Future (BlueSoft):

Gartner's compilation spotlights trends like AI integration, sustainable tech, and democratized generative AI, transcending borders to impact industries worldwide.

Explore the complete article


Seven Game-Changing Technologies in 2024 (Nature):

Nature identifies pivotal technologies including protein engineering, 3D printing, and deepfake detection, with far-reaching global implications shaping our scientific landscape.

Read the full insights here


Decoding 10 Breakthrough Technologies of 2024 (MIT Technology Review):

Significance: MIT Technology Review's annual list spans biotechnology, AI, computing, robotics, and climate tech, unleashing breakthroughs that transcend borders and drive innovation worldwide.

Delve into the breakthroughs


What do you think?  Are these the technologies and transformations that will define 2024?  What are you excited to see pan out over the course of this year?  Let me know in the comments.

Unleash the Magic: A Step-by-Step Guide to Crafting Effective Generative AI Prompts




Generative AI is a powerful tool, but its magic lies in crafting the right prompt. Think of it as the conductor's baton, guiding the AI orchestra towards your desired creative vision. So, how do you become a maestro of prompts? I've recently been doing a lot of work with Generative AI having completed a number of Google AI certifications.  As a result I've learnt a fair bit about the dos and don'ts, so I've put this guide toether to help you create the most effective prompts you can to get great result.  Let's delve into the essential steps and explore the "why" behind each one:


Step 1: Define Your Goal with Laser Focus 

Imagine entering a restaurant without knowing what you crave. Similarly, a vague prompt leaves the AI guessing. Be specific! Knowing your goal (poem, script, code snippet, etc.) sets the foundation, helping the AI tailor its response to your intended format and purpose.


Good Prompt: "Write a suspenseful short story (around 1000 words) about a time traveler trapped in the past, using elements of historical fiction."


Bad Prompt: "Make me something cool." (This leaves the AI lost in a sea of possibilities, potentially missing your mark.)


Step 2: Paint a Vivid Picture with Context

Think of this step like setting the scene for a movie. Provide details about the world, characters, and situation. The richer the context, the better the AI can understand the relationships, motivations, and overall atmosphere you envision.


Good Prompt: "In a dystopian future where corporations rule, a group of rebels led by a charismatic hacker plans a daring heist to steal sensitive data from the megacorporation headquarters. Describe their tense infiltration under heavy security."


Bad Prompt: "People doing something in a place." (This lacks the specifics that bring your scenario to life, hindering the AI's ability to generate a truly immersive response.)


Step 3: Be the Director, Not Just the Producer

Imagine wanting a specific genre of music but only telling the musician to "play something." With generative AI, you're both the producer and the director. Specify the desired tone (serious, humorous, etc.), style (formal, informal, etc.), and even length to guide the AI towards the specific output you have in mind.


Good Prompt: "Write a humorous blog post in a conversational tone, targeting tech enthusiasts, about the latest developments in virtual reality, aiming for a length of around 500 words."


Bad Prompt: "Write a tech article about VR." (This leaves the AI unsure of the intended tone, style, or target audience, potentially resulting in a mismatched output.)


Step 4: Show, Don't Just Tell, with Examples 

Think of this as providing reference photos to an artist. Share examples of similar content (poems, scripts, code, etc.) you like, highlighting specific elements you want the AI to incorporate. This gives the AI a concrete understanding of your preferences and desired style.


Good Prompt: "Generate a poem in the style of Emily Dickinson, similar to her 'Hope' poem, exploring the theme of resilience in the face of adversity."


Bad Prompt: "Write a sad poem like Dickinson." (Without a specific reference, the AI might miss the nuances of Dickinson's style and tone, leading to a poem that doesn't capture the intended essence.)


Step 5: Remember, Iteration is Your Friend 

Don't expect perfection on the first try. Experiment with different phrasings, adjust details, and see how the AI responds. Each iteration is a learning opportunity, helping you refine your prompt and guide the AI closer to your creative vision.


Bonus Tip: Don't be shy to explore existing prompt libraries and communities. Learn from others' successes and failures to enhance your own prompt-crafting skills.  GoDaddy have a library you can explore, its aimed primarily at small business users, but its equally as useful for larger enterprises.  You can find it Here


By following these steps and understanding the "why" behind each one, you'll transform from a novice prompt writer to a confident conductor, wielding the power of generative AI to bring your unique creative visions to life. So, grab your metaphorical baton and start composing!

Human First: 5 Killer Design Thinking Resources


In the ever-evolving landscape of business and technology, the traditional approach to problem-solving is undergoing a profound transformation. Design Thinking has emerged as a potent methodology, placing people at the forefront of design and innovation. It represents a shift from a solution-centric to a human-centric mindset, recognizing that understanding the needs, aspirations, and challenges of individuals is paramount to achieving true success and delivering genuine value to customers.

To help you understand and deliver the impact of human-centric design for your organisation, here are some amazing online resources that will elevate you to a hemp-clad, mystical CX guru in no time at all.

1.  Interaction Design Foundation (IDF): Design Thinking Guide

Interaction Design Foundation Logo
If you're new to Design Thinking, and want to understand what it is, and how to start using it, this is a great place to start.  The Interaction Design Foundation is renowned for its commitment to providing quality education in design and usability. Their Design Thinking Guide is a robust web page offering a structured overview of the methodology. From understanding the core principles to exploring the various stages of the process, IDF's guide provides a solid foundation for beginners and serves as a quick reference for experienced practitioners.
Link: IDF Design Thinking Guide

 2.  Make:Iterate: Design Thinking Case Studies

Just the fact that you're here, means you probably already realise the power of taking a human-centric approach to design and innovation.  However, getting the support and investment from leaders to move to new approaches can sometimes be challenging.  This isn't surprising given the number of new and shiny supposed silver bullets everyone tries to sell us on what seems like a      weekly basis.  So how can you convince the people that matter that this is not just a fad? What can really help is real world examples of how Design Thinking has been deployed, and the impact it can bring. Make:Iterate have put together alist of 6 practical examples of Design Thinking in action, which can help you build the business case, and bring your organisation's leaders along with you.

Link: Make:Iterate DT Case Studies 

3.  Green Dot: Design Thinking Tools and Templates


So, you've learnt what Design Thinking is all about, and you've got your team and the company's decision makers on board.  Now it's time to kick off your first Design Thinking project.  Green Dot Consulting Group provide an excellent library of Design Thinking tools and templates to help you on your way.  Need to plot out your customer journey map?  Want to build needs and requirements in a 'How Might We' Exercise?  Green Dot have you covered.

Link: The Green Dot: Tools & Templates  

4.  The Argonauts: Design Thinking Playlist

A great companion to the Green Dot's Templates is The Argonauts Design        Thinking playlist on Youtube.  In this series of videos they walk you                  through examples of how to make use of many of the templates and tools          on the Green Dot site (as well as some additional ones).  If you find video easier to follow than reading through wordy websites, this is a great place to start.

Link: The Argonauts: Design Thinking Playlist

 5.  The Big Bang Partnership: Digitising The Process

Sometimes an analog approach can provide great benefits when undertaking Design Thinking sprints.  Most Design Thinking practioners will be used to rooms filled with Post-It notes and hand-drawn prototypes.  Digital tools can have their place though, particularly where you need to integrate into existing digital workflows, or where you need to collaborate amongst a distributed team.  The Big Bang Partnership have produced a very comprehensive list of some of the most effect digital tools which you can use for your Design Thinking projects.  What's nice about this list is that they also explain how they can be used to provide value in each of the Design Thinking Phases.

                                                  Link: The Big Bang Partnership: Digital DT


 Bonus Resource:  Change By Design, Tim Brown

"Change by Design" by Tim Brown is a best-selling, seminal work that explores the principles and applications of Design Thinking in driving innovation. Brown, the CEO of IDEO, a renowned design and innovation consultancy, provides a compelling framework for problem-solving and creativity. The book emphasizes the human-centric nature of Design Thinking, advocating for empathy, collaboration, and experimentation in the design process. Through engaging case studies and real-world examples, Brown illustrates how integrating Design Thinking into organizational culture can lead to transformative change and sustainable success. "Change by Design" serves as a guide for leaders, designers, and innovators seeking to harness the power of creative problem-solving to address complex challenges and foster a culture of innovation within their teams and organizationsYou can get this as a Free audiobook with a trial of Audible using this link: Change By Design