LUA Script: Time how much a device is ON

Post Reply
User avatar
ycahome
Posts: 70
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

LUA Script: Time how much a device is ON

Post by ycahome » Saturday 12 August 2017 14:17

Hi there,

just finished the first version of the "script_time_switch-timer" LUA script.
Its a modified version of "Time Script to Turn a Light Off"
on "https://www.domoticz.com/wiki/Smart_Lua_Script"

With this script:
if you create a "General, Counter incremental" with a name of a switch prefixed by " (TMR)" this will count
when your switch is ON.

Domoticz Installation instructions:
- Save this script as "script_time_switch-timer.lua" on "Domoticz/scripts/lua" folder
- Add a new "General, Counter incremental" with a name of a switch prefixed by " (TMR)" for each switch you want to time.

example:
Timed switch:
yianniss5.JPG
yianniss5.JPG (17.13 KiB) Viewed 666 times
Timer device:
yianniss5tmr.JPG
yianniss5tmr.JPG (12.04 KiB) Viewed 666 times
Hope you like it!!

Code: Select all


--
--   Switch Timer for Domoticz via LUA Script
--
--   Ycahome, 2017
--   https://www.domoticz.com/forum
--
--

function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

function trim1(s)
  return (s:gsub("^%s*(.-)%s*$", "%1"))
end


commandArray = {}
debuglevel = 0
 

local function update(idx, value1, value2)
    local cmd = idx.."|"..value1.."|"..value2
    --local cmd = string.format("%d|0|%f;%f", idx, value1, value2)
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end



for i, v in pairs(otherdevices) do
   timeon = 240
   tc = tostring(i)
   --v = i:sub(1,4)
   v = string.sub(i, -5)

   if (v == '(TMR)') then
     if (debuglevel > 0) then print("Timer Device Detected:".. i) end
     tc2 = string.len(i)
     --print("Len:".. tc2)
     vname = trim1(tostring(string.sub(i,1,tc2-5)))
     if (otherdevices[vname] == 'On') then
        if (debuglevel > 0) then print("Timed Device:".. vname) end
        print("Timed Device Detected ON:" ..vname)
        if (debuglevel > 0) then print("Timer Dev Idx:".. otherdevices_idx[i]) end
        if (debuglevel > 0) then print("Timed Dev Idx:".. otherdevices_idx[vname]) end
        if (debuglevel > 0) then print("Current Value:".. otherdevices[i]) end
        newvalue = otherdevices[i] + 1
        --newvalue = 1
        if (debuglevel > 0) then print("New Value:".. newvalue) end
        --commandArray['UpdateDevice'] = otherdevices_idx[i]..'|1|'..newvalue
        update (otherdevices_idx[i], 1, newvalue)
     end
   end

   if (v == 'PIR----') then
      difference = timedifference(otherdevices_lastupdate[tc])
      if (difference > timeon and difference < (timeon + 60)) then
         tempdiff = tostring(difference)
         c = i:sub(4)
         tempmessage = c.." Light Off - after at least " .. (timeon+1) .. "secs up - actually - " .. tempdiff .. "seconds"
         print(tempmessage)
         commandArray[c] = 'Off'
      end
   end
end
 
return commandArray



Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Thursday 17 August 2017 6:22

Hi,

Thanks for the script, I thought i would give it a try, but think I am missing something, it is counting way to fast.

Does it only count from the time you start running it or does it do a count back using the logs?

I created a dummy incremental counter, the Type is set to count, the Offset 0 and I entered Time if Quantity and mins in Value Unit.

Thanks,
Wob

User avatar
ycahome
Posts: 70
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: LUA Script: Time how much a device is ON

Post by ycahome » Thursday 17 August 2017 10:03

Hello Wob76.

LUA scripts executed every minute on Domoticz. So, this script just perform the calculations (discovering "(TMR)" devices and their counterparts)
and increasing "*(TMR)" value by 1.

In fact, starts to count as soon as run it.

Your counter (from what you said) seems correct. Be sure to name this device correctly <name of switch>+"(TMR)".

Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Thursday 17 August 2017 11:35

