Page 1 of 1

Writing temperature til text file

Posted: Wednesday 19 August 2020 21:46
by tandersen
Hi folks, I am a newbee using domoticz on Windows 10 to read four thermometers and control three switches. For my Blue Iris camera surveillance system, I need regularly (every 15 minutes or so) to write a temperature and humidity to two text files. What is the best way to accomplish this?
I hope for your patience if it's a stupid question :D

Re: Writing temperature til text file

Posted: Wednesday 19 August 2020 21:59
by psubiaco
Hi Tandersen,
it's easy to write a LUA script that regularly write the temperature value on a text file
You have to create the script file domoticz/scripts/lua/script_time_temperature.lua
within something like this:

Code: Select all

file=io.open(/path/filename,'w')
-- replace /path/filename with the file that should be written
io.output(file)
io.write("Temperature="..otherdevices_svalues['Temperature_devicename'])
-- Replace Temperature_devicename with the name of temperature device in Domoticz
This simple script will write the temperature every minute. I did not try it, so it's possible that it does not work!!
Regards.
Paolo

Re: Writing temperature til text file

Posted: Saturday 29 August 2020 20:42
by tandersen
H psubiaco,

Thanks so much for your hint. It took me a while to get back because of other important stuff but I have now added your script to the Lua file, so it runs automatically. My sensor gives out both temperature and humidity, so I had to add extraction of a substring but apart from that it worked beautifully:

Code: Select all

print('Executing script_time_logForBI')

commandArray = {}
	
	file1=io.open('C:/Users/User/Documents/Light control/c-programs/readThermometersNew/temperature.txt','w')
	io.output(file1)
	print(otherdevices_svalues)
	io.write(string.sub(otherdevices_svalues['Outdoor'],1,4).."°C")
	
	file2=io.open('C:/Users/User/Documents/Light control/c-programs/readThermometersNew/humidity.txt','w')
	io.output(file2)
	print(otherdevices_svalues)
	io.write(string.sub(otherdevices_svalues['Outdoor'],6,7).."%RH")
	
return commandArray
It would have been enough to write this file every 15 minutes and not every 1 minute but of course that would be more complicated.

Best regards from Sweden

Re: Writing temperature til text file

Posted: Saturday 29 August 2020 22:33
by waaren
tandersen wrote: Saturday 29 August 2020 20:42 It would have been enough to write this file every 15 minutes and not every 1 minute but of course that would be more complicated.

Best regards from Sweden
can you try with this ?

Code: Select all

print('Executing script_time_logForBI')

local executeAt = { 1, 17, 31, 47 } -- minutes the script need to execute

local function tableContains(t, v)
	for _, value in pairs(t) do
		if v == value then return true end
	end
	return false
end

commandArray = {}

	if tableContains(executeAt, math.floor(os.date('%M'))) then

		file1=io.open('C:/Users/User/Documents/Light control/c-programs/readThermometersNew/temperature.txt','w')
		io.output(file1)
		print(otherdevices_svalues)
		io.write(string.sub(otherdevices_svalues['Outdoor'],1,4).."°C")
		
		file2=io.open('C:/Users/User/Documents/Light control/c-programs/readThermometersNew/humidity.txt','w')
		io.output(file2)
		print(otherdevices_svalues)
		io.write(string.sub(otherdevices_svalues['Outdoor'],6,7).."%RH")
	
	end

return commandArray

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 9:22
by frank666
thank you for your suggestions, i was able to publish the data of a bmp280 sensor online.
I would also like to report the date and time of detection but I can't find the variable in domoticz.
As I can do, I enclose a screenshot that shows which data I need.
Schermata da 2020-12-08 09-07-03.png
Schermata da 2020-12-08 09-07-03.png (17.17 KiB) Viewed 1673 times

Code: Select all

print('Executing script_time_logForBI')
local executeAt = { 1, 6, 12, 18, 24, 30, 36, 42, 48, 54, 59 } -- minutes the script need to execute

local function tableContains(t, v)
        for _, value in pairs(t) do
                if v == value then return true end
        end
        return false
end

commandArray = {}

        if tableContains(executeAt, math.floor(os.date('%M'))) then

                file1=io.open('/tmp/temperature.js','w')
                io.output(file1)
                print(otherdevices_svalues)
                io.write "document.write ('Temperature:"
                io.write (string.sub(otherdevices_svalues['bmp280'],1,4).."&degC Pressure sea level:")
                io.write(string.sub(otherdevices_svalues['bmp280'],13,16).."Hpa ")
                io.write "')"

end

