problems with Persistent data [SOLVED]

gus
Posts: 13
Joined: Tuesday 04 November 2014 11:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

problems with Persistent data [SOLVED]

Postby gus » Wednesday 11 January 2017 17:28

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
Last edited by gus on Wednesday 11 January 2017 23:03, edited 1 time in total.

Egregius
Posts: 1906
Joined: Thursday 09 April 2015 12:19
Target OS: NAS (Synology & others)
Domoticz version: 3.6312
Location: Beitem, BE
Contact:

Re: problems with Persistent data

Postby Egregius » Wednesday 11 January 2017 17:32

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

gus
Posts: 13
Joined: Tuesday 04 November 2014 11:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: problems with Persistent data

Postby gus » Wednesday 11 January 2017 18:13

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
}

elmortero
Posts: 83
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: problems with Persistent data

Postby elmortero » Wednesday 11 January 2017 18:37

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

gus
Posts: 13
Joined: Tuesday 04 November 2014 11:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: problems with Persistent data

Postby gus » Wednesday 11 January 2017 18:49

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.

elmortero
Posts: 83
Joined: Sunday 29 November 2015 21:46
Target OS: Raspberry Pi
Domoticz version: 2.3852
Location: Spain
Contact:

Re: problems with Persistent data

Postby elmortero » Wednesday 11 January 2017 19:24

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
}

gus
Posts: 13
Joined: Tuesday 04 November 2014 11:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: problems with Persistent data

Postby gus » Wednesday 11 January 2017 23:03

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

Doler
Posts: 90
Joined: Friday 31 July 2015 21:02
Target OS: Raspberry Pi
Domoticz version: v3.6277
Location: Sint-Oedenrode, Netherlands
Contact:

Re: problems with Persistent data [SOLVED]

Postby Doler » Wednesday 11 January 2017 23:13

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.
Mark: Domoticz Beta on Raspberry Pi B2 running Debian Jessie - RaZberry - RFXCom - MySensors - SolarEdge - Nefit

gus
Posts: 13
Joined: Tuesday 04 November 2014 11:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: problems with Persistent data [SOLVED]

Postby gus » Thursday 12 January 2017 7:53

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!


Return to “dzVents Lua Framework”

Who is online

Users browsing this forum: No registered users and 1 guest