delta t lua

Moderator: leecollings

Post Reply
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

delta t lua

Post by tiga »

what is going wrong??

i have the clean temp values for the temp inside and outside.

i want to calculate the delta.....and for now print it.
local tempbinnen = 'binnen temperatuur'--only temp value from inside
local tempbuiten = 'buiten temp wu'--only temp valu from outside


commandArray = {}

delta = tempbuiten - tempbinnen
print (delta)

return commandArray
do i miss something?
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

You are trying to calculate with 2 strings as the error indicates.
This works fine:

Code: Select all

local tempbinnen = 20 --only temp value from inside
local tempbuiten = 10 --only temp valu from outside


commandArray = {}

delta = tempbuiten - tempbinnen
print(delta)

return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

thanks for your answer jos.

the answer is -10
but i made up 2 dummy devices in temperature that i want to subtract from eachother.that must be possible right?
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

Sure. Could be that a string is returned in which case you simple convert it to a number with tonumber() like:

Code: Select all

local tempbinnen = '20'--only temp value from inside
local tempbuiten = '10'--only temp valu from outside


commandArray = {}

delta = tonumber(tempbuiten) - tonumber(tempbinnen)
print(delta)

return commandArray
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: delta t lua

Post by georgesattali »

Hello,
here's my proposal :

Code: Select all

local tempbinnenDeviceName = 'binnen temperatuur'--only temp value from inside
local tempbuitenDeviceName = 'buiten temp wu'--only temp valu from outside

local tempbinnen = otherdevices_temperature{tempbinnenDeviceName]
local tempbuiten = otherdevices_temperature{tempbuitenDeviceName]

commandArray = {}

delta = tempbuiten - tempbinnen
print (delta)

return commandArray
See you,
GD
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

thanks you helped me a lot!!!!!!

i am very thankfull.
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

ok...this is how my script looks now:

Code: Select all

local tempbinnenDeviceName = 'binnen temperatuur'--only temp value from inside
local tempbuitenDeviceName = 'buiten temp wu'--only temp valu from outside

local tempbinnen = otherdevices_temperature[tempbinnenDeviceName]
local tempbuiten = otherdevices_temperature[tempbuitenDeviceName]

commandArray = {}

delta = tempbuiten - tempbinnen
print (delta)
print(tempbinnen)
print(tempbuiten)

commandArray = {['Variable:deltaT'] = tostring(delta)}

return commandArray
the reason sor this script is to generate a uservariable Delta with a ONE decimal value....but....
this comes up:LUA: -7.3999996185303 a number with 13 decimals.
thats a little to much so i added print tempbinnen and print tempbuiten to figure out what couses it.this is the log:

2016-06-25 21:39:00.080 LUA: -7.3999996185303 (delta)
2016-06-25 21:39:00.080 LUA: 23 (temp binnen) (this is an exact 23 but normaly 1 decimal value)
2016-06-25 21:39:00.080 LUA: 15.60000038147 (temp buiten)

so tempbuiten was the issue i thougt.this is a value from weather underground that i split up.
but when i print the value from this device in my split script it prints out a nice 15.6

it is a mistery to me why this happens.

can i make it a one decimal value again in my delta variable?
thanks
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

This example contains a Round function that will help rounding values:

Code: Select all

function Round(num, idp)
	return tonumber(string.format("%." ..(idp or 0).. "f", num))
end

local tempbinnen = 23.0 --only temp value from inside
local tempbuiten = 15.60000038147 --only temp valu from outside

--~ local tempbinnenDeviceName = 'binnen temperatuur'--only temp value from inside
--~ local tempbuitenDeviceName = 'buiten temp wu'--only temp valu from outside

--~ local tempbinnen = otherdevices_temperature[tempbinnenDeviceName]
--~ local tempbuiten = otherdevices_temperature[tempbuitenDeviceName]

commandArray = {}

delta = tempbuiten - tempbinnen
print(delta)
delta = Round(delta, 1)
print(delta)
print(tempbinnen)
print(tempbuiten)

--~ commandArray['Variable:deltaT'] = tostring(delta)}

return commandArray
Think the commented line is the correct format for updating with the commandArray[] table.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

thanks jos that did the job.script looks like this now

Code: Select all

function Round(num, idp)
   return tonumber(string.format("%." ..(idp or 0).. "f", num))
end

local tempbinnenDeviceName = 'binnen temperatuur'--only temp value from inside
local tempbuitenDeviceName = 'buiten temp wu'--only temp valu from outside

local tempbinnen = otherdevices_temperature[tempbinnenDeviceName]
local tempbuiten = otherdevices_temperature[tempbuitenDeviceName]

commandArray = {}

delta = tempbuiten - tempbinnen
delta = Round(delta, 1)
print (delta)

commandArray = {['Variable:deltaT'] = tostring(delta)}

return commandArray
but now i did a simple test:
delta.png
delta.png (12.31 KiB) Viewed 4042 times
and !!tada!! nothing happens.... :cry:

lua script runs on time so updates my deltaT variable every minute.
but when i correct the user variable with the same value by hand then it works......

how is that possible????
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

Funny, you are the second one asking this today. See my answer here: http://www.domoticz.com/forum/viewtopic ... 490#p89503

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

ok that makes it a little bit complicated for me.so if understand correctly i have to update my user variable via json with a variable number....delta t
can you help me with that line please jos?
i realy want to use blockly from here on with my delta value :)

i know a little about json but what do i have to fill in here : vvalue=.?.?.?.
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

Can't test but something like this should be close:

Code: Select all

commandArray['OpenURL']='127.0.0.1/json.htm?type=command&param=updateuservariable&vname=deltaT&vtype=0&vvalue='..tostring(delta)
Coding is fun isn't it? :)

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
tiga
Posts: 159
Joined: Friday 27 May 2016 20:15
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: nederland
Contact:

Re: delta t lua

Post by tiga »

ja it is is fun if it works :lol:
im not that good but this works jos:
commandArray['OpenURL']='192.168.0.115:8080/json.htm?type=command&param=updateuservariable&vname=deltaT&vtype=1&vvalue='..tostring(delta)

perfect man jou helped me a lot thanks!!!!!
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

ha yea, you needed a float since you have decimals. Just thought I make a generic function for my general function lua file so I can also use this going forward.

Code: Select all

--
--
-- Update variable and ensure the Event system will trigger the variable event scripts
-- param2; Vartype =
-- 		0 = Integer, e.g. -1, 1, 0, 2,10
-- 		1 = Float, e.g. -1.1, 1.2, 3.1
-- 		2 = String
-- 		3 = Date in format DD/MM/YYYY
-- 		4 = Time in 24 hr format HH:MM
function UpdateVariable(Varname, Vartype, Value)
	if Varname == nil then
		print("function UpdateVariable param:Varname is nil")
		return
	end
	if Varname == nil then
		print("function UpdateVariable param:Vartype is nil")
		return
	end
	if Varname == nil then
		print("function UpdateVariable param:Value is nil")
		return
	end
	commandArray['OpenURL'] = '127.0.0.1/json.htm?type=command&param=updateuservariable&vname='..Varname..'&vtype='..Vartype..'&vvalue='..tostring(Value)
end
So you can simply call it in your script like:

Code: Select all

UpdateVariable("deltaT", 1, delta)
Enjoy,
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

Re: delta t lua

Post by commodore white »

You REALLY should check out http://www.domoticz.com/forum/viewtopic ... 23&t=10834. You'll find it a whole lot easier to access device values rather than mess with JSON.

To help with printing out floating point variables, I use a slightly modified print statement defined thus:

local printf = function(s,...) print(s:format(...)) end

You can still construct strings to print the old way but you can also print values and have the print function honour printing floating point numbers to different precisions and do string alignment. This I got from the LUA string formating library:

s:format(e1, e2, ...)

Create a formatted string from the format and arguments provided. This is similar to the printf("format",...) function in C. An additional option %q puts quotes around a string argument's value.

c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument.
q and s expect a string.
> = string.format("%s %q", "Hello", "Lua user!") -- string and quoted string
Hello "Lua user!"
> = string.format("%c%c%c", 76,117,97) -- char
Lua
> = string.format("%e, %E", math.pi,math.pi) -- exponent
3.141593e+000, 3.141593E+000
> = string.format("%f, %g", math.pi,math.pi) -- float and compact float
3.141593, 3.14159
> = string.format("%d, %i, %u", -100,-100,-100) -- signed, signed, unsigned integer
-100, -100, 4294967196
> = string.format("%o, %x, %X", -100,-100,-100) -- octal, hex, hex
37777777634, ffffff9c, FFFFFF9C

Try it. You'll like it!

Regards
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: delta t lua

Post by jvdz »

commodore white wrote:You REALLY should check out http://www.domoticz.com/forum/viewtopic ... 23&t=10834. You'll find it a whole lot easier to access device values rather than mess with JSON.
Agree it is a pretty nice project, although I prefer coding myself as that is part of the fun for me. :)
Either way, it is still LUA scripting so will have the same limitation as I've pointed out earlier.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest