How to Retrieve the TIME of a DATA entry for use in Notification  [Solved]

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

Moderator: leecollings

Post Reply
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

How to Retrieve the TIME of a DATA entry for use in Notification

Post by lastofthejediknights »

Hi,

I seem to keep falling over, trying to return the time of a (the last actually) entry in persistent data.
I wish to use this in a notification, to tell WHEN something as last done. the actual data in this case is meaningless, as it is simply binary.

Code: Select all

return {
	on = {
		devices = {
			'$FridgeOpen',
		},
		timer = {
				    'at 09:00', 'at 14:00', 'at 19:00',  'at 20:00', 'at 22:00',
				    },
	},
	
		data = {myHistoryFridge = { history = true, maxItems = 50 , maxHours = 72 }, 

	        },

	execute = function(domoticz, item)
	    
    	    local lastupdate = domoticz.devices('Fridge').LastUpdate--lastUpdate --Time(node.LastUpdate)
    	    
    	        if ((item.isDevice)) then
        			domoticz.devices('Fridge').open()
        			domoticz.data.myHistoryFridge.add(1)
        		end
        		
                if (item.isTimer) then
                        if (domoticz.devices('Fridge').lastUpdate.minutesAgo > 60 ) and (domoticz.devices('Emma At Home').state == 'On'  or domoticz.devices('Nigel At Home').state == 'On'   )  then --at 12:45-21:15 and domoticz.time.matchesRule('at 07:30-11:00') ) then --at 12:45-21:15 
                            domoticz.log(' Fridge not opened since ' .. tostring(domoticz.datalastUpdate), domoticz.LOG_INFO)
                             domoticz.notify(' Fridge not opened since ' .. tostring(domoticz.data.lastUpdate) ..' Mins Ago')
                        end
                end
            domoticz.log(' Fridge Last opened ' .. tostring(domoticz.data.lastUpdate), domoticz.LOG_INFO)
        
	end
}
i know I can do domoticz.data.myHistoryFridge.lastUpdate.minutesAgo
However I am trying to output the actual timestamp
User avatar
erem
Posts: 230
Joined: Tuesday 27 March 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Amsterdam/netherlands
Contact:

Re: How to Retrieve the TIME of a DATA entry for use in Notification

Post by erem »

just an idea

if you can do domoticz.data.myHistoryFridge.lastUpdate.minutesAgo,
you can also do (now - domoticz.data.myHistoryFridge.lastUpdate.minutesAgo) to ge the time
Regards,

Rob
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

Re: How to Retrieve the TIME of a DATA entry for use in Notification

Post by lastofthejediknights »

local lastupdate = domoticz.devices('Fridge').LastUpdate--lastUpdate --Time(node.LastUpdate)
local Time = require('Time')
local now = Time() -- current time

domoticz.log(' Fridge Last opened ' .. tostring(now - domoticz.devices('Fridge').lastUpdate.minutesAgo), domoticz.LOG_INFO)

gives an error of

2021-08-27 08:54:01.150 Error: dzVents: Error: (3.1.7) ...omoticz\scripts\dzVents\generated_scripts/FridgeOpen.lua:33: attempt to perform arithmetic on a table value (local 'now')
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

Re: How to Retrieve the TIME of a DATA entry for use in Notification  [Solved]

Post by lastofthejediknights »

solved

Code: Select all

return {
	on = {
		devices = {
			'$FridgeOpen',
		},
		timer = {
				    'at 09:00', 'at 13:29', 'at 19:00',  'at 20:00', 'at 22:00',--'every minute',
				    },
	},
	
		data = {myHistoryFridge = { history = true, maxItems = 50 , maxHours = 72 }, 

	        },

	execute = function(domoticz, item)
 
    	    
    	        if ((item.isDevice)) then
        			domoticz.devices('Fridge').open()
        			domoticz.data.myHistoryFridge.add(1)
        		end
        		
        		 local LastUpdateSec = domoticz.data.myHistoryFridge.getLatest()
        		
                if (item.isTimer) then
                        if (domoticz.devices('Fridge').lastUpdate.minutesAgo > 180 ) and (domoticz.devices('Emma At Home').state == 'On'  or domoticz.devices('Nigel At Home').state == 'On'   )  then --at 12:45-21:15 and domoticz.time.matchesRule('at 07:30-11:00') ) then --at 12:45-21:15 
                            domoticz.log(' Fridge not opened since ' .. tostring(domoticz.time.timestampToDate(now,'dd/mm/yy ii:MM mer nZero',( LastUpdateSec.time.secondsAgo * -1) ))) --domoticz.data.myHistoryFridge.lastUpdate
                             domoticz.notify(' Fridge not opened since ' .. tostring(domoticz.time.timestampToDate(now,'dd/mm/yy ii:MM mer nZero',( LastUpdateSec.time.secondsAgo * -1) )))
                        end
                end
            domoticz.log(' Fridge Last opened ' .. tostring(domoticz.time.timestampToDate(now,'dd/mm/yy ii:MM mer nZero',( LastUpdateSec.time.secondsAgo * -1) )))        --> '22/08/20 8:24 AM'), domoticz.LOG_INFO)
           -- print( now)
        
	end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest