Help voicing text from a report

I have a python script that queries data sources and creates a small report for me. It’s 4-5 sentences.
What I want to do is send that to home assistant, TTS it, and play that over my smart speakers.
Can someone give me any kind of approach to doing this?
I tried using the text control, btu that only stores up to 100 characters.
I created a sensor and tried to store the text in an attribute. My python script said that worked - I get a 200 back when I send to the webhook for the sensor. But the report_text attribute in the sensor is not visible in the UI.
What is the correct approach to accomplishing this project?

David Buttrick

Hi! An attribute should be the way to go for longer texts. But you are not very clear on what is wrong with the attribute. You can show it in the gui if you need. Look at the sensor with the attribute in the developer tools to see if the attribute is set. If it is not set, show us your automation or trigger based template sensor used to receive the webhook data, properly formatted using the preformatted text block of the forum. (It should be a trigger based template sensor if you are trying to use an attribute. You cannot set attributes without some template sensor trickery).

(Personally I would likely try to use HA to take away the Python step also, but that was not your question).

With text control you mean a “text helper”, which has a default lenght of 100 but can be expanded to 255 in the configuration?
I have a compareable flow to the AI which results in a text paragraph and as far as I can remenber you also have to make sure that the size of your text does not exceed the 100 or 255 limit otherwise it is not set.
For attributes there’s no limitation of 100 to the text size.

That limitation does not apply to attributes, so using an attribute to store the text can circumvent the limitation.

1 Like
template:
  - trigger:
      - platform: webhook
        webhook_id: "daveshouse_weather_report_update"

    sensor:
      - name: "Daveshouse Weather Report"
        unique_id: "daveshouse_weather_report_sensor"
        icon: "mdi:text-box-outline"
        state: "{{ now().isoformat() }}"

        attributes:
          report_text: "{{ trigger.json.report_text }}"

Above is my trigger and sensor.
I need the python because I use influx to store some timeseries data, and postgres to store some other transaction data, and the python reaches into these data sources and then assembles the text for the report.
Here is the function in python that sends the text…

def push_to_ha(report_string, config):
    """
    Pushes the final report string to the Home Assistant
    trigger-based template sensor via a webhook.
    """

    HA_URL = config['HOME_ASSISTANT']['url']
    # The webhook ID you defined in your YAML
    WEBHOOK_ID = config['HOME_ASSISTANT']['webhook_id']

    # --- CHANGED ---
    # The URL now points to the webhook, not the /api/states endpoint
    api_url = f"{HA_URL}/api/webhook/{WEBHOOK_ID}"

    # --- CHANGED ---
    # Webhooks do not need the Authorization Bearer token.
    # The 'webhook_id' itself is the secret.
    headers = {
        "Content-Type": "application/json",
    }

    # --- CHANGED ---
    # The JSON payload now has a key 'report_text'.
    # This must match the template in your sensor:
    # {{ trigger.json.report_text }}
    data = {"report_text": report_string}

    try:
        response = requests.post(api_url, headers=headers, json=data)
        response.raise_for_status()
        print(f"Successfully pushed report to Home Assistant. Status: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error pushing to Home Assistant: {e}")

When I run it, I get a 200 back. So I can only assume that the attribute is getting set with the value.
Here’s a screenshot of the entity states.

I figured out that I’ll always get a 200 back from the webhook. That makes sense.
Can I get some logs from the webhook, so I can actually see what the failure is?

I wasn’t posting to the correct url. I was posting to sensorname. You need to post to sensorname_update to make the webhook work.

So, everything works now.

Alright… I’ve made progress here.
My python script generates the text and sends it to a sensor attribute in HA.

Now, I want to TTS this text and play it to a speaker? or multiple speakers?

How do I find the list of the fully qualified names of all the speakers in my house?

Well, I’m all done.
I got this working.

Thanks for the help.