Sending a POST to TTS

Hi,

I’m a newbie with Home Assistant. I’m running Home Assistant with Sonos devices, and I’d like to send a POST command using the RESTful API to have one of the Sonos devices say something. This works fine from the Home Assistant home page, where my Sonos devices are listed.

I’m sending the POST in the following format, and I get a message back saying “Method not allowed”. I’m not that familiar with JSON formatting, and I’m also not sure I have the url right. Can someone correct the following:

URL: http://192.168.86.200:8123/api/services
Headers: Content-Type: application/json
Data: { "service": "google_say", "entity_id": "media_player.upstairs_study", "data":{"message": "May the Force be with you." }}

The response I get is ‘The remote server returned an error: (405) Method Not Allowed.’

Important: forgot to mention that I am coding this in VBA on another home automation platform (Homeseer), so the strings above are being sent using the POST command (called ‘urlAction’) in VBA. I’m not sure if I am translating the Python examples in the documentation properly into the three strings above.

Thanks in advance for any help.

Is there anyone who can help on this? Thanks.

Have you pehraps set a password for your HA setup?

If so you will need to add a header (x-ha-access) with the password.

See the documentation here:

You may find postman handy to debug things:

+1 on using PostMan to debug.

It looks like you’ve got the wrong URL for the endpoint. It’ll be something like

http://192.168.86.200:8123/api/services/DOMAIN/SERVICE

Also, you can put your password (if you set up one in your HTTP component) on the URL:

http://192.168.86.200:8123/api/services/DOMAIN/SERVICE?api_password=PASSWORD

(otherwise, you’d pass it in the header as @hastarin notes).

I don’t use Google Say, so I’m not sure of it’s domain/service. But you can see that from the services dev tool.

for example, a call to turn on a light would be
http://192.168.86.200:8123/api/services/light/turn_on?api_password=PASSWORD
Then pass the JSON data in the body.

Thanks for the input. I’m handling the password in the URL.

With respect to the proper domain\service for Google Say, where would that be documented? I can’t find it anywhere.

To update further, here’s the POST I’m trying now:

url = "http://192.168.86.200:8123/api/services/tts/google_say?api_password=xxxxxx"
headers="Content-Type: application/json"  
data = "{entity_id: 'media_player.upstairs_study', data:{message: 'May the Force be with you.' }}"

I get a 500 Server Error with this. Any thoughts?

Strings in JSON need to be wrapped in a double quote. That’s prob your issue. See:

@jwshome were you ever able to get this to work?

No, I gave up. If I try again I’ll post if I solve the problem.

Sorry about that.

Try like this:

url = "http://192.168.86.200:8123/api/services/tts/google_say?api_password=xxxxxx"
headers="Content-Type: application/json"  

and then:

data = {"entity_id":"media_player.upstairs_study","message":"May the Force be with you"}
3 Likes

Thanks Much @Bob_NL :slight_smile: It’s working for me.

1 Like