I abandoned this approach and wrote a Node-RED node for doing this. There are also a couple of Appdaemon solutions here as well.