Milliseconds since epoch timestamp

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

Moderator: leecollings

Post Reply
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Milliseconds since epoch timestamp

Post 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 ] ...
            
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Re: Milliseconds since epoch timestamp

Post by RvdM »

Also tried this;

Code: Select all

        local timestamp = os.execute('echo $((`date +%s`*1000+`date +%-N`/1000000))')
But this returns "true"
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Milliseconds since epoch timestamp

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
RvdM
Posts: 39
Joined: Friday 10 July 2015 21:06
Target OS: Linux
Domoticz version: 2020.2 1
Location: Deventer, NL
Contact:

Re: Milliseconds since epoch timestamp

Post 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
}
Running virtualized domoticz server on a proxmox cluster with a slave for optimal RFX communication. Using Aeotec Z-Stick Gen5, rfxtrx433e, Homebridge and a Philips HUE bridge for all sort of home automation.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Milliseconds since epoch timestamp

Post 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','')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest