Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Home Assistant - Ho...
 
Share:
Notifications
Clear all

[Solved] Home Assistant - How to make a shortcut on your desktop to toggle a switch (macOS)

1 Posts
1 Users
0 Reactions
83 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2854
Topic starter  

I wanted a shortcut on my desktop, since I have a switchable outlet socket to switch my printer ON or OFF.
Now opening Home Assistant each time seems a bit cumbersome, so I figured out how to make a shortcut on my desktop.
I've done this on my Mac, and I'm sure this works the same under Linux, and maybe even Windows.

Requires "curl" which standard comes with your Mac and I would have to guess with Linux as well.

Step 1: Create an authentication token

Open your profile in Home Assistant (most bottom left of the menu) and go to the "Security" tab.
At the bottom of that page you'll find the "Long-lived access tokens" section and at the bottom of that you can create a token.
Give it a meaningful name and copy the code. The code typically looks something like this (just a random example):

ezJhAGciOiJI9zI1NiIsInX5cCI6IkpXVAB9.eyJpc3MiOiIxZTZzNWQ4NDUxZBg8NmYxYTg1MzFhyZ6hZWRmYjE9OC$

Save it carefully, because if you loose it you'll have to create a new token.

Step 2: Make a script that toggles the switch

You'll need:
- the IPADDRESS of home assistant (eg. 192.168.1.100),
- the TOKEN that we just generated, and
- the entity name of the device (eg. switch.socket_printer in this example - determine the one you need).

This is what the script should look like (modify to your needs):

#!/bin/zsh

curl \
  -X POST \
  -H "Authorization: TOKEN" \
  -H "Content-Type: Application/json" \
  -d "{\"entity_id\": \"switch.socket_printer\"}"  http://IPADDRESS:8123/api/services/switch/toggle;  echo

Save the script where ever you think is a good place and name it something like switch.command (extension ".command" is recommended).

You can now make a shortcut on your desktop to this script.

Tip:
1) if you want the Shell/Terminal window to close after executing this script: in the Terminal.app go to Preferences -> Profiles -> <Select a Profile> -> Shell and set 'When the shell exits' to 'Close the window'.
2) Do not forget that you can give the Alias a nice icon (right click the shortcurt -> Get Info -> drag PNG image on the icon in the upper left corner)


   
ReplyQuote
Share: