Files
thrawn_services/db_logger.lua
2020-11-25 23:21:43 +01:00

47 lines
1.4 KiB
Lua

SERVICE_NAME = "DB Logger"
SERVICE_VERSION = "1.0.0"
function thrawn_on_start()
thrawn_register_service(SERVICE_NAME)
thrawn_log("Starting LUA Service " .. SERVICE_NAME)
--room/type/device_name/state_name
--example:
--wohnzimmer/lights/eckstehlampe01/on
--kueche/sensors/kuehlschrank_tuer/open
thrawn_subscribe("+/lights/+/on")
thrawn_subscribe("+/lights/+/brightness")
thrawn_subscribe("+/lights/+/color_temp")
end
function thrawn_on_update(state)
thrawn_log("RECV: " .. state.value .. " ON TOPIC " .. state.topic)
local topic_comp = thrawn_topic_components(state.topic)
if #topic_comp == 4 and topic_comp[2] == "lights" then
log_light(topic_comp[1], topic_comp[3], topic_comp[4], state.value)
end
end
function log_light(room, name, type, value)
local base_topic = room .. "/lights/" .. name
local is_on = false
local is_on_cached = tonumber(thrawn_cached_topic(base_topic .. "/on"))
if is_on_cached ~= nil then
is_on = is_on_cached > 0
end
local brightness = tonumber(thrawn_cached_topic(base_topic .. "/brightness"))
local color_temp = tonumber(thrawn_cached_topic(base_topic .. "/color_temp"))
if brightness ~= nil and color_temp ~= nil then
thrawn_log_light_tw(room, name, is_on, brightness, color_temp)
elseif brightness ~= nil and color_temp == nil then
thrawn_log_light_dimmable(room, name, is_on, brightness)
elseif brightness == nil and olor_temp == nil then
thrawn_log_light(room, name, is_on)
end
end