Writing temperature til text file Topic is solved

Moderator: leecollings

Post Reply
tandersen
Posts: 2
Joined: Wednesday 19 August 2020 21:34
Target OS: Windows
Domoticz version:
Contact:

Writing temperature til text file

Post 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
User avatar
psubiaco
Posts: 194
Joined: Monday 20 August 2018 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Italy
Contact:

Re: Writing temperature til text file

Post 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
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
tandersen
Posts: 2
Joined: Wednesday 19 August 2020 21:34
Target OS: Windows
Domoticz version:
Contact:

Re: Writing temperature til text file

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing temperature til text file

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
frank666
Posts: 17
Joined: Monday 07 December 2020 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: San Marino

Re: Writing temperature til text file

Post 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 1487 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
Domoticz running on Docker,Orange Pi Zero Plus
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing temperature til text file

Post 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

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
frank666
Posts: 17
Joined: Monday 07 December 2020 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: San Marino

Re: Writing temperature til text file

Post by frank666 »

@waaren wonderful !!! it works great! TNX
Domoticz running on Docker,Orange Pi Zero Plus
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing temperature til text file

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
frank666
Posts: 17
Joined: Monday 07 December 2020 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: San Marino

Re: Writing temperature til text file

Post 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.
Domoticz running on Docker,Orange Pi Zero Plus
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing temperature til text file

Post 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"  ..
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
frank666
Posts: 17
Joined: Monday 07 December 2020 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: San Marino

Re: Writing temperature til text file

Post by frank666 »

nothing. seems not to act on the variable
Domoticz running on Docker,Orange Pi Zero Plus
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Writing temperature til text file

Post 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')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
frank666
Posts: 17
Joined: Monday 07 December 2020 10:11
Target OS: Linux
Domoticz version: 2024.7
Location: San Marino

Re: Writing temperature til text file

Post 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')
Domoticz running on Docker,Orange Pi Zero Plus
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests