Last active
June 26, 2023 14:16
-
-
Save alexey-kirdin-ezlo/3d8edc052df8513de734e6a3263df922 to your computer and use it in GitHub Desktop.
Ezlo Linux Firmware Lua Script: parse openweather output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Oleksii.Kirdin@ezlo.com, 2023-06-12, fixed 2023-06-22 | |
-- Anonymous Plugin (aka Lua script) for Ezlogic. | |
-- Get a stringified json from one variable, parse it to a Lua table, extract some value by path, save it to another variable with value type respect. | |
-- Meshbots/Scenes/Global Variables/HTTP Request Parsing. | |
-- The script can be used as an action in a meshbot after the sendHttpRequest/cloudAPI calls with SaveResult attribute | |
-- to post-process JSON value: extract a value to another expressions variable with type information. | |
-- After that, the variable with type can be used as a trigger in meshbots. | |
-- OpenWeather - a variable of type "string"; | |
-- OpenWeatherCloudCover - a variable of type "float" or "int"; | |
-- path_to_extract - path inside of the JSON stored in OpenWeather variable to fetch data by. | |
-- Result: saves extracted numeric value to the target variable. | |
local logging = require "logging" | |
local json = assert(require "json", "Could not load json library") | |
local scenes = require "scenes" | |
local variables = { | |
source = "OpenWeather", | |
target = "OpenWeatherCloudCover" | |
} | |
local path_to_extract = {"clouds", "all"} | |
local function extract_by_path(data, path) | |
local value = data | |
for _, key in ipairs(path) do | |
logging.debug("key: " .. tostring(key)) | |
value = value[key] | |
logging.trace(value) | |
end | |
return value | |
end | |
local function extract_and_save(variable_from, variable_to, path) | |
logging.debug("HTTP Response variable: " .. variables.source) | |
local http_response_variable = scenes.get_expression_value(variable_from) | |
if http_response_variable.value ~= nil then | |
logging.debug("Source variable has value: " .. tostring(http_response_variable.value)) | |
local data = http_response_variable.value | |
local res = json.decode(data) | |
logging.debug("After json.decode") | |
logging.trace(res) | |
local found_value = tonumber(extract_by_path(res, path)) | |
local success, saved_value = pcall(scenes.get_expression_value, variable_to, {compact = false}) | |
logging.trace("pcall.success: " .. tostring(success)) | |
logging.trace("pcall.saved_value: " .. json.encode(saved_value)) | |
local target_value = { | |
value = found_value, | |
value_type = saved_value.value_type or "float", | |
metadata = saved_value.metadata or {} | |
} | |
logging.debug("Save to target variable: " .. variable_to) | |
scenes.set_variable(variable_to, target_value) | |
return true | |
else | |
logging.error("Source variable has no value") | |
if http_response_variable.error ~= nil then | |
logging.error("Variable.error: " .. tostring(http_response_variable.error)) | |
end | |
return false | |
end | |
end | |
return extract_and_save(variables.source, variables.target, path_to_extract) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment