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.

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!)

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

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.

Do not use sources more than 12 months old.

Do not use video sources.

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

  • Tom_l

  • Taras 123

  • Troon

  • Didgeridrew

  • petro

  • Fes

For examples of Home Assistant templating:

  • Start with the official documentation here https://www.home-assistant.io/docs/configuration/templating/

  • Use this documentation for structure and rules on templates.

  • Use the Home Assistant Community Cookbook https://community.home-assistant.io/t/the-home-assistant-cookbook-index/707144 for examples of best practice.

  • Do not use python structures such as from_json(default=‘{}’) they are not valid.

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 have to include them in each new one.


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

2 Likes