You are not logged in.

#1 2017-02-10 23:06:19

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Simple Lua weather script for conky

I've been using a lua script for grabbing current weather information and thought other people might like it. It uses lua, lua-json and lua-sockets. The output looks like "75°F Clouds"

#!/usr/bin/lua
-- load the http socket module
http = require("socket.http")
-- load the json module
json = require("json")

api_url = "http://api.openweathermap.org/data/2.5/weather?"

-- http://openweathermap.org/help/city_list.txt
cityid = "5128581"

-- metric or imperial
cf = "imperial"

-- get an open weather map api key: http://openweathermap.org/appid
apikey = "<API KEY>"

if cf == "metric" then
    measure = "°C"
elseif cf == "imperial" then
   measure = "°F"
end

weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))

response = json.decode(weather)

math.round = function (n)
    return math.floor(n + 0.5)
end

temp = response.main.temp
conditions = response.weather[1].main

io.write(("%s%s %s\n"):format(math.round(temp), measure, conditions))

inside conky itself I have "${execpi 3600 ~/scripts/weather.lua}"

To use it you need to change the variables how you want and get an API Key from open weather map. I've been thinking about adding in unicode weather icon support too, but I haven't gotten around to doing it.


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

#2 2017-03-29 19:04:10

Paul-S
Member
From: Wales
Registered: 2008-02-04
Posts: 353

Re: Simple Lua weather script for conky

Thanks for this Meskarune, post the new one with icon support if you get around to it smile

Cheers
Paul-S

Offline

#3 2018-03-07 06:42:19

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Re: Simple Lua weather script for conky

I made a bunch of updates to the script. It now has unicode icons and caches the weather data. If the cached data is older than an hour it is updated, and if the network is down, the cache is used as a fallback. I choose an hour because open weather map doesn't update more often than that. The output looks like:

? 37°F Rain

Here is the updated code:

#!/usr/bin/lua
-- load the http socket module
http = require("socket.http")
-- load the json module
json = require("json")

api_url = "http://api.openweathermap.org/data/2.5/weather?"

-- http://openweathermap.org/help/city_list.txt , http://openweathermap.org/find
cityid = "5128581"

-- metric or imperial
cf = "imperial"

-- get an open weather map api key: http://openweathermap.org/appid
apikey = "<API KEY>"

-- measure is °C if metric and °F if imperial
measure = '°' .. (cf == 'metric' and 'C' or 'F')

-- Unicode weather symbols to use
icons = {
  ["01"] = "☀",
  ["02"] = "?",
  ["03"] = "?",
  ["04"] = "☁",
  ["09"] = "?",
  ["10"] = "?",
  ["11"] = "?",
  ["13"] = "?",
  ["50"] = "?",
}

currenttime = os.date("!%Y%m%d%H%M%S")

file_exists = function (name)
    f=io.open(name,"r")
    if f~=nil then
        io.close(f)
        return true
    else
        return false
    end
end

if file_exists("weather.json") then
    cache = io.open("weather.json","r+")
    data = json.decode(cache:read())
    timepassed = os.difftime(currenttime, data.timestamp)
else
    cache = io.open("weather.json", "w")
    timepassed = 6000
end

makecache = function (s)
    s.timestamp = currenttime
    save = json.encode(s)
    cache:write(save)
end

if timepassed < 3600 then
    response = data
else
    weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, cf, apikey))
    if weather then
        response = json.decode(weather)
        makecache(response)
    else
        response = data
    end
end

math.round = function (n)
    return math.floor(n + 0.5)
end

temp = response.main.temp
conditions = response.weather[1].main
icon = response.weather[1].icon:sub(1, 2)

io.write(("${font Symbola:size=8}%s${font} %s%s %s\n"):format(icons[icon], math.round(temp), measure, conditions))

cache:close()

The script sets the Symbola font as the weather icon font using conky's syntax. You will need to install that font or change it to another icon font if you want something different. It's line 61 with `${font Symbola:size=8}%s${font}`. If you wanted to use the script in a panel you can remove the conky font syntax.

To use the script in conky you can again just add `${execpi 1800 ~/scripts/weather.lua}` to ~/conkyrc and it will run every 30 min. Just change the path to wherever you save the script.

edit: here is a gist so its easier to download: https://gist.github.com/meskarune/5729e … ed475db4e1

Last edited by Meskarune (2018-03-07 07:46:03)


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

#4 2018-08-01 21:43:39

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Re: Simple Lua weather script for conky

I made a bunch of updates to the script and have them in a gist here: https://gist.github.com/meskarune/e4157 … 42d66011e8

There is also a calendar script and conky config file.

Here is a screenshot:

4RcJ.png


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

#5 2019-11-21 12:01:04

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Re: Simple Lua weather script for conky

New updates. There is a moonphase script and some data the api stopped serving was removed. https://gist.github.com/meskarune/e4157 … 42d66011e8

screenshot:

calendar with weather information


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

#6 2020-04-10 01:26:51

drpr15
Member
Registered: 2018-02-25
Posts: 3

Re: Simple Lua weather script for conky

How would one modify the calendar to center it?  I tried ${alignc} in the conky file, but it only affected the top row.  My conky is wider than yours, and the calendar comes out left justified.

Offline

#7 2020-09-07 11:51:48

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Re: Simple Lua weather script for conky

drpr15 wrote:

How would one modify the calendar to center it?  I tried ${alignc} in the conky file, but it only affected the top row.  My conky is wider than yours, and the calendar comes out left justified.

You would need to edit the lua script to add spaces for each row. Unfortunately conky doesn't see the calendar as an object that can be moved around in whole.

You could maybe try also changing the font and font size so the calendar is larger to take up more of the space. You could also have the calendar run in its own conky instance and change the x, y location that way, then the rest can be another conky process.

On my own computer I just set the width of my conky to match the width of the calendar.

I hope this helps!


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

#8 2020-09-07 12:23:17

Meskarune
Member
From: Helsinki, Finland
Registered: 2009-03-21
Posts: 361
Website

Re: Simple Lua weather script for conky

I did more updates to the script. It uses the new "one call" api so it can show forecast information now and wind direction is working again. This is the updated gist: https://gist.github.com/meskarune/e4157 … 42d66011e8

Conky monitor showing weather information


Homepage  | Arch Linux Women | Arch Linux Classroom
Acer Aspire E5-575G-53VG:
Intel Dual-Core i5-6200U 2.3 GHz; Skylake rev3 | 8GB DDR4 ram | Intel HD Graphics 520 + NVIDIA GeForce 940MX
Qualcomm Atheros QCA9377 802.11ac | Realtek RTL8111/8168/8411 Gigabit Ethernet Controller

Offline

Board footer

Powered by FluxBB