Passing dynamic info to shell_command

I’m slowly-but-surely porting functionality over from my Vera setup to HA.

In Vera, I’ve got a global script snippet that lets me send text-to-speech to speakers in the house via a URL.

I’m totally new to HA and Linux, so I’m open to the idea that I’m doing this completely wrong, but here’s what I have so far:

 shell_command:
   speak: bash /home/pi/scripts/speak.sh this%20in%20only%20a%20test.

 script:
   speak:
     alias: 'Test the TTS engine'
     sequence:
       - service: shell_command.speak

speak.sh is simply using cURL to talk to my TTS engine.

What I’d like to do is replace that “this is only a test” with a variable. Like “The garage door is open” or “The temp is 70 degrees”. I’ve seen that I can use templates when defining a shell command, but I can’t figure out how to build “this is a test” as an argument, then pass it to the shell command. I don’t understand where I can store that text (examples usually grab the val of a slider).

Ideally, I’d want to url_escape the string before I pass it on to the script.

Is thins something I can do with scripts?

it should works with double quoting “”{{ variable }}"" in the shell command
then add it to your service call

- service: shell_command.speak
  data_template:
    variable: '{{"this%20in%20only%20a%20test."}}'

as for url escaping you could try a cli tool, here’s a few ideas https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/urltools/urltools.plugin.zsh

Nice. Thanks for reviving this. That worked. Here’s the config (including the shell_command)

   script:
    speaktest:
      alias: Another Speech Test
      sequence:
      - service: shell_command.speak
        data_template:
          text: '{{"this is only a test." | urlencode}}'

shell_command:
  speak: bash /home/pi/scripts/speak.sh {{text}}

and the contents of the shell command look like:

curl http://192.168.1.110/gir/speak.lhtml?t=$1

(I have a TTS engine on that computer running inside of Girder. $1 gets the variable from the script.)

7 Likes

Good to hear! I also did something like this but would be careful about $1 although I think some validation is already done by the HA component (at least stoping pipes like | and &&).
You’ll thank me l8ter but think about adding a volume parameter. I used one of the python script in the wiki (keyword occasion) to have sensors dynamically adjust the value depending of time of day, avoiding to shout when everyone’s sleeping :wink:

Btw can you tell me more about your tts engine? Am using google_speak in python and might have to move away from it if it stops being free

CHeck here

1 Like

I had seen it but now also can see how to swap it with https://jasperproject.github.io/documentation/installation/

Thank you ! I marked your post as <3 and bookmarked it so
I can post how I got about with my volume option, I first used templates but the python option is easier for finer grained times

Girder is using the Microsoft TTS engine, so it’s nothing exotic. I already have booleans in place to make sure that everyone’s awake before text is spoken.

I implemented dyamic volumes for morning radio feeds that are opened on desktop and ipads, using booleans only to overwrite these defaults for 2h :wink:

The ones on linux were so robotic it made my ears bleed, I much prefer making google and siri speak but it was a long way until i got the right parameters to parse different feeds (from states to REST api calls telling me when to leave to catch the next bus)

I’m really looking forward to the prospect of push notifications on Alexa’s, One less thing that my Windows server is responsible for. One less level of complexity that I need to maintain.