I'm trying to work with Persistent data, but for some reason I don't get it to work the way that I want to.
Even if barometer_old contains the same value (980) as presented in barometer the if state triggers.
data = {
barometer_old = {},
},
if (barometer) ~= (barometer_old) then
domoticz.devices['Trigger request Text 2_1'].switchOn()
domoticz.data.barometer_old = (barometer)
end
Last edited by gus on Wednesday 11 January 2017 22:03, edited 1 time in total.
Are you sure both values are the same?
I know that for example a setpoint sometimes returns 1 decimal place, and sometimes 2. Like 21.0 and 21.00.
If you then compare that as a string they are different.
I also think you should post the complete code...
-- ------------------------------------
-- Hantering av display --
-- ------------------------------------
return {
active = true,
on = {
['timer'] = {'every minute'}
},
data = {
barometer_old = {},
},
execute = function(domoticz)
local function roundToNthDecimal(num, n)
local mult = 10^(n or 0)
return math.floor(num * mult + 0.5) / mult
end
local barometer = roundToNthDecimal((domoticz.devices['Barometer'].barometer), 0)
local balja = roundToNthDecimal((domoticz.devices['Temp to Pool'].temperature), 1)
local utetemp = roundToNthDecimal((domoticz.devices['Temp Veranda Ute'].temperature), 1)
local vindgust = roundToNthDecimal((domoticz.devices['Wind'].windgust), 1)
local regn = roundToNthDecimal((domoticz.devices['Rain'].rain), 1)
domoticz.log(('barometer_old value: ') .. (domoticz.data.barometer_old))
domoticz.log(('barometer value: ') .. (barometer))
domoticz.devices['Text 1_1'].updateText('Klockan ar ' .. os.date("%R"))
domoticz.devices['Trigger request Text 1_1'].switchOn()
if (barometer) ~= (barometer_old) then
domoticz.devices['Trigger request Text 2_1'].switchOn()
domoticz.data.barometer_old = (barometer)
end
domoticz.devices['Text 3_1'].updateText('Temp Balja ' .. (balja) .. ' Grader')
domoticz.devices['Trigger request Text 3_1'].switchOn()
domoticz.devices['Text 4_1'].updateText('Utetemp ar ' .. (utetemp) .. ' Grader')
domoticz.devices['Trigger request Text 4_1'].switchOn()
if domoticz.devices['Lockstatus'].state == 'Closed' then
domoticz.devices['Text 5_1'].updateText('Alla dorrar ar lasta')
domoticz.devices['Trigger request Text 5_1'].switchOn()
else domoticz.devices['Text 5_1'].updateText('Nagot ar oppet!')
domoticz.devices['Trigger request Text 5_1'].switchOn()
end
if (vindgust) <= 2 then
domoticz.devices['Text 6_1'].updateText('Det ar vindstilla')
domoticz.devices['Trigger request Text 6_1'].switchOn()
elseif (vindgust) >= 3 then
domoticz.devices['Text 6_1'].updateText('Det blaser nu ' .. (vindgust) .. ' m/s')
domoticz.devices['Trigger request Text 6_1'].switchOn()
end
if (regn) == 0 then
domoticz.devices['Text 7_1'].updateText('Inget regn idag!')
domoticz.devices['Trigger request Text 7_1'].switchOn()
elseif (regn) > 0 then
domoticz.devices['Text 7_1'].updateText('Det har regnat ' .. (regn) .. ' mm idag')
domoticz.devices['Trigger request Text 7_1'].switchOn()
end
if (domoticz.time.hour) >= 0 and (domoticz.time.hour) <= 4 then
domoticz.devices['Text 8_1'].updateText('Natti Natti!')
domoticz.devices['Trigger request Text 8_1'].switchOn()
elseif (domoticz.time.hour) >= 5 and (domoticz.time.hour) <= 9 then
domoticz.devices['Text 8_1'].updateText('God Morgon!')
domoticz.devices['Trigger request Text 8_1'].switchOn()
elseif (domoticz.time.hour) >= 10 and (domoticz.time.hour) <= 16 then
domoticz.devices['Text 8_1'].updateText('God Dag!')
domoticz.devices['Trigger request Text 8_1'].switchOn()
elseif (domoticz.time.hour) >= 17 and (domoticz.time.hour) <= 23 then
domoticz.devices['Text 8_1'].updateText('God Afton!')
domoticz.devices['Trigger request Text 8_1'].switchOn()
end
end
}
elmortero wrote:I don't know if it is compulsary, but I always give an initial value, even if It is nil.
That is how is described on the dzvents github page
Verstuurd vanaf mijn SM-G903F met Tapatalk
That was included in my first code, but when it didn't work, I'll tried to take it away.
return {
active = true,
on = {
['timer'] = 'every 30 minutes'
},
data = {
prevtbath = {initial=15}
},
execute = function(domoticz)
local tempd = domoticz.devices['Temp_bathroom']
local prevt = domoticz.data.prevtbath
local diffd = domoticz.devices['bathdiff']
local precurrtemp = tempd.temperature
local idp = 2
local mult = 10^(idp or 0)
local precurrtemp = tonumber(tempd.temperature)
local diff = tonumber(precurrtemp - prevt)
local currtemp = math.floor(precurrtemp * mult +0.5) / mult
local diffround = math.floor(diff * mult +0.5) / mult
diffd.updateTemperature(diffround)
domoticz.log('Current temperature in bathroom is ' .. currtemp, domoticz.LOG_INFO)
domoticz.log('previous temperature in bathroom was ' .. prevt, domoticz.LOG_INFO)
domoticz.log('Difference is ' .. diffround, domoticz.LOG_INFO)
domoticz.data.prevtbath = currtemp
end
}
return {
active = true,
on = {
['timer'] = 'every 30 minutes'
},
data = {
prevtbath = {initial=15}
},
execute = function(domoticz)
local tempd = domoticz.devices['Temp_bathroom']
local prevt = domoticz.data.prevtbath
local diffd = domoticz.devices['bathdiff']
local precurrtemp = tempd.temperature
local idp = 2
local mult = 10^(idp or 0)
local precurrtemp = tonumber(tempd.temperature)
local diff = tonumber(precurrtemp - prevt)
local currtemp = math.floor(precurrtemp * mult +0.5) / mult
local diffround = math.floor(diff * mult +0.5) / mult
diffd.updateTemperature(diffround)
domoticz.log('Current temperature in bathroom is ' .. currtemp, domoticz.LOG_INFO)
domoticz.log('previous temperature in bathroom was ' .. prevt, domoticz.LOG_INFO)
domoticz.log('Difference is ' .. diffround, domoticz.LOG_INFO)
domoticz.data.prevtbath = currtemp
end
}
Yes, thank you!
I found the problem, you can not use Persistent data directly. When I created a local variable as you did, then it worked!