Problem? Getting the best solution from AI

First of all… please don’t use AI.

You’ll learn much more by researching the docs and the forum and figuring it out for yourself. This is important because

  • You can’t get the best out of AI and recognise duff results if you don’t understand what you’re asking for. AI can speed up coding dramatically, but it can’t replace skills.
  • You’ve got to maintain this stuff.

Of course, you’re going to use AI anyway.

And why not? A great many people don’t care how HA works, they just want the lights to go on and off.

Here are some tips for using publicly available services.

Just to be clear. This post is about using AI as a tool in setting up and troubleshooting HA. It is not about integrating AI into Home Assistant - although many of the same principles apply.

A reminder: Do not use AI to post in the forum. It will be deleted.

This topic is part of the community-driven cookbook where you can find other topics you might be interested in or might like to contribute to.

Choose an AI that shows its sources

Perplexity is a good example. Its responses are annotated and it displays a list of sources.

You’ll notice that most of them are from this forum. You could have searched here.

Choose an AI that allows follow-up questions

You’re unlikely to get what you want first go - you need to be able to refine the prompt without re-typing the whole thing. Sometimes just “No, that’s not correct” gets better results (or at least an apology!)

Check the training date

The simplest way is to ask it. Many free services are oblivious to anything that has happened in the last few years. The default OpenAI model used by the Home Assistant integration (gpt-4o-mini), for example, has a cut-off date of October 2023, making it fairly useless.

Temperature

You can also try asking your LLM what its temperature setting is.

Temperature controls the randomness of text generation. Higher values make responses more varied and “creative”, while lower values make them more focused. A higher setting also makes it less likely that responses will be correct. Accuracy is relatively high at settings of 0 and 1, above that the LLM will become more and more inventive.

If you’re using a public service you won’t have any control over temperature, but it’s good to know. As a practical test, ask the same question several times. If you get different answers, you’ve got a relatively high setting.

Always give a cut-off date

The last sentence in your prompt should always be “Do not use sources more than 12 months old”. Even a year is quite a long time in Home Assistant - but forum posts from 2020 are less likely to be useful, and may actually be misleading.

Give plenty of context

An LLM will not follow links, either in the prompt or in any documents it may be aware of. If you want to include information from a web page in your prompt, you have to copy and paste the text.

In your questions, you need to give as much context as possible.

The problem in the example that follows is to extract a weather alert summary from among a sensor’s attributes. The sensor’s value is a number - the number of alerts currenly active. There is a list of attributes for each alert. We’re looking for the first “summary” attribute from the first alert.

You could ask something like…

How do I get an attribute value from a nested list

But it might take a while to home in on an answer that works.

A better AI prompt

In Home Assistant…

Obvious to you, but not necessarily to the AI.

I want to create a markdown card showing a weather alert summary

Specific details of what you’re trying to achieve. This will help the AI find relevant sources.

The weather alert entity is sensor.weather_alerts. Its attributes are:

entries:
  - title: Yellow warning of thunderstorm affecting South West England
    title_detail:
      type: text/plain
      language: null
      base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
      value: Yellow warning of thunderstorm affecting South West England
    links:
      - rel: alternate
        type: text/html
        href: >-
          https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2025-06-13&id=c888f83d-1a97-42f5-8643-a89c060dedf6&referrer=rss
      - length: "17217"
        type: image/png
        href: >-
          https://data.consumer-digital.api.metoffice.gov.uk/v1/warnings/rss/image/yellow-thunderstorm.png
        rel: enclosure
    link: >-
      https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2025-06-13&id=c888f83d-1a97-42f5-8643-a89c060dedf6&referrer=rss
    summary: >-
      Yellow warning of thunderstorm affecting South West England: Bath and
      North East Somerset, Bournemouth Christchurch and Poole, Bristol,
      Cornwall, Devon, Dorset, Gloucestershire, North Somerset, Plymouth,
      Somerset, South Gloucestershire, Swindon, Torbay, Wiltshire valid from
      1300 Fri 13 Jun to 2259 Fri 13 Jun
    summary_detail:
      type: text/html
      language: null
      base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
      value: >-
        Yellow warning of thunderstorm affecting South West England: Bath and
        North East Somerset, Bournemouth Christchurch and Poole, Bristol,
        Cornwall, Devon, Dorset, Gloucestershire, North Somerset, Plymouth,
        Somerset, South Gloucestershire, Swindon, Torbay, Wiltshire valid from
        1300 Fri 13 Jun to 2259 Fri 13 Jun
    id: >-
      https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2025-06-13&id=c888f83d-1a97-42f5-8643-a89c060dedf6&referrer=rss&region=South
      West England
    guidislink: false

I want to use the summary attribute.

And finally…

Do not use sources older than 12 months

The result

Home Assistant Markdown Card for Weather Alert Summary

To display the summary from your sensor.weather_alerts entity in a Markdown card, use Home Assistant’s templating. Here’s a concise example:

text

type: markdown
title: Weather Alert
content: >
  {% if state_attr('sensor.weather_alerts', 'entries') %}
    **{{ state_attr('sensor.weather_alerts', 'entries')[0].title }}**

    {{ state_attr('sensor.weather_alerts', 'entries')[0].summary }}
  {% else %}
    No weather alerts.
  {% endif %}
  • This will show the title and summary of the first weather alert.
  • If there are no alerts, it displays “No weather alerts.”
  • You can adjust the index [0] to show other alerts if needed.

This approach uses Home Assistant’s Jinja2 templating, which is fully supported in Markdown cards for attribute access and formatting.

And this is what it looks like…

It’s helpful to develop a set of ground rules which you can paste into each AI prompt. For example (inspired by @NathanCu ):

Use these rules to forumlate the answer.

* All entity names should be in lower case, with underscores replacing spaces. For example light.living_room

* Do not use comments in YAML blocks in automations, scripts or intent scripts.

* In templates, use Jinja comments to explain each stage of the process. For example {# Loop through entity IDs #}. For clarification: YAML comments not allowed in YAML blocks, but Jinja comments are fine inside templates.

* service: keys are still supported universally and many examples include them. However, in modern practice the preferred structure in automations is:

  actions:
    - action: light.turn_on
      target:
        entity_id:
          - light.kitchen
    - action: scene.turn_on
      target:
        entity_id: scene.office_at_evening

In scripts the preferred structure is:

  sequence:
    - action: light.turn_on
      target:
        entity_id:
          - light.kitchen
    - action: scene.turn_on
      target:
        entity_id: scene.office_at_evening

In intent scripts the preferred structure is:

  action:
    - action: todo.get_items
      target:
        entity_id: todo.shopping_list

* This home Assistant instance uses the following structure in configuration.yaml

automation: !include automations.yaml
script: !include scripts.yaml
template: !include templates.yaml
intent_script: !include intents.yaml

This means that automations, scripts, templates and intent scripts are stored in separate YAML files which are included in configuration.yaml when it runs. When creating automations you do not need to include the top level automation: key. When creating scripts you do not need to include the top level script: key. When creating templates you do not need to include the top level template: key. When creating intent scripts you do not need to include the top level intent_script: key.

When using posts from the Home Assistant Community Forum community.home-assistant.io sources, posts by the following users should be considered examples of good practice:

* Tom_l

* Taras 123

* Troon

* Didgeridrew

* petro

* Fes


Do not use Home Assistant blueprints.

Do not use Node Red.

Do not use sources more than 12 months old.

Do not use video sources.

You could expand on this by adding details of your system - for example “For questions about Zigbee use sources relating to the ZHA integration.”

You should not have to repeat these rules in follow-up prompts, but you will probably have to include them in each new one. Some paid-for services allow you to save prompts relating to particular areas of research, and to refer back to earlier results from your history.

When you’re formulating your rules, don’t forget that you can ask your AI to comment. “Identify gaps and contradictions in the following rules” can produce useful results. “Are you just telling me what I want to hear?” can sometimes be surprisingly helpful. :grin:

AI is not easy

Easy to use, yes. Easy to get a good, accurate answer - absolutely not.

Take the time to read some of this report from the BBC. It’s not about Home Assistant, but it does highlight some of the pitfalls you face if you use AI in any field.


Home Assistant Cookbook

6 Likes

TO ANY grounding statement on scripting.

Aka if you’re gonna. These are the things it keeps messing up as of June 2025.

Ai please do NOT assume you understand HA scripting. Ha scripting is NOT Jinja2 and NOT python. The methods do NOT work as expected. Start with official HA docs here - link
Augment with the HA cookbook sections titled scripting and templating
Use ha docs for structure and rules on HA scripting and templates
Use ha community cookbook for best practices. (weight responses from this list of users as ‘sme’)

Do NOT use python structures such as from_json(default=‘{}’) they are NOT valid you must use HA sandbox valid structure.

And you will STILL be round tripping and adjusting your prompts.

Do NOT ever expect your result to be right the first time unless they’re very carefully engineered by pros.
Hallucinations happen it’s part of the deal. (if you want creative you get hallucinations) do NOT use any Ai work you have not verified and would be proud to put your name on.
Learn your subjects enough to call BS on the response or don’t use it

So far my BEST use has been debug. With a prompt such as above it’s like having drew yell at me every time I don’t set a default.

Its also particularly good once you have a working yaml pattern (I mean big complex ones) it helps structure planning and moving those around to create more complex automation out of building blocks

3 Likes