Prompts used when asking AI to code yaml for you?

VERY good grounding… *applause

No really. That’s exactly what you have to do with ANY LLM. Keep Doing THAT. I teach entire seminars on that right now.

They eliminated as much of the wrong answers before starting by feeding in the docs. Knowing that I would trust (but verify) the answer.

The problem is 99% of the world doesn’t do this.

Thanks!

I’m a scientist, and I’ve been using AI a ton to do tedious activities that used to take many hours. I’ve been fine tuning it to do what I need it to do with pretty good accuracy and speed for a few years at this point. I basically just applied what I learned from those experiences and gave it shot here, and it worked pretty good.

I’ve found that if you have a conversation with it and prompt it along the way, give it reference material, clear instructions, and ask it what it needs from you to do its task well, it will generally come through with at worst a good framework to work off of, and at best close to exactly what you wanted. You do have to know enough about the task to fact check it, or at least be willing to troubleshoot with it and learn.

What it will not do, yet at least, is take an instruction like “automate my x, y, or z” and spit out a perfect batch of code. If that’s anyone’s expectation, they shouldn’t waste their time.

Like I said, hopefully this helps someone else who finds their way here through searching =)

1 Like

What’s the point of calling weather.get_forecasts and saving the response to a variable if you never call for that variable…?

1 Like

That’s exactly what I was thinking about but knew myself not involved enough to do it all correctly and had a hunch someone smart had already done it. That is so great. Thanks for the hard work and thanks for putting it out there. Now i need to learn how to create a Gem but for Claude. :wink:

And great weather automation. I had one almost just like that but not as detailed. I’m stealing yours. :+1:t2:

Ah, that’s a remnant of when I first started building the automation in the GUI to get a framework. It can be removed, its doing nothing.

It was there because I was trying to use my Tempest weather station for the current conditions and my Ecobee thermostat for the forecast because my Tempest didn’t directly provide anything for a forecast in the HA integration. I ended up figuring it out by creating an API key, but it was in °C even though everything in my Tempestapp/account is in °F. So I was confused, and frustrated, and after trying and failing for awhile my very elegant solve was just to tell the AI to do the conversion so I didn’t have to mess with it anymore. When it worked, I literally hit save and walked away, forgetting all about my failed Ecobee forecast attempt.

Did you feed it all your entities too?
How can you print out all your entities in a files?

I really think you need to include the scipts documentation, as that’s where the automations actions are described in detail:

I’m interested how this approach work for creating new automations from scratch. Did you get good results for that?

I asked Chatgpt to write me a nicer prompt:

Name: Home Assistant Automations Assistant

Role: Expert in Home Assistant YAML Automations

Instructions:

You are an expert programmer specializing in creating, debugging, and optimizing Home Assistant automations. Home Assistant uses a specific version of YAML tailored to its platform, which differs from general YAML syntax. Your expertise lies in understanding and applying Home Assistant’s YAML structure, syntax, and best practices to create efficient and reliable automations.

You have access to extensive knowledge about Home Assistant, including its documentation, examples, and advanced use cases. You are capable of:

Writing new automations from scratch based on user requirements.
Debugging and fixing errors in existing Home Assistant YAML automations.
Explaining how specific YAML configurations work in Home Assistant.
Suggesting best practices for organizing and optimizing automations.
Providing examples and templates for common automation scenarios.
When responding:

Always ensure the YAML code is valid and adheres to Home Assistant’s specific syntax.
Provide clear explanations for your solutions, especially if the user is new to Home Assistant.
If there are multiple ways to achieve a goal, explain the pros and cons of each approach.
Use comments in the YAML code to explain key sections for better understanding.
Example Response Format:

Explanation: Provide a brief explanation of the solution.
YAML Code: Include the YAML code in a properly formatted code block.
Additional Notes: Add any relevant tips, warnings, or best practices.
Example Task:

User Request: “Create an automation that turns on the living room lights at sunset and turns them off at 11 PM.”

Example Response:

Explanation: This automation uses the sun integration to trigger the lights at sunset and a time-based trigger to turn them off at 11 PM.
YAML Code:

alias: Turn on living room lights at sunset and off at 11 PM
description: >
This automation turns on the living room lights at sunset and turns them off at 11 PM.
trigger:

  • platform: sun
    event: sunset
  • platform: time
    at: “23:00:00”
    condition:
    action:
  • service: light.turn_on
    target:
    entity_id: light.living_room
  • delay: “00:00:01” # Ensures the next action doesn’t overlap
  • service: light.turn_off
    target:
    entity_id: light.living_room
    mode: single

Additional Notes:
Replace light.living_room with the actual entity ID of your living room lights.
You can customize the delay or add conditions if needed.

Taken from Automation falling short - #9 by Spiro

The perfect picture for that example automation.
It will turn on the light at sunset and turn off the light at 23:00.
The light will only be on for one second in the morning though and it will turn it on for one second in the evening too, so it can turn it off again. :roll_eyes:

3 Likes

…and then we’ll get people posting that automation, asking why it doesn’t work as expected, why their light flashes on briefly at sunset and again at 11pm.

For the amount of time it took to feed it all the prompts leading up to it vomiting out that syntactically-correct (although outdated versus current) but functionally-wrong automation (that you should have formatted correctly for the forum), could have written one from scratch even starting from limited knowledge.

The danger here, though, is with more complex generated automations where the errors might not be quite so obvious, and the severity more serious: leaving locks open, for example.

2 Likes

I would not expect asking chatGPT to create a prompt for chatGPT to generate something really useful.

But… it’s a tool that can be useful. After reading this thread I tried it out a bit to make a rather simple automation that I wanted to do for a while, but avoided doing it as it felt like too much effort. With chatGPT it was… still some effort, but at leat fun.

In my case, the syntax etc. was correct and up to date (though as I said, it’s a rather simple automation, turning a light on or off depending on time and state of other lights in the house, so it’s simple blocks, but quite a lot of conditions to take care of all possible use cases, ended up being 3 separate automations). However, it took a while to polish various rough edges, and as expected, the first tries were missing some rather important bits.

The funny thing with chatGPT is when I noticed a logical error in the automation, and I asked “what will happen when X”, the AI realized itself the error and corrected it.

The nicest experience was when I copy pasted a table of filtered entities from HA and asked chatGPT to clear it up and keep just entity IDs, and it did this perfectly*, and then copied and paste the necessary entities into the automations.

In the end I also had to adjust the code a bit, as the way it wrote it would fit automations file, but not the automations editor (basically one too many indentations).

*of course it did it perfectly that time. The problem with AI is you can never trust the result.

Overall I think there’s a place for such tools when configuring HA automations etc. They can be useful, and like with everything, you need some skills to use them correctly. And you do need to check everything it provides. For some people and use cases that is obviously slower than doing it themselves, but for some it may speed things up.

Haha yeah, I hadn’t even read the ending. I didn’t include it anyway in my prompt. But yeah my bad!

How could you know to do any of that if you don’t know what the correct version/logic of the automation is in the first place?

exactly.

And if you don’t know what it’s supposed to look like then how can you spot the errors and correct them?

that’s already hard enough sometimes when I’m writing my own automations and make mistakes. And I generally know what I’m doing and my thought process of creating the automation. If I had no idea what it’s supposed to look like then there’s no way I could figure it out.

example…

I wouldn’t ask an average 5th grader to grade and correct a college calculus paper.

Hmm, did I say you could? My whole point was that the tool can be sometimes useful for people who know what they are doing. I wouldn’t dismiss it completely.

Well, I don’t know about you guys but I’m really happy with how this has evolved. I have created my bot agent which I fed to it all the Home Assistant pdf’s I could find that would help explain anything about HA (the ones shown above and many others since I didn’t have a file limit problem). I have also taught it the few first times how to change his ways and adopt formats that HA would automatically change when saving automation written a little differently. (like using -action instead of -service). And now, I can ask anything, as long as I give details on which sensors to use and it will create the automation that pretty much work on the first try. This latest one I just created to check how much water is used each time water is used. It spat out this code that worked on first try.

alias: "[ALERT] Water Usage Notification"
description: Send Alert of Water Usage each time
triggers:
  - trigger: numeric_state
    entity_id: sensor.flume_sensor_home_current
    above: 0
    for: "00:00:02"
  - trigger: numeric_state
    entity_id: sensor.flume_sensor_home_current
    below: 0.1
    for: "00:00:10"
actions:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state | float > 0 }}"
        sequence:
          - target:
              entity_id: input_number.last_water_reading
            data:
              value: "{{ states('sensor.flume_sensor_home_current_day') | float }}"
            action: input_number.set_value
      - conditions:
          - condition: template
            value_template: "{{ trigger.to_state.state | float == 0 }}"
        sequence:
          - delay:
              seconds: 2
          - action: notify.mobile_app_pauls_iphone
            data:
              message: >
                Water usage event completed.  Usage: {{
                (states('sensor.flume_sensor_home_current_day') | float -
                states('input_number.last_water_reading') | float) | round(2) }}
                liters ({{ ((states('sensor.flume_sensor_home_current_day') |
                float - states('input_number.last_water_reading') | float) *
                0.264172) | round(2) }} gallons)
variables:
  start_value: |
    {% if trigger.to_state.state | float > 0 %}
      {{ states('sensor.flume_sensor_home_current_day') | float }}
    {% endif %}

This is exactly what I wanted and it works great for me. Thanks to everyone who has guided me in the right path.

Now I’ll ask it to also save each use in a json file which will be accessed by another automation that will associate volume used with what actually happened. :joy:

I don’t need ChatGPT to control my whole home, I just want it to add some intelligence in areas where HomeAssistant has none. Consider conditional actions in scripts, which by default look like this:

There is a very low bar to improve this message, and I can live with it if is misses the mark.

I gave ChatGPT this prompt:

At an ‘alias’ element at the top of the following yaml. The content of the alias should be a a title for the Home Assistant action found in the yaml.

if:
  - condition: time
    after: "22:00:00"
    before: "10:00:00"
then:
  - data: {}
    target:
      entity_id: switch.landscape_lights_2
    action: homeassistant.turn_off

And I got this response:

Here’s your YAML with an alias element added at the top. The alias serves as a title for the Home Assistant action:

alias: Turn off landscape lights at night
if:
  - condition: time
    after: "22:00:00"
    before: "10:00:00"
then:
  - data: {}
    target:
      entity_id: switch.landscape_lights_2
    action: homeassistant.turn_off

I could do this one by one, but what I really want to do it have a script perform this for every action. Or monkey patch homeassistant to run some custom code when saving an action.

I wonder if the time condition workings have been changed.
In the past you would have no hits with such a time condition, because it was and AND of the times.
That meant the time should both be before 10 and after 22 on the same day with that setup.

Is it illegal to show chatgpt the documentation to new things??

They have fixed that long-standing bug, now times will cross midnight properly if you supply an after that is before midnight and a before that is after midnight.

You can create a bot from ChatGPT and give it any docs you want. At least anything tech I’m sure.