Re: dzVents 1.0 released
Posted: Wednesday 06 July 2016 14:27
well, problem solved
(for now)

Code: Select all
return {
active = true ,
on = { 'timer' },
execute = function(domoticz)
domoticz.variables.forEach(function(variable)
print(variable.Value)
if (variable.Value == 'alarm') then
domoticz.devices['Alarm Triggered'].switchOn()
domoticz.notify('Alarm triggered Event Test!', '', domoticz.PRIORITY_EMERGENCY)
else
print(variable.Value)
domoticz.devices['Alarm Triggered'].switchOff()
end
end)
end
}
Code: Select all
{
result: [
{
LastUpdate: "2016-07-06 13:56:37",
Name: "G_SPC_ZONE_STATUS_TESTalarm",
Type: "2",
Value: "alarm",
idx: "63"
}
],
status: "OK",
title: "GetUserVariable"
}
Code: Select all
2016-07-06 15:08:41.309 (RaZberry) Temp (HW Tank Temp)
2016-07-06 15:09:00.579 LUA: =====================================================
2016-07-06 15:09:00.579 LUA: >>> Handler: spc_alarm
2016-07-06 15:09:00.579 LUA: .....................................................
2016-07-06 15:09:00.582 LUA: .....................................................
2016-07-06 15:09:00.582 LUA: <<< Done
2016-07-06 15:09:00.582 LUA: -----------------------------------------------------
2016-07-06 15:09:00.582 LUA: [1] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [2] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [3] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [4] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [5] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [6] = Alarm Triggered: Off
2016-07-06 15:09:00.582 LUA: [7] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [8] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [9] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [10] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [11] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [12] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [13] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [14] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [15] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [16] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [17] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [18] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [19] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [20] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [21] = Alarm Triggered: Off
2016-07-06 15:09:00.583 LUA: [22] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [23] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [24] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [25] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [26] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [27] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [28] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [29] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [30] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [31] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [32] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [33] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [34] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [35] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [36] = Alarm Triggered: Off
2016-07-06 15:09:00.584 LUA: [37] = Alarm Triggered: Off
2016-07-06 15:09:00.585 LUA: [38] = Alarm Triggered: Off
2016-07-06 15:09:00.585 LUA: [39] = Alarm Triggered: Off
2016-07-06 15:09:00.585 LUA: =====================================================
2016-07-06 15:09:00.610 EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_time_main.lua
2016-07-06 15:09:00.843 (DUMMY) Lighting 1 (Alarm Triggered)
Ah, it took me a little but it should be variable.value (lowercase)fergalom wrote:Query on my code:
I have a number (39) of user variables being populated from an alarm system with the values "ok" or "alarm"
I want to monitor them for the "alarm" value and if "alarm" I want to turn on a switch in domoticz and send a notification
This is my code:Code: Select all
return { active = true , on = { 'timer' }, execute = function(domoticz) domoticz.variables.forEach(function(variable) local switched = false print(variable.Value) if (variable.Value == 'alarm') then domoticz.devices['Alarm Triggered'].switchOn() domoticz.notify('Alarm triggered Event Test!', '', domoticz.PRIORITY_EMERGENCY) else print(variable.value) if (switched == false) then domoticz.devices['Alarm Triggered'].switchOff() switched = true end end end) end }
Code: Select all
return {
active = true ,
on = { 'timer' },
execute = function(domoticz)
local alarmTrigger = domoticz.devices['Alarm Triggered']
local switchCmd = 'Off'
domoticz.variables.forEach(function(variable)
if (variable.value == 'alarm') then
switchCmd = 'On'
end
end)
if (switchCmd == 'On' and alarmTrigger.state == 'Off') then
alarmTrigger.switchOn()
domoticz.notify('Alarm triggered Event Test!', '', domoticz.PRIORITY_EMERGENCY)
elseif (alarmTrigger.state == 'On') then
-- only switchOff if it was On
alarmTrigger.switchOff()
end
end
}
Code: Select all
return {
active = true ,
on = { 'G_SPC_ZONE_STATUS_*'},
execute = function(domoticz)
etc
etc
Code: Select all
commandArray = {}
logging = true
if (uservariablechanged['G_SPC_AREA_House'] == 'unset') then
commandArray['House Alarm Set/Unset']='Off'
elseif (uservariablechanged['G_SPC_AREA_House'] == 'set') then
commandArray['House Alarm Set/Unset']='On'
end
if (uservariablechanged['G_SPC_ZONE_INPUT_Front_Door'] == 'open' and otherdevices['Front Door']=='Closed') then
commandArray['UpdateDevice']='122|1|Open'
print('Front Door Open')
elseif (uservariablechanged['G_SPC_ZONE_INPUT_Front_Door'] == 'closed' and otherdevices['Front Door']=='Open') then
commandArray['UpdateDevice']='122|0|Closed'
print('Front Door Closed')
end
return commandArray
@dannyblueNice.
I just added an example script in the dev branch that controls heating in one room where a setpoint device, a temp-sensor and a boiler switch work together with some basic hysteresis control. You could duplicate the script for all device or adapt it to support multi-rooms. It uses one hystorical variable to catch unwanted peaks in the temperature readings. The script is triggered by device events and timer events all in one.
Code: Select all
local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR)
Code: Select all
lua:76: attempt to concatenate local 'avgTemp' (a nil value)
That surely isn't what it should dojkimmel wrote:@dannyblueNice.
I just added an example script in the dev branch that controls heating in one room where a setpoint device, a temp-sensor and a boiler switch work together with some basic hysteresis control. You could duplicate the script for all device or adapt it to support multi-rooms. It uses one hystorical variable to catch unwanted peaks in the temperature readings. The script is triggered by device events and timer events all in one.
please have a look at your example
leads toCode: Select all
local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR)
Code: Select all
lua:76: attempt to concatenate local 'avgTemp' (a nil value)
Any news?dannybloe wrote:That surely isn't what it should dojkimmel wrote:@dannyblueNice.
I just added an example script in the dev branch that controls heating in one room where a setpoint device, a temp-sensor and a boiler switch work together with some basic hysteresis control. You could duplicate the script for all device or adapt it to support multi-rooms. It uses one hystorical variable to catch unwanted peaks in the temperature readings. The script is triggered by device events and timer events all in one.
please have a look at your example
leads toCode: Select all
local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR)
Code: Select all
lua:76: attempt to concatenate local 'avgTemp' (a nil value)
I'll look into it. Thanks for testing.
Will after the weekend though... going for a weekend trip to Berlin.
So sorry but my house is a construction area right now (renovation and isolation) which will take at least one or two weeks so I don't have time to fix that I think. I will try but no promises. On the other hand... pull requests are always welcomejkimmel wrote:dannybloe wrote:jkimmel wrote:
Any news?
Solved. There had been not enough valuesdannybloe wrote:So sorry but my house is a construction area right now (renovation and isolation) which will take at least one or two weeks so I don't have time to fix that I think. I will try but no promises. On the other hand... pull requests are always welcomejkimmel wrote:dannybloe wrote:Again sorry.
Take you time, but here is my next question concerning the following codedannybloe wrote:So sorry but my house is a construction area right now (renovation and isolation) which will take at least one or two weeks so I don't have time to fix that I think. I will try but no promises. On the other hand... pull requests are always welcomejkimmel wrote:dannybloe wrote:Again sorry.
Code: Select all
-- assumptions:
-- the setpoint is set by a selector dummy device where the values are numeric temperatures
-- but you can easily change it to a setpoint device
local BOILER_DEVICE = 'FB 41 Heizkreis Gaestebad' -- switch device
local SETPOINT_DEVICE = 'Setpoint Gaestebad' -- selector dummy device
local TEMPERATURE_SENSOR = 'Gaestebad'
local HYSTERESIS = 0.5 -- temp has to drop this value below setpoint before boiler is on again
local SMOOTH_FACTOR = 3
local LOGGING = true
return {
on = {
['timer'] = 'every minute',
TEMPERATURE_SENSOR,
SETPOINT_DEVICE
},
data = {
temperatureReadings = { history = true, maxItems = SMOOTH_FACTOR }
},
active = true,
execute = function(domoticz, device, triggerInfo)
local avgTemp
local temperatureReadings = domoticz.data.temperatureReadings
-- first check if the sensor got a new reading or the setpoint was changed:
if (triggerInfo.type == domoticz.EVENT_TYPE_DEVICE) then
local sensor = domoticz.devices[TEMPERATURE_SENSOR]
if (sensor.changed and sensor.attributeChanged('temperature')) then
-- sensor just reported a new reading
-- add it to the readings table
local current = sensor.temperature
if (current ~= 0 and current ~= nil) then
temperatureReadings.add(current)
else
-- no need to be here, weird state detected
domoticz.log('Strange sensor reading.. skiping', domoticz.LOG_ERROR)
return
end
elseif (domoticz.devices[SETPOINT_DEVICE].changed) then
-- a new setpoint was set
if LOGGING then domoticz.log('Setpoint was set to ' .. device.state) end
else
-- no business here, bail out...
return
end
end
-- now determine what to do
local boiler = domoticz.devices[BOILER_DEVICE]
local setpoint = domoticz.devices[SETPOINT_DEVICE]
if (setpoint.state == nil or setpoint.state == 'Off') then
boiler.switchOff()
return -- we're done here
end
local setpointValue = tonumber(setpoint.state)
-- determine at which temperature the boiler should be
-- switched on
local switchOnTemp = setpointValue - HYSTERESIS
-- don't use the current reading but average it out over
-- the past <SMOOTH_FACTOR> readings (data smoothing) to get rid of noise, wrong readings etc
local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR)
if LOGGING then
domoticz.log('Average: ' .. avgTemp, domoticz.LOG_INFO)
domoticz.log('Setpoint: ' .. setpointValue, domoticz.LOG_INFO)
domoticz.log('Current boiler state: ' .. boiler.state, domoticz.LOG_INFO)
domoticz.log('Switch-on temperature: ' .. switchOnTemp, domoticz.LOG_INFO)
end
if (avgTemp >= setpointValue and boiler.state == 'On') then
if LOGGING then domoticz.log('Target temperature reached, boiler off') end
boiler.switchOff()
end
if (avgTemp < setpointValue and boiler.state == 'Off') then
if (avgTemp < switchOnTemp) then
if LOGGING then domoticz.log('Heating is required, boiler switched on') end
boiler.switchOn()
else
if LOGGING then domoticz.log('Average temperature below setpoint but within hysteresis range, waiting for temperature to drop to ' .. switchOnTemp) end
end
end
end
}
Code: Select all
2016-11-07 20:20:38.697 LUA: Handling events for: "Gaestebad_Dewpoint", value: "12.422341346741"
2016-11-07 20:20:38.698 LUA: =====================================================
2016-11-07 20:20:38.698 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:20:38.698 LUA: >>> Device: "Gaestebad" Index: 280
2016-11-07 20:20:38.698 LUA: .....................................................
2016-11-07 20:20:38.707 LUA: Average: 21
2016-11-07 20:20:38.708 LUA: Setpoint: 15
2016-11-07 20:20:38.708 LUA: Current boiler state: Off
2016-11-07 20:20:38.709 LUA: Switch-on temperature: 14.5
2016-11-07 20:20:38.713 LUA: .....................................................
2016-11-07 20:20:38.713 LUA: <<< Done
2016-11-07 20:20:38.714 LUA: -----------------------------------------------------
2016-11-07 20:21:00.718 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:21:00.718 LUA: .....................................................
2016-11-07 20:21:00.725 LUA: Average: 21
2016-11-07 20:21:00.725 LUA: Setpoint: 15
2016-11-07 20:21:00.725 LUA: Current boiler state: Off
2016-11-07 20:21:00.726 LUA: Switch-on temperature: 14.5
2016-11-07 20:21:00.731 LUA: .....................................................
2016-11-07 20:21:00.731 LUA: <<< Done
2016-11-07 20:21:00.731 LUA: -----------------------------------------------------
2016-11-07 20:21:00.732 LUA: =====================================================
2016-11-07 20:21:21.555 LUA: Handling events for: "Gaestebad_Temperature", value: "21"
2016-11-07 20:21:21.556 LUA: =====================================================
2016-11-07 20:21:21.556 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:21:21.556 LUA: >>> Device: "Gaestebad" Index: 280
2016-11-07 20:21:21.556 LUA: .....................................................
2016-11-07 20:21:21.565 LUA: Average: 21
2016-11-07 20:21:21.566 LUA: Setpoint: 15
2016-11-07 20:21:21.566 LUA: Current boiler state: Off
2016-11-07 20:21:21.566 LUA: Switch-on temperature: 14.5
2016-11-07 20:21:21.571 LUA: .....................................................
2016-11-07 20:21:21.571 LUA: <<< Done
2016-11-07 20:21:21.571 LUA: -----------------------------------------------------
2016-11-07 20:22:00.839 LUA: -----------------------------------------------------
2016-11-07 20:22:04.556 LUA: Handling events for: "Gaestebad_Humidity", value: "58"
2016-11-07 20:22:04.556 LUA: =====================================================
2016-11-07 20:22:04.556 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:22:04.557 LUA: >>> Device: "Gaestebad" Index: 280
2016-11-07 20:22:04.557 LUA: .....................................................
2016-11-07 20:22:04.565 LUA: Average: 21
2016-11-07 20:22:04.566 LUA: Setpoint: 15
2016-11-07 20:22:04.566 LUA: Current boiler state: Off
2016-11-07 20:22:04.566 LUA: Switch-on temperature: 14.5
2016-11-07 20:22:04.571 LUA: .....................................................
2016-11-07 20:22:04.571 LUA: <<< Done
2016-11-07 20:22:04.571 LUA: -----------------------------------------------------
2016-11-07 20:22:47.556 LUA: Handling events for: "Gaestebad", value: "21.0;58;1"
2016-11-07 20:22:47.556 LUA: =====================================================
2016-11-07 20:22:47.556 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:22:47.557 LUA: >>> Device: "Gaestebad" Index: 280
2016-11-07 20:22:47.557 LUA: .....................................................
2016-11-07 20:22:47.566 LUA: Average: 21
2016-11-07 20:22:47.566 LUA: Setpoint: 15
2016-11-07 20:22:47.566 LUA: Current boiler state: Off
2016-11-07 20:22:47.567 LUA: Switch-on temperature: 14.5
2016-11-07 20:22:47.571 LUA: .....................................................
2016-11-07 20:22:47.571 LUA: <<< Done
2016-11-07 20:22:47.572 LUA: -----------------------------------------------------
2016-11-07 20:26:22.553 LUA: Handling events for: "Gaestebad_Dewpoint", value: "12.422341346741"
2016-11-07 20:26:22.554 LUA: =====================================================
2016-11-07 20:26:22.554 LUA: >>> Handler: Heizung Gaestebad
2016-11-07 20:26:22.554 LUA: >>> Device: "Gaestebad" Index: 280
2016-11-07 20:26:22.554 LUA: .....................................................
2016-11-07 20:26:22.563 LUA: Average: 21
2016-11-07 20:26:22.563 LUA: Setpoint: 15
2016-11-07 20:26:22.564 LUA: Current boiler state: Off
2016-11-07 20:26:22.564 LUA: Switch-on temperature: 14.5
2016-11-07 20:26:22.568 LUA: .....................................................
2016-11-07 20:26:22.569 LUA: <<< Done
2016-11-07 20:26:22.569 LUA: -----------------------------------------------------
Code: Select all
-- Water leak detection
--
-- assumptions:
-- need 2 devices :
-- a Water Flow devices name "Water_Flow"
-- a Dummy Device type percentage "Leakage_Percent"
--
-- 1 / leakage "open valve"
-- Every minute if a non-zero water flow is present, it incerments a counter (Leakage_Percent)
-- If the water flow is zero is that it leaks more continuously.
-- A notification can be put on "Leakage_Percent" if >80% (80% = 80 minutes of continuous flow)
--
-- 2 / "micro continuous flow" (drip)
-- in 24 hours one must have at least 2 hours without flow (detection 0.5 liters / hour is 4.5m3 / year)
-- if not, "Leakage_Percent" is forced at 100%.
local FLOW_DEVICE = 'Water_Flow' -- Flow device
local LEAK_DEVICE = 'Leakage_Percent' -- percent dummy device
return {
active = true,
on = {
['timer'] = 'every minute'
},
data = {
time_0_flow = { initial = 0 },
total_time = { initial = 0 },
},
execute = function(domoticz)
-- Flow in liter/minute
local flow = tonumber(domoticz.devices[FLOW_DEVICE].rawData[1])
-- Dummy device in %
local leakage = domoticz.devices[LEAK_DEVICE]
local time_with_flow = tonumber(leakage.rawData[1])
local new_time_with_flow = time_with_flow
-- 1 / leakage "open valve"
if (flow > 0) then
domoticz.data.time_0_flow = 0 -- there is a flow
new_time_with_flow = new_time_with_flow + 1 -- time with flow
if (new_time_with_flow > 100) then
new_time_with_flow = 100
end
else
new_time_with_flow = 0
domoticz.data.time_0_flow = domoticz.data.time_0_flow + 1 -- time without flow
end
-- 2 / flight type "micro continuous flow" (drip)
domoticz.data.total_time = domoticz.data.total_time + 1 -- time without since last 2 hours with no flow
if (domoticz.data.time_0_flow > 120) then
-- 2 hours with no flow
domoticz.data.total_time = 0
elseif (domoticz.data.total_time > (60*24)) then
-- 24 heures since last 2 hours with no flow
new_time_with_flow = 100
end
-- log
domoticz.log(new_time_with_flow .. ' minutes with flow ')
domoticz.log(domoticz.data.time_0_flow .. ' minutes without flow ')
domoticz.log(domoticz.data.total_time .. ' minutes witout 2hrs witout flow ')
-- update dummy device %
if (time_with_flow ~= new_time_with_flow) then
leakage.update(0,new_time_with_flow)
end
end
}