return commandArray

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 10:31
by waaren
frank666 wrote: Tuesday 08 December 2020 9:22 thank you for your suggestions, i was able to publish the data of a bmp280 sensor online.
I would also like to report the date and time of detection but I can't find the variable in domoticz.
This should do it

Code: Select all

print('Executing script_time_logForBI')
local executeAt = { 1, 6, 12, 18, 24, 30, 36, 42, 48, 54, 59 } -- minutes the script need to execute
local devicename = 'bmp280'
local filename = '/tmp/temperature.js'

local function tableContains(t, v)
    for _, value in pairs(t) do
        if v == value then return true end
    end
    return false
end

commandArray = {}

    if tableContains(executeAt, math.floor(os.date('%M'))) then -- check minutes
        print(otherdevices_svalues[devicename])
        local line =    "document.write ('" ..
                        "Temperature:" .. string.sub(otherdevices_svalues[devicename],1,4).."&degC" ..
                        ", Pressure sea level: " .. string.sub(otherdevices_svalues[devicename],13,16).." Hpa" ..
                        ", LastUpdated: " .. otherdevices_lastupdate[devicename] ..
                        "')"
        print(line)

        local file = io.open(filename,'w')
        file:write(line)
        file:close()
    end

return commandArray


Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 10:46
by frank666
@waaren wonderful !!! it works great! TNX

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 16:27
by waaren
frank666 wrote: Tuesday 08 December 2020 10:46 it works great!
Probably easier to use the otherdevices tables ?

Code: Select all

print('Executing script_time_logForBI')
local executeAt = { 1, 6, 12, 18, 24, 30, 36, 42, 48, 54, 59 } -- minutes the script need to execute
local devicename = 'bmp280'
local filename = '/tmp/temperature.js'

local function tableContains(t, v)
        for _, value in pairs(t) do
                if v == value then return true end
        end
        return false
end

commandArray = {}

    if tableContains(executeAt, math.floor(os.date('%M'))) then

        print(otherdevices_svalues[devicename])
        local line =    "document.write ('" .. 
                        "Temperature:" .. otherdevices_temperature[devicename] .."&degC" ..
                        ", Pressure sea level: " .. otherdevices_barometer[devicename] .." Hpa" ..
                        ", LastUpdated: " .. otherdevices_lastupdate[devicename] ..
                        "')"
        print(line)
        local file = io.open(filename,'w')
        file:write(line)
        file:close()
    end

return commandArray

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 20:26
by frank666
@waaren I also tried the method with otherdevices tables, but the value of the temperature contains many decimals after the comma,
I tried putting .. round(otherdevices_temperature[devicename], 2) .. but it does not round.
ps:is it possible to import in a web page the domoticz temperature charts? I will open a new post.

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 21:58
by waaren
frank666 wrote: Tuesday 08 December 2020 20:26 but it does not round.
You can round with

Code: Select all

local decimals = 2
"Temperature:" .. math.floor( otherdevices_temperature[devicename] * 10^decimals ) / 10^decimals  .."&degC"  ..

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 22:43
by frank666
nothing. seems not to act on the variable

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 23:04
by waaren
frank666 wrote: Tuesday 08 December 2020 22:43 nothing. seems not to act on the variable
:o Can you try this ?

Code: Select all

print('Executing script_time_logForBI')
local executeAt = { 1, 6, 12, 18, 24, 30, 36, 42, 48, 54, 59 } -- minutes the script need to execute
local devicename = 'bmp280'
local filename = '/tmp/temperature.js'
local decimals = 2

local function tableContains(t, v)
        for _, value in pairs(t) do
                if v == value then return true end
        end
        return false
end

commandArray = {}

    if tableContains(executeAt, math.floor(os.date('%M'))) then

        print(otherdevices_svalues[devicename])
        local line =    "document.write ('" .. 
                        "Temperature:" .. math.floor( otherdevices_temperature[devicename] * 10^decimals ) / 10^decimals  .. "&degC"  .. 
                        ", Pressure sea level: " .. otherdevices_barometer[devicename] .. " Hpa" ..
                        ", LastUpdated: " .. otherdevices_lastupdate[devicename] ..
                        "')"
        print(line)
        local file = io.open(filename,'w')
        file:write(line)
        file:close()
    end

return commandArray
Spoiler: show

Code: Select all

more /tmp/temperature.js
document.write ('Temperature:0.3&degC, Pressure sea level: 1007.0 Hpa, LastUpdated: 2020-12-08 23:00:34')

Re: Writing temperature til text file

Posted: Tuesday 08 December 2020 23:37
by frank666
🥳 works perfectly

Code: Select all

cat /tmp/temperature.js
document.write ('Temperature:1.91&degC, Pressure sea level: 1007.0 Hpa, LastUpdated: 2020-12-07 15:43:10')