How to run commands from Home Assistant
-
Home Assistant (aka Hass.io) is a popular automation system sort of like SmartThings but it's open source.
EDIT 10/6/2024: The official TRIGGERcmd integration is now available in Home Assistant version 2024.10+. This Youtube video shows how it works.
I wrote this
start.sh
bash script to run Home Assistant for the Youtube demo but you could also use it to try out Home Assistant and/or the TRIGGERcmd integration for it:#!/bin/bash mkdir -p ~/ha/config docker run -d \ --name homeassistant \ --privileged \ -e TZ=America/Detroit \ -v ~/ha/config:/config \ -v /run/dbus:/run/dbus:ro \ -p 8123:8123 \ --rm \ ghcr.io/home-assistant/home-assistant:stable
This shows my session where I ran it, then confirmed it's running with the docker ps command:
russ@RussHP:~/ha$ cat start.sh #!/bin/bash mkdir -p ~/ha/config docker run -d \ --name homeassistant \ --privileged \ -e TZ=America/Detroit \ -v ~/ha/config:/config \ -v /run/dbus:/run/dbus:ro \ -p 8123:8123 \ --rm \ ghcr.io/home-assistant/home-assistant:stable russ@RussHP:~/ha$ ./start.sh a32957000a3f029fda67ff1cc2d96b22d2fe4f13125536023e94723a5df2dc6f russ@RussHP:~/ha$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 59c343cb87fa ghcr.io/home-assistant/home-assistant:stable "/init" 2 hours ago Up 2 hours 0.0.0.0:8123->8123/tcp homeassistant
After it was running, I was able to access it at http://localhost:8123
The following is the old method that uses the "RESTful Command" to call the TRIGGERcmd api:
Here's how you trigger commands on your computers from Home Assistant using a RESTful Command.
Add this to the end of your configuration.yaml file:
rest_command: triggercmd_calculator: url: 'https://www.triggercmd.com/api/run/triggerSave' method: "post" content_type: "application/x-www-form-urlencoded" payload: "trigger=calculator&computer=russfam&token=(your token from Instructions page)"
You can test it by clicking the little cell phone icon in the lower left under Developer tools, then select rest_command.triggercmd_calculator in the Service field, then click CALL SERVICE. This is what it should look like:
I installed Home Assistant on my QNAP nas as a Docker container. If you do that, I recommend these two settings:
- Create a folder you have access to, and mount it as /config in your container. That will give you easy access to the configuration files, and when you upgrade the container, you can re-mount this folder to /config.
- Use HOST networking, so you can access Home Assistant at a URL like http://homeassistant.local:8123.
-
I can get the commands to be executed by Alexa, but when I try to make the call directly through the Home Assistant, I get the error: Status Code 400
Does the call syntax remain the same?
-
@rafastanger, that should still work, but you can also use this "get" method below. One thing to note - your trigger and computer values can be different from your voice words you'd use with Alexa.
rest_command: triggercmd_calculator: url: 'https://www.triggercmd.com/api/run/triggerSave?trigger=calculator&computer=russhp&token=(your token)' method: "get"
-
Is it possible to pass arguments via this method?
-
@blizzrdof77, good question. Yes, you can add this to your URL or payload to pass parameters:
¶ms=(yourParams)
You can use a space or %20 between parameters if you have multiple parameters to pass, like this:
¶ms=(param1)%20(param2)
-
@russ Thanks for this, by the way!
For anyone else using TriggerCMD with Home Assistant, I wrote this helper
script
with arest_command
that I have found very useful for running commands with parameters (or without parameters).Just be sure to change "
ADD.YOUR.TRIGGERCMD.TOKEN.HERE
" to your token.The Script:
## -- RUN TRIGGERCMD REST COMMAND script: run_triggercmd_rest_command: alias: Run TriggerCMD REST Command description: Execute a RESTful command with params and a device using triggercmd. icon: mdi:apple-keyboard-command variables: params: '' token: 'ADD.YOUR.TRIGGERCMD.TOKEN.HERE' fields: trigger: description: The name of the trigger example: Exit Windows Program required: true computer: description: The name of the computer as shown in your computer list on triggercmd.com. example: My Computer Name required: true params: description: Optional args string to pass with the trigger, using a space for multiple params [optional]. example: '' required: false sequence: - service: rest_command.tcmd_run_command data: trigger: "{{ trigger }}" computer: "{{ computer }}" params: "{{ params }}" token: "{{ token }}"
The Rest Command
## -- RUN TRIGGERCMD COMMAND rest_command: tcmd_run_command: url: 'https://www.triggercmd.com/api/run/triggerSave' method: POST content_type: 'application/x-www-form-urlencoded' payload: 'trigger={{trigger|replace(" ","%20")}}&computer={{computer|replace(" ","-")}}¶ms=+{{params}}&token={{token}}'
Example Usage
service: script.run_triggercmd_rest_command data: trigger: This Is The Trigger Name # Literally the name of the command computer: My Computer # (will render as 'My-Computer') params: '--female "Shutdown request confirmed"' # This has 3 params
-
-
-
@blizzrdof77 @Russ I am new to HA and trying to get this working but not sure if my issue is related to possible syntax changes since 2021. Does this format still apply? Thanks.
-
@AZRushFan, yes, I just tried it. It worked for me with this text at the end of my configuration.yaml:
rest_command: triggercmd_calculator: url: 'https://www.triggercmd.com/api/run/triggerSave' method: "post" content_type: "application/x-www-form-urlencoded" payload: "trigger=calculator&computer=russfam&token=(my token)"
That said, would it help if I built an official Home Assistant integration?
EDIT 9/15/24: The official TRIGGERcmd integration will be available in next Home Assistant version (2024.10). It's due to be released on 10/2/2024. Please try using it instead of the "RESTful Command" method described above.
-