Home Home Assistant - Alexa - Windows
Post
Cancel

Home Assistant - Alexa - Windows

This one really takes some time to setup but it’s worth it in the end. Once again I’ve taken the bullet and gone through the pain of testing to make it easy for you to configure.

I know home assistant does offer a way to shutdown a windows PC but the ones I’ve found always need some agent installed and running on the PC which is just unnecessary

End Outcome

So the end game for this is to use Alexa to shutdown my Windows PC when I say “Alexa, I’m going to bed”

Over view of steps

1) Setup Windows to accept ansible 2) Integrate Alexa with Home Assistant 3) Create an ansible playbook to shutdown the PC 4) Setup Home Assistant to call the playbook from Ansible-Semaphore 5) Discover Home Assistant devices in Alexa and setup a Routine

Setup Windows to accept ansible

This one is easy. Save this code as This powershell script was copied from the ansible repository on github

Configure Ansible for Windows Powershell Script

Then run it, once it’s done the Windows PC should be setup for ansible. If you’re getting SSH errors it means that the SSH service isn’t running.

Integrate Alexa with Home Assistant

Now this one will take a while to setup, make sure you follow the instructions carefully and you won’t go wrong.

Home Assistant Alexa Integration

Create an ansible playbook to shutdown the PC

Save this to your ansible-semaphore github repository (if you haven’t setup ansible-semaphore have a look at my earlier post)

Make sure you remember the name of the file as we’re going to reference it in the next step. I called it

1
2
3
4
5
6
7
---
  - name: Shutdown Windows machine force
    hosts: all
    gather_facts: false
    tasks:
    - name: Shutdown force
      win_command: cmd /k shutdown -s -t 00 -f

Setup Home Assistant to call the playbook from Ansible-Semaphore

For this you need to create a folder to store bash scripts in Home Assistant

Save this code as (or what ever you would like to call it, just make sure you reference it later.)

Make sure you change the details in the bash script to your details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
curl -v -c /tmp/semaphore-cookie -X 'POST' \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-d '{"auth": "ANSIBLE-SEMAPHORE-USERNAME", "password": "ANSIBLE-SEMAPHORE-PASSWORD"}' \
http://<ANSIBLE-SEMAPHORE-IP>:3000/api/auth/login

curl -v -b /tmp/semaphore-cookie \
-H 'accept: application/json' \
http://<ANSIBLE-SEMAPHORE-IP>:3000/api/project/1/


curl -v -b /tmp/semaphore-cookie -X 'POST' \
'http://<ANSIBLE-SEMAPHORE-IP>:3000/api/project/1/tasks' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"template_id": 2,
"debug": false,
"dry_run": false,
"playbook": "shutdown-windows.yml",
"environment": "{}"
}'

Now add this to your configuration.yml

1
2
3
4
5
# this sets up the bash script as a service in Home Assistant
# Note that the shutdown-pc.sh is in the ansible-scripts folder that we setup in the previous step

shell_command:
  windows_shutdown: bash /config/ansible-scripts/shutdown-pc.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
# This sets up a switch that calls the bash shell command service that we just setup

switch:
  - platform: template
    switches:
      off_ansible:
        turn_off:
          service: shell_command.windows_shutdown
        turn_on:
          service: notify.mobile_app_pixel_6
          data:
            message: test
            title: test

Discover Home Assistant devices in Alexa and setup a Routine

Now you should have all the steps done and Alexa should be setup and integrated with Home Assistant. If you can’t see switch “off_ansible”, ask Alexa to “Discover Devices”

Now go to “Routines”

  • Setup a new one
  • Name it
  • Press “When this happens”
  • Voice
  • add “I’m going to bed”
  • Add action
  • Smart Home
  • Switches
  • select “off_ansible”
  • set the power to OFF

Save it and give it a Test.

Now Hopefully your PC has just shutdown

This post is licensed under CC BY 4.0 by the author.
Trending Tags