 alarmParam
				index = i
				break
			end
		end
	end

	-- Alarm not founded
	if ((alarm == nil) and createIfNotExists) then
		alarm = {
			status = "0",
			acknowledge = "0"
		}
		if (lul_settings.alarmId ~= nil) then
			alarm.id = tostring(lul_settings.alarmId)
		else
			-- Search first free id
			for lastId = 1, 20 do
				local isFounded = false
				for i, alarmParam in ipairs(settings.alarms) do
					if (tostring(alarmParam.id) == tostring(lastId)) then
						isFounded = true
						index = i
						break
					end
				end
				if (not isFounded) then
					alarm.id = tostring(lastId)
					break
				end
			end
		end
		alarm.name = lul_settings.alarmName or ("__NEW_ALARM__".. alarm.id)
		table.insert(settings.alarms, alarm)
	end

	if (alarm == nil) then
		error("getAlarm", "Can not retrieve alarm ".. json.encode(lul_settings))
	end

	return alarm, index
end

-- Update HTML panel show on UI
function updatePanel (lul_device)
	local status = "0"
	local style = ""

	local alarmPanel = ""

	if (DEBUG_MODE) then
		alarmPanel = alarmPanel.. '<div style="color:gray;text-align:left;"><small>Debug enabled</small></div>'
	end

	local activeAlarmPanel = ""
	for _, alarm in pairs(settings.alarms) do
		if (alarm.name ~= "") then
			style = "border: gray 1px solid; padding:1px; margin:1px; display:inline-block;"
			if (alarm.status == "1") then
				style = style.. " font-weight:bold;"
				if (alarm.acknowledge == "1") then
					-- Alarm active but with acknowledge
					if (status ~= "1") then
						-- There's not an active alarm without acknowledge
						status = "2"
					end
					style = style.. " color:orange;"
				else
					-- Alarm active
					status = "1"
					style = style.. " color:red;"
				end
				activeAlarmPanel = activeAlarmPanel.. '<li style="'.. style.. '">'.. tostring(alarm.name).. '</li>'
			end
		end
	end

	if (activeAlarmPanel ~= "") then
		alarmPanel = alarmPanel.. '<small><ul class="AlarmPanel" style="text-align: justify; text-indent: 0px; display: inline;">'.. activeAlarmPanel.. '</ul></small>'
	else
		alarmPanel = alarmPanel.. '<div style="color:gray;text-align:left;"><small>No active alarm</small></div>'
	end

	luup.variable_set(SID.VirtualAlarmPanel, "AlarmPanel", alarmPanel, lul_device)
	luup.variable_set(SID.VirtualAlarmPanel, "Status", status, lul_device)
end

-------------------------------------------
-- Alarm management functions
-------------------------------------------

-- Set alarm name (and create if not exists)
function addAlarm (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	local alarm = getAlarm(lul_settings, true)
	if (alarm == nil) then
		return false
	end
	debug("addAlarm", "Add alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "'")
	luup.variable_set(SID.VirtualAlarmPanel, "Alarms", json.encode(settings.alarms), lul_device)
	updatePanel(lul_device)
	return true
end

-- Remove alarm
function removeAlarm (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	local alarm, index = getAlarm(lul_settings)
	if (alarm == nil) then
		return false
	end
	debug("removeAlarm", "Remove alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "'")
	table.remove(settings.alarms, index)
	luup.variable_set(SID.VirtualAlarmPanel, "Alarms", json.encode(settings.alarms), lul_device)
	updatePanel(lul_device)
	return true
end

-- Set alarm name (and create if not exists)
function setAlarmName (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	local alarm = getAlarm(lul_settings, true)
	debug("setAlarmName", "Alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "' - Former name:".. tostring(alarm.name).. " - New name: ".. tostring(lul_settings.newAlarmName))
	if (alarm.name ~= lul_settings.newAlarmName) then
		alarm.name = lul_settings.newAlarmName
		luup.variable_set(SID.VirtualAlarmPanel, "Alarms", json.encode(settings.alarms), lul_device)
		updatePanel(lul_device)
	end
	return true
end

-- Get alarm name
function getAlarmName (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	local alarm = getAlarm(lul_settings)
	if (alarm == nil) then
		luup.variable_set(SID.VirtualAlarmPanel, "LastResult", "", lul_device)
		return false
	end
	debug("getAlarmName", "Alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "' - Name:".. tostring(alarm.name))
	luup.variable_set(SID.VirtualAlarmPanel, "LastResult", tostring(alarm.name), lul_device)
	return { retAlarmName = tostring(alarm.name) }
end

-- Get alarm id
function getAlarmId (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	local alarm = getAlarm(lul_settings)
	if (alarm == nil) then
		luup.variable_set(SID.VirtualAlarmPanel, "LastResult", "", lul_device)
		return false
	end
	debug("getAlarmId", "Alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "' - Id:".. tostring(alarm.id))
	luup.variable_set(SID.VirtualAlarmPanel, "LastResult", tostring(alarm.id), lul_device)
	return { retAlarmId = tostring(alarm.id) }
end

-- Set alarm position
function setAlarmPos (lul_device, lul_settings)
	lul_settings = lul_settings or {}
	lul_settings.newAlarmPos = tonumber(lul_settings.newAlarmPos) or 0
	local alarm, formerAlarmPos = getAlarm(lul_settings)
	if (alarm == nil) then
		luup.variable_set(SID.VirtualAlarmPanel, "LastResult", "Alarm not found", lul_device)
		return false
	end
	if ((lul_settings.newAlarmPos == 0) or (lul_settings.newAlarmPos > table.getn(settings.alarms))) then
		luup.variable_set(SID.VirtualAlarmPanel, "LastResult", "Invalid position", lul_device)
		return false
	end
	-- Get former position
	--local formerAlarmPos = 0
	--for i, alarmParam in ipairs(settings.alarms) do
	--	if (alarmParam.id == alarm.id) then
	--		formerAlarmPos = i
	--		break
	--	end
	--end
	debug("setAlarmPos", "Alarm #".. tostring(alarm.id).. "'".. tostring(alarm.name).. "'