Page 1 of 1
Milliseconds since epoch timestamp
Posted: Saturday 01 May 2021 21:07
by RvdM
Hi,
For the grafana API I need the '
epoch datetime in milliseconds' as timestamp.
However
domoticz.time.dDate gives me the timestamp in seconds since epoch.
How to get the milliseconds variant?
Code: Select all
... [ output truncated ] ...
local timestamp = domoticz.time.dDate
domoticz.log(timestamp)
domoticz.openURL({
url = 'http://XX.XX.XX.XX:3000/api/annotations',
headers = { ['Authorization'] = 'Bearer XXXXXXXXXXXXX', ['Content-Type'] = 'application/json' },
method = 'POST',
postData = {
dashboardId = 12,
panelId = 2,
time = timestamp,
timeEnd = timestamp,
text = "Test Annonation"
},
callback = 'info'
... [ output truncated ] ...
Re: Milliseconds since epoch timestamp
Posted: Saturday 01 May 2021 21:22
by RvdM
Also tried this;
Code: Select all
local timestamp = os.execute('echo $((`date +%s`*1000+`date +%-N`/1000000))')
But this returns "true"
Re: Milliseconds since epoch timestamp
Posted: Saturday 01 May 2021 21:44
by waaren
RvdM wrote: ↑Saturday 01 May 2021 21:22
Also tried this;
Code: Select all
local timestamp = os.execute('echo $((`date +%s`*1000+`date +%-N`/1000000))')
But this returns "true"
There is no native Lua / dzVents method to get this. From the OS you can get nanoseconds so you can use something like below.
Code: Select all
return
{
on =
{
timer =
{
'every minute',
}
},
logging =
{
level = domoticz.LOG_INFO,
marker = 'milliseconds',
},
execute = function(dz)
local function osCommand(cmd)
dz.log('Executing Command: ' .. cmd,dz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
dz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, dz.LOG_DEBUG)
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local timestamp = osCommand('date +%s%3N')
dz.log(timestamp)
end
}
Re: Milliseconds since epoch timestamp
Posted: Sunday 02 May 2021 15:31
by RvdM
waaren wrote: ↑Saturday 01 May 2021 21:44
There is no native Lua / dzVents method to get this. From the OS you can get nanoseconds so you can use something like below.
I must say waaren; it always amazes me how you are able to always answer the questions from the community. Thank you very much!
I had to make a small adjustment to the script because there was a
"\n" at the end of the command.
Solved it by replacing the command itself but perhaps this can also be done in dzVents. However I lack the skill to know how.
Code: Select all
local timestamp = osCommand('date +%s%3N| tr -d "\n"')
Full script below for anyone wanting to do the same:
Code: Select all
return {
active = true,
on = {
devices = {
'Doorsensor - Hall',
'Windowsensor - Bedroom',
'Dimmer - Mechanical Home Ventilation',
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = 'testl'
},
execute = function(domoticz, item)
local function osCommand(cmd)
domoticz.log('Executing Command: ' .. cmd,domoticz.LOG_DEBUG)
local fileHandle = assert(io.popen(cmd))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
domoticz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, domoticz.LOG_DEBUG)
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
local timestamp = osCommand('date +%s%3N | tr -d "\n"')
local annotation = 'Status of sensor or device ' .. item.name .. ' changed. New status/level: ' .. item.state .. '/' .. item.level
domoticz.openURL({
url = 'http://XXX.XXX.XXX.XXX:3000/api/annotations',
headers = { ['Authorization'] = 'Bearer XXXXX', ['Content-Type'] = 'application/json' },
method = 'POST',
postData = '{"dashboardId":XX,"panelId":X,"time":' .. timestamp .. ',"timeEnd":' .. timestamp .. ',"tags":["domoticz"],"text":"' .. annotation .. '"}',
callback = 'info'
})
end
}
Re: Milliseconds since epoch timestamp
Posted: Sunday 02 May 2021 20:55
by waaren
RvdM wrote: ↑Sunday 02 May 2021 15:31
Solved it by replacing the command itself but perhaps this can also be done in dzVents. However I lack the skill to know how.
The Lua / dzVents equivalent of the Linux tr command is string.gsub()
So you could change
Code: Select all
local timestamp = osCommand('date +%s%3N | tr -d "\n"')
to
Code: Select all
local timestamp = osCommand('date +%s%3N'):gsub('\n','')