How to get the time a switch is activated in log/notification

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

Moderator: leecollings

Post Reply
Martinq
Posts: 19
Joined: Wednesday 09 March 2016 17:20
Target OS: Linux
Domoticz version: 3.8153
Contact:

How to get the time a switch is activated in log/notification

Post by Martinq »

I am trying to get the time a switch is turned on into the log. Tried it like this:

Code: Select all

return {
	on = {
		devices = {
			'Keypad Access Control'
		}
	},
	execute = function(domoticz, device)
	    if (device.state == 'On') then
         domoticz.log('Device ' .. device.name .. ' is ingeschakeld om: ' .. device.lastUpdate.raw .. '', domoticz.LOG_INFO)
        else
		 domoticz.log('Device ' .. device.name .. ' is uitgeschakeld om: ' .. device.lastUpdate.raw .. '', domoticz.LOG_INFO)
        end
    end
}
But this is not giving the time the switch was switched ON but the last time it was switched OFF.

You can see it in the log:

Code: Select all

2018-09-24 23:03:23.723 Status: User: Admin initiated a switch command (21/Keypad Access Control/On)
2018-09-24 23:03:23.838 Status: dzVents: Info: Handling events for: "Keypad Access Control", value: "On"
2018-09-24 23:03:23.839 Status: dzVents: Info: ------ Start internal script: myAlarm_keypad: Device: "Keypad Access Control (ideAlarm)", Index: 21
2018-09-24 23:03:23.839 Status: dzVents: Info: Device Keypad Access Control is ingeschakeld om: 2018-09-24 22:48:13
2018-09-24 23:03:23.839 Status: dzVents: Info: ------ Finished myAlarm_keypad
2018-09-24 23:04:06.894 Status: User: Admin initiated a switch command (21/Keypad Access Control/Off)
2018-09-24 23:04:07.009 Status: dzVents: Info: Handling events for: "Keypad Access Control", value: "Off"
2018-09-24 23:04:07.009 Status: dzVents: Info: ------ Start internal script: myAlarm_keypad: Device: "Keypad Access Control (ideAlarm)", Index: 21
2018-09-24 23:04:07.009 Status: dzVents: Info: Device Keypad Access Control is uitgeschakeld om: 2018-09-24 23:03:23
2018-09-24 23:04:07.010 Status: dzVents: Info: ------ Finished myAlarm_keypad
So:
Switch ON at: 23:03:23
In the log: 22:48:13
Switch OFF at: 23:04:07
In the log: 23:03:23

It must be like this:
Switch ON at: 23:03:23
In the log: 23:03:23
Switch OFF at: 23:04:07
In the log: 23:04:07

Can somebody help me with this?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get the time a switch is activated in log/notification

Post by waaren »

Martinq wrote: Monday 24 September 2018 23:08 ........
I am trying to get the time a switch is turned on into the log
........
If the script is triggered by the device, the device.lastUpdate.raw will return the time the device was last updated before the event that triggered the script because that is the info that is passed from domoticz to dzVents at that moment. (this is what you see happening)

If you want to see the date / time the device was switched and therefore the script was triggered, there are a couple of options:

- Look at the log. the date /time the script started is there.
This is not always accurate as domoticz will only execute 1 script at a time. 2nd script will wait until 1st script has finished.

- Change domoticz.lastUpate.raw into domoticz.time.raw in your script
This is also not always accurate for the same reason as above

- Rebuild your script in such a way that you ask domoticz for the last Update time of the device from within the script.
More complex but accurate. See below.

Code: Select all

 return {
   on = {  devices         = { "Keypad Access Control" },
            httpResponses   = { "exactTime" }},

    execute = function(dz, trigger)
    
        local function getDomoticzDevice(idx)                                 -- this function calls local domoticz process and ask for device data
            local jsonString  = "/json.htm?type=devices&rid=" .. idx  
            dz.openURL  ({ url      = dz.settings["Domoticz url"] .. jsonString,
                           method   = "GET", callback = "exactTime" })
        end
        
        if trigger.isHTTPResponse then
            rt = trigger.json.result[1]   -- load json return into Lua table 
            if rt.status == "On" then
                dz.log("Device " .. rt.Name .. " is ingeschakeld om: " .. rt.LastUpdate, dz.LOG_INFO)
            else
                dz.log("Device " .. rt.Name .. " is uitgeschakeld om: " .. rt.LastUpdate, dz.LOG_INFO)
            end
        else
            getDomoticzDevice(trigger.idx)
        end        
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Martinq
Posts: 19
Joined: Wednesday 09 March 2016 17:20
Target OS: Linux
Domoticz version: 3.8153
Contact:

Re: How to get the time a switch is activated in log/notification

Post by Martinq »

Thanks!

I used the second option (domoticz.time.raw), this time is accurate enough. And with your explanation it makes sense why I got the wrong time.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest