Prompts used when asking AI to code yaml for you?

K, I get what you guys are saying. But I think of it differently because I know that his first iteration will never be the correct one but that it will be a very nice starting point to go from there.
For example, I just asked it to put together a list of sensors to use with the Stanley chart card and I gave it a list of sensors, a sensor template and then it’ll spit out this type of code:

- platform: template
sensors:
  major_appliances_power:
    friendly_name: "Major Appliances Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.tesla_usage')|float(0) + 
         states('sensor.washer_usage')|float(0) + 
         states('sensor.dryer_usage_3')|float(0) + 
         states('sensor.water_heater_usage')|float(0) + 
         states('sensor.vacuum_2_usage')|float(0) }}

  heating_cooling_power:
    friendly_name: "Heating & Cooling Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.ac_usage')|float(0) + 
         states('sensor.heat_2_usage')|float(0) + 
         states('sensor.heat_11_usage')|float(0) + 
         states('sensor.heat_14_usage')|float(0) + 
         states('sensor.juliettes_heater_usage')|float(0) + 
         states('sensor.juliens_heater_usage')|float(0) + 
         states('sensor.garage_heater_usage')|float(0) + 
         states('sensor.heating_blanket_nath_usage')|float(0) + 
         states('sensor.black_hair_dryer_usage_2')|float(0) }}

  kitchen_appliances_power:
    friendly_name: "Kitchen Appliances Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.coffee_maker_usage')|float(0) + 
         states('sensor.coffee_grinder_usage')|float(0) + 
         states('sensor.kettle_usage')|float(0) + 
         states('sensor.rice_cooker_usage')|float(0) + 
         states('sensor.microwave_usage')|float(0) + 
         states('sensor.disposal_usage')|float(0) + 
         states('sensor.toaster_usage')|float(0) + 
         states('sensor.main_oven_usage')|float(0) + 
         states('sensor.toaster_oven_usage')|float(0) + 
         states('sensor.fruit_juicer_usage')|float(0) }}

  electronics_power:
    friendly_name: "Electronics Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.3d_printer_usage')|float(0) + 
         states('sensor.brother_laser_printer_usage')|float(0) + 
         states('sensor.laser_printer_2_usage')|float(0) + 
         states('sensor.laser_printer_4_usage')|float(0) + 
         states('sensor.sony_projector_usage')|float(0) + 
         states('sensor.julien_desk_usage')|float(0) }}

  lighting_power:
    friendly_name: "Lighting Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.light_2_usage')|float(0) + 
         states('sensor.light_15_usage')|float(0) + 
         states('sensor.master_bedroom_lights_usage')|float(0) + 
         states('sensor.red_lamp_usage')|float(0) + 
         states('sensor.living_room_lights_usage_3')|float(0) + 
         states('sensor.juliettes_room_lights_usage_3')|float(0) }}

  always_on_power:
    friendly_name: "Always On Power"
    unit_of_measurement: 'W'
    value_template: >
      {{ states('sensor.always_on_usage')|float(0) + 
         states('sensor.device_2_usage')|float(0) + 
         states('sensor.plug_2_usage')|float(0) + 
         states('sensor.plug_3_usage')|float(0) + 
         states('sensor.plug_4_usage')|float(0) + 
         states('sensor.other_usage')|float(0) }}

I didn’t ask him to classify them like this. It decided to do it. I liked it. In this case I didn’t have to fix anything.

I’ve noticed that most times, if I tell it what template to use then it hits pretty close to what it’s supposed to be.

I realise English is probably not your first language but we refer to objects like LLMs as “it” not “him” or “he”.

And those entities the LLM generated for you are using the legacy template platform (because that is likely all it was trained on). You should be using the newer template integration for new sensors. While still supported the legacy template platform will not be getting new features. Like state_class that allows you to generate long term statistics so you can see data from longer than 10 days ago.

1 Like

GitHub Copilot says it’s aware of Home Assistant configuration syntax as of October 2023. FWIW.

As mentioned earlier in the thread, the training for those AI is done with old documentation.
HA is developing extremely fast and if you ask for guides on the forum you will usually get a comment about only using guides made within the last 6 months if possible and never guides older than an year old, because guides older than that are usually outright wrong due to the evolution of HA.
Training an AI takes time and keeping up with a fast evolution, like the one HA is on, is near impossible.

It is not just getting the new information into the AI model, which can be hard enough, but more importantly it is getting the old obsolete and wrong information out of the AI too, which is the hardest part.

1 Like

I pretty much achieved this just now, after coming across this thread in my search to see if anyone has figured it out yet - maybe this will help you.

I will start off by saying that I’ve only tested this using Google Gemini Advanced. And I did it by creating a custom gem that essentially turned Gemini into a coding expert for Home Assistant specific YAML. I’m pretty sure the subscription for Gemini Advanced is required for custom gems. And I’m not sure which other AI’s have whatever the equivalent to Gemini Gems are, so your mileage may vary.

I gave it the following instructions when creating the custom gem:

Name: Home Assistant Automations
Instructions: You are an expert programmer specializing in Home Assistant automations. Home Assistant uses YAML for its automations, but it uses a version specific to itself that isn’t always the same as general YAML code. Therefore, you have become an expert in the Home Assistant YAML programming language by referencing the attached knowledge documents.

I then went to the Home Assistant documentation and created reference documents for Gemini to use when interacting with this Gem. I created 10 of them, as that is the attachment limit.

Here are the one’s I chose and how I did it:

I right clicked on the page, chose print, and saved as a PDF. I named them whatever the file defaulted to, but I added a numerical prefix, i.e. 01 - filename.pdf, 02 - filename.pdf, 03 - filename.pdf, etc. I don’t know if this matters, but I wanted to be as helpful to the AI as possible because in my experience that gives you better results.

I then attached the pdf files I created into the “Knowledge” section in the custom gem creator and hit save.

This is the first prompt I gave it to test it out:

I have a daily weather report automation set using the following YAML code:

alias: Weather Update - Weekday Morning
description: >-
  Announces a weather update from the Tempest Weather Station at 6 AM on
  weekdays on Google Home Speakers with a random accent.
triggers:
  - at: "06:00:00"
    trigger: time
conditions:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
actions:
  - target:
      entity_id:
        - media_player.googlehome6016
        - media_player.master_bedroom_speaker
        - media_player.kitchen_display
    data:
      volume_level: 0.5
    action: media_player.volume_set
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: daily
    response_variable: daily_forecast
    target:
      entity_id: weather.downstairs_thermostat
  - action: conversation.process
    metadata: {}
    data:
      agent_id: conversation.chatgpt
      text: >-
        You are an advanced smart home with a built in personal weather station
        and multiple personalities, which you will introduce each time with a
        random name inspired by real-life female weather television
        personalities. The names should be clever, witty, and sophisticated
        while avoiding being cheesy. They should also be culturally appropriate
        based on the accent chosen each time. Weather updates will be spoken out
        loud and should always be funny and creative. Each personality will have
        a different sense of humor that will tie into their name. Two 41 year
        old adults will be the only ones listening, so it does not need to be
        safe or appropriate for children. Please use whole numbers and report
        the temperatures in farenheit. All of the forecast data is given in
        celsius by the weather station and will need to be converted to
        farenheit before the weather report is given. Try to make it amusing!
        This should be less than 1 minute in length. \n Current Conditions are
        below\n  Current time is {{ now()}} Temperature is {{
        states('sensor.st_00133566_temperature') }}  Feels like temperature is
        {{ states('sensor.st_00133566_feels_like') }}  Humidity is {{
        states('sensor.st_00133566_humidity') }}  UV Index is {{
        states('sensor.st_00133566_uv_index') }}  Current Rain Intensity {{
        states('sensor.st_00133566_precipitation_intensity') }}  Forecasted
        Conditions for Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].conditions }}  Forecasted High Tempterature for
        Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].air_temp_high }}  Forecasted Low Temperature for
        Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].air_temp_low }}  Forecasted Precipitation
        Probability {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].precip_probability}}
    response_variable: agent
  - variables:
      accent_list:
        - en-US
        - en-GB
        - en-AU
        - en-IE
        - en-NZ
        - en-CA
        - cy-GB
  - data:
      cache: false
      entity_id:
        - media_player.googlehome6016
        - media_player.master_bedroom_speaker
        - media_player.kitchen_display
      language: "{{ accent_list | random }}"
      message: "{{agent.response.speech.plain.speech}}"
    action: tts.cloud_say
mode: single

I would like to convert this into a script to then expose to my google assistant, and then import into the google home app so it can be activated by a voice command. can you help me with this task?

This is the response it spit out:

Certainly, I can help with that!
**Understanding the Goal**
You want to transform your existing Home Assistant automation into a script that can be:
1. Executed on demand via a voice command to your Google Assistant.
2. Imported into the Google Home app for easy access and activation.
**Steps to Create and Integrate the Script**
1. **Create the Script:**
  * In your Home Assistant UI, navigate to **Settings > Automations & Scenes > Scripts**.
  * Click the "**+ Create Script**" button.
  * Give your script a name (e.g., "Weather Report").
  * In the script editor, paste the actions section from your existing automation YAML code. This will ensure the script performs the same weather report generation and announcement.
sequence:
  - target:
      entity_id:
        - media_player.googlehome6016
        - media_player.master_bedroom_speaker
        - media_player.kitchen_display
    data:
      volume_level: 0.6
    action: media_player.volume_set
  - action: weather.get_forecasts
    metadata: {}
    data:
      type: daily
    response_variable: daily_forecast
    target:
      entity_id: weather.downstairs_thermostat
  - action: conversation.process
    metadata: {}
    data:
      agent_id: conversation.chatgpt
      text: >-
        You are an advanced smart home with a built in personal weather station
        and multiple personalities, which you will introduce each time with a
        random name inspired by real-life female weather television
        personalities. The names should be clever, witty, and sophisticated
        while avoiding being cheesy. They should also be culturally appropriate
        based on the accent chosen each time. Weather updates will be spoken out
        loud and should always be funny and creative. Each personality will have
        a different sense of humor that will tie into their name. Two 41 year
        old adults will be the only ones listening, so it does not need to be
        safe or appropriate for children. Please use whole numbers and report
        the temperatures in farenheit. All of the forecast data is given in
        celsius by the weather station and will need to be converted to
        farenheit before the weather report is given. Try to make it amusing!
        This should be less than 1 minute in length. \n Current Conditions are
        below\n  Current time is {{ now()}} Temperature is {{
        states('sensor.st_00133566_temperature') }}  Feels like temperature is
        {{ states('sensor.st_00133566_feels_like') }}  Humidity is {{
        states('sensor.st_00133566_humidity') }}  UV Index is {{
        states('sensor.st_00133566_uv_index') }}  Current Rain Intensity {{
        states('sensor.st_00133566_precipitation_intensity') }}  Forecasted
        Conditions for Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].conditions }}  Forecasted High Tempterature for
        Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].air_temp_high }}  Forecasted Low Temperature for
        Today {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].air_temp_low }}  Forecasted Precipitation
        Probability {{ state_attr('sensor.tempest_daily_forecast',
        'forecast').daily[0].precip_probability}}
    response_variable: agent
  - variables:
      accent_list:
        - en-US
        - en-GB
        - en-AU
        - en-IE
        - en-NZ
        - en-CA
        - cy-GB
  - data:
      cache: false
      entity_id:
        - media_player.googlehome6016
        - media_player.master_bedroom_speaker
        - media_player.kitchen_display
      language: "{{ accent_list | random }}"
      message: "{{agent.response.speech.plain.speech}}"
    action: tts.cloud_say
alias: Current Weather
description: ""

Gemini then gave me instructions to make the script work with a Google Home speaker, which is a lot of text to copy and paste so I’ll just link the instructions someone else gave here that are perfectly accurate:

I’ve gotten Gemini to write me some other automations that were less complex and it did a wonderful job.

Quick example:

alias: Turn Off Lights and Arm Alarm When Nobody is Home
description: Turn Off Lights and Arm Alarm When Nobody is Home
mode: single
triggers:
  - entity_id: zone.home_2
    to: "0"
    trigger: state
conditions: []
actions:
  - data: {}
    target:
      entity_id:
        - light.all_interior_lights
        - switch.all_interior_light_switches
    action: homeassistant.turn_off
  - metadata: {}
    data:
      mode: away
      skip_delay: false
      force: false
      entity_id: alarm_control_panel.alarmo
    action: alarmo.arm

I hope this works as well for you as it did for me.

2 Likes

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: