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.
Given the power of Wolfram Alpha I can imagine there may be a lot of ways this API could be useful.