Main Content

Use Google Assistant to Turn a Light On and Off

This example shows how to use your voice to set a channel value on ThingSpeak™. IFTTT interfaces directly with Google® Assistant to understand your speech and then write to a ThingSpeak channel.

A lamp indicator widget provides a graphical display of the channel value in your ThingSpeak channel view. You can also connect a real light to the channel. For details, see Connect Light to Channel.

Set Up Software

1) Create a ThingSpeak account and at least one channel as shown in Collect Data in a New Channel. Record the write API key from the API Keys tab in your channel view.

2) Add a Lamp Indicator to your channel. This example uses a lamp indicator widget set so the lamp is on if the field 1 value is greater than 0.

3) Create an IFTTT account if you do not already have one. Log in to your account.

4) Install the Google Assistant app on your mobile device.

Create IFTTT Applet to Turn Lamp On

IFTTT applets require a trigger and an action. For this example, the trigger is a command from Google Assistant and the action is Webhooks. The webhook sends a command to the ThingSpeak REST API to change the channel value.

Sign in to your IFTTT account. Choose My Applets from the top menu and then select New Applet.

Set Trigger

1) Click +this to set the trigger.

3) Enter Google Assistant in the search bar and select Google Assistant as your trigger service.

4) Choose Say a simple phrase.

5) Enter a phrase to trigger your lamp. Since the assistant is also capable of web searches, avoid simple patterns such as Turn light on. This example uses the phrase Light my fire to turn the lamp on.

Set Action

Now choose Webhooks as your action.

1) Select +that to continue.

2) Enter Webhooks in the search box and select the Webhooks card.

3) Complete the Webhooks action fields. Enter the URL to change the field value to 1. The URL has the following form.

https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1=1

Use GET for the Method and application/x-www-form-urlencoded for the Content Type.

Create IFTTT Applet to Turn Lamp Off

Repeat the previous steps to set up another applet to turn the lamp off. Use a different phrase to indicate that you want to change the lamp state to off. This example uses the command Kill the lamp, which is easy to distinguish from the on command. Use Webhooks and the REST call to set the field value to 0 for the off state.

https://api.thingspeak.com/update?api_key=XXXXXXXXXXXXXXXX&field1=0

Test Your Lamp Control

1) Open the Google Assistant app on your phone or device.

2) Say the trigger phrase.

3) Observe the changing lamp and the channel values.

Connect Light to Channel

You can control a light with a device that has ThingSpeak access, such as an Arduino with a Wi-Fi shield or an ESP8266. Many of the device examples show how to connect a device to ThingSpeak. Prototyping with Sonar Proximity Sensor describes the setup for an Arduino device. For an LED, you can connect the light directly to an output pin on the device. For other lights, you might need to integrate a relay or another type of switch. Then, use MQTT Subscribe or Read Data to read the current channel value with your device, and set the light status based on the channel value.

External Websites