If you're looking for a quick and easy way to add Wink light toggling to your home-automation setup, you're in the right place.
Guide on how to programmatically toggle a Wink light
The Wink API (including Shortcuts and Robots) doesn't offer the ability to toggle a light's state. Searching 3rd party apps such as IFTTT also only have the ability to turn lights either on, or off.
Go to the Wink Developer Portal and create an account
- Hit the New button in the Your Applications
- Enter a Name for your application
- Enter a Website for your application, this can be anything
- Enter a redirect URI for the application (this will be used later for auth), I used https://127.0.0.1
- Agree to the terms and conditions and save the application
- Wait 5-10 minutes for the application to be approved
Using your application's OAuth URLs, attain a valid token. Your token will be valid for approximately 40 days, after this you will need to get a new token.
// TODO: Explain how to do this
// TODO
This step will use a Flic button as the example, but you can modify the steps and code to suite another Zapier trigger
- Create a new Zap! with the following settings
- Connect this app... Flic -> Code by Zapier
- When this happens... New Flic Press -> Run Javascript
- Select your Flic account
- Choose your Flic button and the click type you want (you can leave as Any since the code checks)
- (Optional) Pull in sample clicks from your account (this helps with the sample
inputData
object) - Choose Code as the action
- Choose Run Javascript
- In the Input Data section create a variable named
clickType
and associate it with Click Type from the choices drop down - Paste the following code into the Code box
if (inputData.clickType !== "click") return {}; const authToken = "YOUR_TOKEN"; const deviceId = "YOUR_DEVICE_ID"; const lightState = await (await fetch(`https://api.wink.com/light_bulbs/${deviceId}`, { headers: { Authorization: `Bearer ${authToken}` } })).json(); if (lightState) { const newState = { powered: !lightState.data.last_reading.powered, brightness: 1 }; await fetch(`https://api.wink.com/light_bulbs/${deviceId}/desired_state`, { method: "PUT", headers: { Authorization: `Bearer ${authToken}`, "content-type": "application/json" }, body: JSON.stringify({ desired_state: newState }) }); } return {};
- Replace
YOUR_TOKEN
with your authentication token - Replace
YOUR_DEVICE_ID
with your device's ID - Press Continue
- Press Send Test To Code by Zapier (or Skip Test)
- Press Finish
- Name your Zap and switch it on
- You can now toggle your light using your Flic button (Or other service that you integrated with)
// TODO
Due to the number of services between the trigger and the light, the time from trigger activation, to the light responding can be rather long (~5-10 seconds). This could be sped up a little by using a local server rather than Zapier, however using Zapier is a great option for those who don't want to run their own server.