Solar Battery and Alexa - in 30 lines of code


Amazon Alexa is a fun little bit of kit. But it can be tricky getting it to work with all your smart devices. Not every company has an Alexa skill - just like not every company has an app.

Using Flask-Ask it is possible to bring Alexa smarts to a range of previously mute devices.

Alexa coding works on "intents" - the following is a simple intent. That is, you can only ask the skill one thing. No state is maintained, no multiple commands to get right, no complexity. This gets information from a single source and speaks it.

Code

The basic Python is pretty simple and can be adapted to query almost any basic JSON API. Let me walk you through it.

All the boilerplate needed to set up the skill:

import logging
from operator import itemgetter

import requests
from flask import Flask
from flask_ask import Ask, statement

app = Flask(__name__)
ask = Ask(app, '/')
logger = logging.getLogger()

@ask.launch
def launch():
    return stats()

The API that you want to call. This is a basic JSON API which doesn't require authentication.

ENDPOINT = "https://example.com/api/maslow.json"

Here's the main part of the Skill. When the intent is triggered, call the API. Get the data and format it for speech. If there was an error, tell the user.

Make sure that the name of the intent is identical to the one you set up in the Alexa Developer console.

@ask.intent("BatteryIntent")
def stats():
    r = requests.get(ENDPOINT)
    data = r.json()

    if r.status_code == 200:
	percent = data['battery/amphours']
	speech = "Your Moixa battery is at " + str(int(round(percent))) + " percent capacity right now."
    else:
	speech = "There was a problem connecting to the battery."

    logger.info('speech = {}'.format(speech))
    return statement(speech)

Short and easy.

Deploying

Again, Amazon don't make it easy to deploy Alexa skills - here's a guide to getting started using Zappa.

Remember, Alexa is not AI. You must painstakingly type in all the "utterances" that you think your users might say to activate the skill.

A list of phrases used to activate the device

Problems

There is one major problem with retrofitting Alexa skills.

Lots of Internet connected devices have no ability to log in remotely - and certainly don't have the OAuth systems that Amazon demands. Alexa has no ability to directly connect to IP addresses on its own subnets.

This means most skills will need hard-coded credentials - and a way to traverse into your network.

In short, this means means they can't be shared on the Amazon store.


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">