Linked Mentions display multiple lines as one

I have it do both – a slash command to just get a snapshot and just calling the function will do it dynamically (with hourly updates). I pulled some of this from another thread here on the forum.

local ONE_HOUR = 60 * 60 * 1000

function getWeather(location)
  local timestamp = os.time() * 1000
  local weather = datastore.get({"weather", location})
  
  if not weather or not weather.timestamp or weather.timestamp < (timestamp - ONE_HOUR) then
    local wttrUrl = string.format("https://wttr.in/%s?format=%%t+%%C", location)
    local response = http.request(wttrUrl)
    if response.ok then
      local condition = response.body
      weather = {
        condition = condition,
        timestamp = timestamp,
      }
      datastore.set({"weather", location}, weather)
    end
    if not response.ok then
      return "HTTP request failed."
    end
  end
    
    if not weather then
      return "⚠️ No weather data available."
    end

  return string.format("%s", weather.condition)
end

slashCommand.define {
  name = "weather",
  run = function()
    editor.insertAtCursor(getWeather("Your Location Here"))
  end
}

When called from a template like my journal, just do: ${"$"}{getWeather("Your Location")}

2 Likes