I am commanding my Sony Android TV via Siri (iPhone + Shortcuts) to switch on/off, start applications, etc.
To do this, I am using some local network requests like...
curl -v -XPOST http://192.168.0.11/sony/system -H 'X-Auth-PSK:XXX' -d '{"id":20,"method":"getRemoteControllerInfo","version":"1.0","params":[]}'
This often works, but sometimes it does not work to switch the TV on, while all the other commands always work fine.
I've investigated sending the requests from the PC at random times when the device was off and I've found out that sometimes it is not responding, so the requests are timing out after a number of seconds.
I've tried then sending a ping beforehand and sometimes the TV started responding, while some other times it took a lot of time (dozens of seconds), but it eventually started responding after I tried multiple times with ping, GET, POST, etc.
The TV resulted being correctly connected to the router with the appropriate IP address all the time (I checked the connected devices on the router control panel).
Why is the device sometimes randomly "sleeping" and responding to web requests only after a random period of time/attempts?
Well, I'll just throw in my experience and see if it helps you. My Sony TV had a bad habit or not having network whenever I would go into Amazon Video, etc. .it would take a second for the network to connect and then it would work. It seemed like the TV turned off the network as a power saving feature to me. Then when I found out Google Home integration worked, just was an issue because of this.. I went to town to find out how to fix it.
https://www.reddit.com/r/AndroidTV/comments/a4yx6o/reminder_check_your_android_tv_for_onoff/ebio59y/?context=3
I found these recommendations on the web and after using them all, my TV no longer disconnects at all.
I think that was everything.. and since then.. I have not had a single time I turned the TV on and the wireless was not connected yet. That, and my Google Home integration is at 100% and works every time. So I really think it is an issue with wireless channel switching. If the above does not work for you.. try Googling the issue.. as I found a ton of forum posts, etc.. about it and I might have missed an item.