Thanks for the reply.

It seems to match up fine, I can see it detected in the log, but after about 60mins from install my counter is showing 249mims, the switch has been on only about 3 hours all day, any idea why it would count to much?

Thanks,
Wob

Sent from my SM-G935F using Tapatalk


User avatar
ycahome
Posts: 70
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: LUA Script: Time how much a device is ON

Post by ycahome » Thursday 17 August 2017 12:03

i don't know if its a Domoticz version difference (my Version: 3.8153) or something else.

my log clearly display firings every 1min:
2017-08-17 13:01:03.504 EventSystem: Script event triggered: /home/yiannis/domoticz/scripts/lua/script_time_switch-timer.lua
2017-08-17 13:02:03.102 EventSystem: Script event triggered: /home/yiannis/domoticz/scripts/lua/script_time_switch-timer.lua
2017-08-17 13:03:03.221 EventSystem: Script event triggered: /home/yiannis/domoticz/scripts/lua/script_time_switch-timer.lua

can you give more details from your setup?

Nautilus
Posts: 653
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: LUA Script: Time how much a device is ON

Post by Nautilus » Thursday 17 August 2017 23:55

Sounds like a classic case of defining the script as "device" or "all" type of script (triggering at any device change) when in fact it should be set as time script (runs only once a minute). Hope this helps...:)

Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Friday 18 August 2017 1:06

Nautilus wrote:
Thursday 17 August 2017 23:55
Sounds like a classic case of defining the script as "device" or "all" type of script (triggering at any device change) when in fact it should be set as time script (runs only once a minute). Hope this helps...:)
You are on the money, I haven't actually used any lua scripts (I have been using dzVents), and I had saved it using the Events interface (default is all).

Now if I change it to "Time" nothing at all happens.

So I went back and followed the instructions and manually saved the script as domoticz/scripts/lua/script_time_switch-timer.lua. But it still doesn't trigger.

I have also restarted Domoticz, what am I missing? My dzVents scripts are firing as expected.

Thanks,
Wob

Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Friday 18 August 2017 1:24

OK, I enabled debugging in the script and I can see it firing, but it is only detecting the switch, it is not counting it as on.

Code: Select all

2017-08-18 09:15:00.032 LUA: Timer Device Detected:Test Switch (TMR)
2017-08-18 09:16:00.041 LUA: Timer Device Detected:Test Switch (TMR)
Timer.JPG
Timer.JPG (27.46 KiB) Viewed 499 times
I am running Domoticz Version: 3.8280

Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Friday 18 August 2017 1:55

OK, I added a second timer device for my AC, the unit I actually want to monitor, and the Test Switch started to work, seems to be counting properly now. Just need to wait for the AC to be used :)

Edit: I just set a scene to turn the AC on for 2 min, script counted 1 min, but I think that was prob just timing of the script, so I am calling it working.

Edit2: My partner used the AC today, and time shown on the counter matches up with the logs of the switch, so all good.

Thanks for the help.

Wob

User avatar
ycahome
Posts: 70
Joined: Sunday 12 February 2017 11:55
Target OS: Linux
Domoticz version: beta
Contact:

Re: LUA Script: Time how much a device is ON

Post by ycahome » Friday 18 August 2017 10:10

Glad to hear that this worked for you.

This script is a workaround for a feature that is missing from the core of Domoticz.

This timer has a big problem. If a switch turned on at "10:01" and off at "10:59" nothing will be recorded (58 seconds lost from counter).

Am checking if this can be implemented with "OnAction" and "OffAction" scripts. But -for now- ,current functionality, seems enough.

Wob76
Posts: 55
Joined: Wednesday 19 April 2017 6:31
Target OS: Linux
Domoticz version:
Contact:

Re: LUA Script: Time how much a device is ON

Post by Wob76 » Friday 18 August 2017 13:28

It does seem like something that could be introduced at the core, the system records timestamps for on off, so wouldn't be to hard to include running time stats. This seems good enough for my needs, but I'll be interested in how you go.

Wob

Sent from my SM-G935F using Tapatalk


Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests