Page 1 of 1

problems with Persistent data [SOLVED]

Posted: Wednesday 11 January 2017 17:28
by gus
Hi,

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.

This is my code, please advice a LUA dummies...

Code: Select all

              
        data = {
        barometer_old = {},
    },


		if (barometer) ~= (barometer_old) then
			domoticz.devices['Trigger request Text 2_1'].switchOn()
			domoticz.data.barometer_old = (barometer)
		end

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 17:32
by Egregius
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...

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 18:13
by gus
Ok, some complements!


This is th values from my log:

Code: Select all

2017-01-11 17:12:00.942 LUA: barometer_old value: 976
2017-01-11 17:12:00.943 LUA: barometer value: 976

This is my code:

Code: Select all

-- ------------------------------------
-- 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
}

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 18:37
by elmortero
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

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 18:49
by gus
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.

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 19:24
by elmortero
In that case I wouldn't know.

This is a working script I use that is similar to what you want, maybe you find something that can help you

Code: Select all

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
}

Re: problems with Persistent data

Posted: Wednesday 11 January 2017 23:03
by gus
elmortero wrote:In that case I wouldn't know.

This is a working script I use that is similar to what you want, maybe you find something that can help you

Code: Select all

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!

Code: Select all

local prevt = domoticz.data.prevtbath

Re: problems with Persistent data [SOLVED]

Posted: Wednesday 11 January 2017 23:13
by Doler
It would already have worked if you had changed

Code: Select all

if (barometer) ~= (barometer_old) then
into

Code: Select all

if (barometer) ~= (domoticz.data.barometer_old) then
. No special need to put it into a local variable other than nicer code.

Re: problems with Persistent data [SOLVED]

Posted: Thursday 12 January 2017 7:53
by gus
Doler wrote:It would already have worked if you had changed

Code: Select all

if (barometer) ~= (barometer_old) then
into

Code: Select all

if (barometer) ~= (domoticz.data.barometer_old) then
. No special need to put it into a local variable other than nicer code.
Correct, thx!