Description

This shortcode retrieves the current weather conditions from OpenWeatherMap.org and displays them as a very clean, minimal widget that can be placed just about anywhere on your internal or external websites.

weather-status-screenshot.jpg

Only the current temperature and condition icon are visible initially, but on hover the location, high/low temperatures, conditions, "feels like" temperature, humidity, precipitation and wind speed are shown in a tooltip.

How-To

Sign up For OpenWeather API

While OpenWeather does have a free API, this shortcode relies on the "paid" One Call API 3.0. The good news is that it's still entirely free for up to 1,000 requests per day. They only take your credit card info in case you go over that 1,000 threshhold.

By default this shortcode's output is cached for two minutes, so even if someone sat there and reloaded your page non-stop for 24 hours, you'd still only end up with 720 requests per day. We've had this displayed on our internal home page for a couple weeks and at most we've had just over 100 requests in a single day.

They also provide a feature where you can set your own maximum number of requests per day. So, if you set your max to 1,000 and you ever reach that limit, their system will just cut you off until the next day and you won't ever end up with a bill.

Here's how to get your account set up:

  1. Create an account
  2. Check your email for a verification link. Verify your account and log in.
  3. Sign up for the One Call by Call subscription
  4. Find your API key and save it for later. You should have already received an automated email with your API key, but if you didn't, you can just go to their site and click on your username in the top right and choose My API Keys to get your key.
  5. Optional: To set your maximum calls per day, go to your Billing Plans page, find the One Call by Call Base plan and click the pencil icon under "Calls per day (no more than)". Change the value to 1000 and hit save.

Shortcode Details

Create a new Lava Shortcode with the following details:

  • Name: Weather Status Widget
  • Tag Name: weather
  • Tag Type: Inline
  • Description: Displays the current weather conditions for the specified latitude and longitude using data from OpenWeatherMap.org

Documentation:

<p><strong>Usage:</strong></p>
<pre>{[ weather apikey:'1a2b3c4d5e6f7890' lat:'36.852458' lng:'-119.747348' locname:'Fresno, CA' ]}</pre>
<ul>
    <li><strong>apikey</strong> (string, required, default:<code>blank</code>) – API key from OpenWeatherMap.org.</li>
    <li><strong>lat</strong> (decimal, required, default:<code>blank</code>) – Latitude of the location to get the weather for.</li>
    <li><strong>lng</strong> (decimal, required, default:<code>blank</code>) – Longitude of the location to get the weather for.</li>
    <li><strong>locname</strong> (string, required, default:<code>blank</code>) – A short location name that will be displayed to the user when they hover over the current temperature.</li>
    <li><strong>units</strong> (string, optional, default:<code>imperial</code>) – The units of measurement to use. Valid options are <code>imperial</code> <small>(Fahrenheit/Miles)</small>, <code>metric</code> <small>(Celcius/Meters)</small> or <code>standard</code> <small>(Kelvin/Meters)</small>.</li>
    <li><strong>cachkey</strong> (string, optional, default:<code>blank</code>) – A unique string to be used the cache key. This is only necessary when using this shortcode for more than one location.</li>
    <li><strong>cacheduration</strong> (integer, optional, default:<code>120</code>) – The length of time in seconds that cached weather conditions should be kept before querying OpenWeatherMap.org again.</li>
</ul>
<p><strong>Output:</strong></p>
<span data-toggle="tooltip" data-html="true" data-placement="bottom" data-container="body" data-viewport=".main-content" data-original-title="<div class=&quot;pr-3&quot;>
    <strong>Fresno, CA</strong><br>
    <div class=&quot;my-1&quot;><span class=&quot;badge badge-danger&quot;>99°</span> <span class=&quot;badge badge-info&quot;>69°</span></div>
    Clouds<br>
    Feels Like: 96°<br>
    Humidity: 26%<br>
    Rain: 0%<br>
    Wind: 1mph<br><br>
    <small>Updated: 2:54 PM</small>
</div>
">
    <i class="fas fa-cloud-sun" title="Clouds"></i> <strong>97°</strong>
</span>

Shortcode Markup:

{%- assign cachekey = cachekey | Default:'weather-status' -%}
{%- cache key:'{{ cachekey }}' duration:'{{ cacheduration }}' tags:'weather-status' -%}
    {%- capture url %}https://api.openweathermap.org/data/3.0/onecall?lat={{ lat }}&lon={{ lng }}&exclude=minutely,hourly,alerts&units={{ units }}&appid={{ apikey }}{% endcapture -%}
    {%- webrequest url:'{{ url }}' -%}
        {%- unless results contains 'BadRequest' or results contains 'Unauthorized' -%}
            {%- case results.current.weather[0].icon -%}
                {%- when '01d' -%} {%- assign icon = 'fas fa-sun' -%}
                {%- when '01n' -%} {%- assign icon = 'fas fa-moon' -%}
                {%- when '02d' -%} {%- assign icon = 'fas fa-cloud-sun' -%}
                {%- when '02n' -%} {%- assign icon = 'fas fa-cloud-moon' -%}
                {%- when '03d' or '03n' or '04d' or '04n' -%} {%- assign icon = 'fas fa-cloud' -%}
                {%- when '09d' or '09n' -%} {%- assign icon = 'fas fa-cloud-rain' -%}
                {%- when '10n' -%} {%- assign icon = 'fas fa-cloud-sun-rain' -%}
                {%- when '10n' -%} {%- assign icon = 'fas fa-cloud-moon-rain' -%}
                {%- when '11d' or '11n' -%} {%- assign icon = 'fas fa-cloud-showers-heavy' -%}
                {%- when '13d' or '13n' -%} {%- assign icon = 'fas fa-snowflake' -%}
                {%- when '50d' or '50n' -%} {%- assign icon = 'fas fa-smog' -%}
                {%- else -%} {%- assign icon = 'fas fa-poo-storm' -%}
            {%- endcase -%}
            {%- assign conditions = results.current.weather | Select:'main' | Join:', ' -%}
            {%- capture tooltip -%}
<div class=&quot;pr-3&quot;>
    <strong>{{ locname }}</strong><br>
    <div class=&quot;my-1&quot;><span class=&quot;badge badge-danger&quot;>{{ results.daily[0].temp.max | AsInteger }}&deg;</span> <span class=&quot;badge badge-info&quot;>{{ results.daily[0].temp.min | AsInteger }}&deg;</span></div>
    {{ conditions }}<br>
    Feels Like: {{ results.current.feels_like | AsInteger }}&deg;<br>
    Humidity: {{ results.current.humidity | Default:'0' | AsInteger }}%<br>
    Rain: {{ results.current.rain | Default:'0' | AsDecimal | Times:100 | AsInteger }}%<br>
    Wind: {{ results.current.wind_speed | Default:'0' | AsInteger }}mph<br><br>
    <small>Updated: {{ 'Now' | Date:'h:mm tt' }}</small>
</div>
            {%- endcapture -%}
<span data-toggle="tooltip" data-html="true" data-placement="bottom" data-container="body" data-viewport=".main-content" data-original-title="{{ tooltip }}">
    <i class="{{ icon }}" title="{{ conditions }}"></i> <strong>{{ results.current.temp | AsInteger }}&deg;</strong>
</span>
        {%- endunless -%}
    {%- endwebrequest -%}
{%- endcache -%}

Parameters:

  • apikey (blank)
  • lat (blank)
  • lng (blank)
  • locname (blank)
  • units imperial
  • cachekey weather-status
  • cacheduration 120

Enabled Lava Commands:

Check the boxes for Cache and WebRequest.

All done!

Just take a look at the shortcode documentation to learn how to use it.

Follow Up

Please don't hesitate to leave a comment or hit me up on Rock Chat (@JeffRichmond) if you have questions or find any issues with this recipe.


Change Log

  • 2022-08-12 - Initial version