Going from scientific to number with decimal/period

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
YOYO
Posts: 27
Joined: Tuesday 09 June 2015 21:22
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.23393
Location: The Netherlands
Contact:

Going from scientific to number with decimal/period

Post by YOYO »

Hello,

So im still working on a script thats gonna tell me how much energy im taking from the city heating net here.
Therefor im calculation how much Gj im using on a specific moment.

The problem is that the result of the calculation comes with a verry small number:

Code: Select all

2021-01-22 21:38:00.410 Status: dzVents: Actueel vermogen van Stadsverwarming = 1.0876193105556e-05 Gj
When i put this into a virtual sensor its telling me im using a full Gj on a moment notice.
Im guessing the counter cant handle scientific numbers like e-05.
Is there a way to tell dzvents not to use the scientific notation but a normal (Real in programming language) notation?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by waaren »

YOYO wrote: Friday 22 January 2021 21:41 Is there a way to tell dzvents not to use the scientific notation but a normal (Real in programming language) notation?
dzVents = Lua and in Lua you can do

Code: Select all

local scientific = 1.0876193105556e-05
print(string.format("%f",scientific))
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
YOYO
Posts: 27
Joined: Tuesday 09 June 2015 21:22
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.23393
Location: The Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by YOYO »

Hi Waaren,

Thats works for printing i see.
But to transfer this to a virtual sensor cant be working in my reasoning as you make a string out of a number?
Try'd to look up if a .format is available for numbers but cant find something like that?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by waaren »

YOYO wrote: Friday 22 January 2021 22:54 But to transfer this to a virtual sensor cant be working in my reasoning as you make a string out of a number?
Did you try ? I see no reason why it should not work.

To be sure I tested with

Code: Select all

return {
	on = {
		timer = {
			'every minute',
		}
	},
	
	execute = function(domoticz, device)
    	local scientific = 1.0876193105556e+07
        print(string.format("%f",scientific))
	    domoticz.devices(823).updateCounter(string.format("%f",scientific))
	end
}
executes without issues.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
YOYO
Posts: 27
Joined: Tuesday 09 June 2015 21:22
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.23393
Location: The Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by YOYO »

Yes i did and got this one back:

Code: Select all

2021-01-22 23:10:00.553 Error: dzVents: Error: (3.0.2) Stadsverwarming: /config/scripts/dzVents/generated_scripts/Script #3.lua:59: attempt to index a nil value
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by waaren »

YOYO wrote: Friday 22 January 2021 23:12 Yes i did and got this one back:

Code: Select all

2021-01-22 23:10:00.553 Error: dzVents: Error: (3.0.2) Stadsverwarming: /config/scripts/dzVents/generated_scripts/Script #3.lua:59: attempt to index a nil value
As I showed in my previous post this error is not because of the string / number conversion.
Maybe I can help if you also share the script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
YOYO
Posts: 27
Joined: Tuesday 09 June 2015 21:22
Target OS: Raspberry Pi / ODroid
Domoticz version: V2.23393
Location: The Netherlands
Contact:

Re: Going from scientific to number with decimal/period

Post by YOYO »

waaren wrote: Friday 22 January 2021 23:18 As I showed in my previous post this error is not because of the string / number conversion.
Maybe I can help if you also share the script.
Seems youre right again.
I am missing the check on syntax of defined variables because a small type error is easily made.
Corrected the error and now it seems to work.

The code is no secret:

Code: Select all

return {
	on = {
		timer = {
			'every minute',              -- causes the script to be called every minute
	   },
    },
    data = {
            Gj_old = { initial = 0 }
        },
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'Stadsverwarming',
    },

	execute = function(domoticz, timer)

-- Inlezen van diverse apparaten rondom stadsverwarming (Wireless mBus)	    
local TempInSensor = domoticz.devices('Stadsverwarming Aanvoer')
local TempOutSensor = domoticz.devices('Stadsverwarming Retour')
local DebietSensor = domoticz.devices('Debiet stadsverwarming')
local Temp_IN = TempInSensor.state
local Temp_Retour = TempOutSensor.state
local Debiet = DebietSensor.state

-- Bereken Delta T van inkomend en uitgaand
local Delta_T = Temp_IN -  Temp_Retour
print('Delta T van Stadsverwarming = ' .. Delta_T ..' °C' )

-- Omzetten van debiet in m3/h naar dm3/h
local Debiet_dm3 = Debiet * 1000
print('Debiet van Stadsverwarming = ' .. Debiet_dm3 .. ' dm3/h')

-- Omrekenen van het debiet in dm3 naar een massa in kg/h
local Water_M = 1000 * Debiet --(Debiet / 3600)
print('Watermassa van Stadsverwarming = ' .. Water_M .. ' kg/h')

-- Omrekenen van de watermassa naar J/h volgens formule: Q=m*c*(delta)T
local Actueel_Vermogen_Jh = Water_M * 4186 * Delta_T
print('Actueel vermogen van Stadsverwarming = ' .. Actueel_Vermogen_Jh .. ' J/h')

-- Omrekenen van J/h naar Watts om in virutele sensor te stoppen (vermogen in W)
local Actueel_Vermogen_W = Actueel_Vermogen_Jh / 3600 
print('Actueel vermogen van Stadsverwarming = ' .. Actueel_Vermogen_W .. ' W')

-- Omrekenen van Watts naar Kilowatts
local Actueel_Vermogen_KW = Actueel_Vermogen_W / 1000
print('Actueel vermogen van Stadsverwarming = ' .. Actueel_Vermogen_KW .. ' KW')

-- Omrekenen van Watts naar kWh om in virtuele sensor te stoppen (vermogen in kwh)
local Actueel_Vermogen_kWh = Actueel_Vermogen_W / 3600000
print('Actueel vermogen van Stadsverwarming = ' .. Actueel_Vermogen_kWh .. ' kWh')

-- Omrekenen van kwh naar Gj om de meterstand bij te kunnen houden
local Actueel_Vermogen_Gj = Actueel_Vermogen_kWh * 0.0036
print('Actueel vermogen van Stadsverwarming = ' .. Actueel_Vermogen_Gj .. ' Gj')

-- Rekenen om de counter voor de Gj op te kunnen tellen
-- Vastleggen van het nieuwe berekende vermogen in Gj
local Gj_add = Actueel_Vermogen_Gj
-- Nieuwe waarde vastleggen door oude waarde op te halen uit geheugen en het huidig berekende getal op te tellen
local Gj_new = domoticz.data.Gj_old + Gj_add
-- Debug printen van beide waardes
print(''  .. domoticz.data.Gj_old .. ' Gj')
print(''  .. Gj_new .. ' Gj')
-- Zodra er gerekend is nieuwe waarde in geheugen stoppen
domoticz.data.Gj_old = Gj_new

-- Doorzetten van nieuwe waardes naar devices
domoticz.devices('Verbruik Stadsverwarming in Gj').updateCounter(string.format("%f",Gj_new),0)
domoticz.devices('Verbruik Stadsverw. in Watt').updateElectricity(domoticz.utils.round(Actueel_Vermogen_W,1),Actueel_Vermogen_kWh) 
end

}
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests