Christmas Countdown Sensor with Wolfram Alpha API

I wanted a sensor that displayed the number of days until a given date. I found a couple of nice solutions on this forum but they either involved a custom component or some heavy templating (an area I am far from understanding fully).

Spotting the RESTful Sensor component I wondered if there was a web API out there that could return the data required. This lead me to take a look to see if Wolfram Alpha had an API…they do! You can get 2,000 non-comercial calls for free every month here.

Specifically the API I was interested in was thier Short Answers API. This will take a question string such as “How many days until 25th December 2017” and will return a simple text response such as “10 days”.

I then configured the component in Home Assistant as follows:

sensor:
  - platform: rest
    name: Christmas Countdown
    resource: http://api.wolframalpha.com/v1/result?appid=JIUY8U-4V8KY45VT1&i=How%20many%20days%20until%2025th%20December%202017
    value_template: "{{ (value|replace(' days', '')) | int }}"
    unit_of_measurement: Days
    scan_interval: 43200

The resource is made up of:

  • The API’s base URL (http://api.wolframalpha.com/v1/result?)
  • My unique app id (appid=JIUY8U-4V8KY45VT1&) - This is not my real app id.
  • The question I want to ask, URL encoded (i=How%20many%20days%20until%2025th%20December%202017)

I found this useful site to help with the URL encoding.

The text response is like “10 days” and so I need to extract the number from this value. This is where the value_template comes in. The template I used simply replaces the string " days" with nothing.

I also changed the scan_interval so that I only polled Wolfram Alpha every 12 hours. This will keep my calls to the API well under the 2,000 limit per month.

I can now use the value in my automations such as giving a visual indication of the days left using an LED strip. :smile:

Given the power of Wolfram Alpha I can imagine there may be a lot of ways this API could be useful.

4 Likes

Lol, a 15M lines of code computational engine on grid computing with 10.000 CPUs to calculate the difference between two dates… Stephen Wolfram has something to be proud of :grinning:

Jokes apart, this seem a good tutorial for interfacing with Alpha… I just can’t imagine a more advanced use case.

Ha, yeh the computing power is a tad over kill. I use Wolfram Alpha quite a bit but mainly for the ability to parse natural language questions into results rather than to perform complex calculations.

There’s probably not of advanced uses for Home Assistant but there’s a lot of data there you can get easy access to including:

  • Currency Conversion
  • Stock Prices
  • Unit Conversion
  • Distance Measurement
  • Sports Results
  • Word Properites
  • Translation
  • Weather (including weather history)
  • And also a whole host of techy stuff!

Nice, thanks!

I’ll probably use this for other WolframAlpha queries now it’s so easy :slightly_smiling_face:

An oldie but a goodie! I ended up implementing this code and it works awesome! Made a quick video on the project.

Enjoy!
CARLO

1 Like

If you want unlimited calls and have a Raspberry Pi, Wolfram Alpha is included with Raspbian for free.

However I’ve not yet tried it but I may give it a go as it’s quite nice to be able to run things like this locally.

EDIT: What happens with such as long scan_interval when you reboot or restart HA?

1 Like

All sensors update when HA restarts.

Thanks, that makes sense.

That explains why I’ve sometimes hit an API rate limit after many restarts of HA while tinkering with settings.

1 Like