Page 1 of 2

LUA Script: Time how much a device is ON

Posted: Saturday 12 August 2017 14:17
by ycahome
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 12244 times
Timer device:
yianniss5tmr.JPG
yianniss5tmr.JPG (12.04 KiB) Viewed 12244 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



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

Posted: Thursday 17 August 2017 6:22
by Wob76
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

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

Posted: Thursday 17 August 2017 10:03
by ycahome
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)".

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

Posted: Thursday 17 August 2017 11:35
by Wob76
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


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

Posted: Thursday 17 August 2017 12:03
by ycahome
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?

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

Posted: Thursday 17 August 2017 23:55
by Nautilus
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...:)

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

Posted: Friday 18 August 2017 1:06
by Wob76
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

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

Posted: Friday 18 August 2017 1:24
by Wob76
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 12078 times
I am running Domoticz Version: 3.8280

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

Posted: Friday 18 August 2017 1:55
by Wob76
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

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

Posted: Friday 18 August 2017 10:10
by ycahome
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.

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

Posted: Friday 18 August 2017 13:28
by Wob76
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


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

Posted: Wednesday 10 January 2018 16:52
by JanAtHome
Thanks
Verry useful, works fine

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

Posted: Sunday 17 June 2018 19:50
by heistje
Hi guys,

thank you for this script, works rather good but when I try to use the value of the incremental counter I get the total incremental value (total of all days, not of today).
I would like to work with the total of today... is this possible? How?
timer.JPG
timer.JPG (16.61 KiB) Viewed 10119 times
I'm using following syntax:
tonumber(otherdevices["Zwembad filterpomp (TMR)"])
where "Zwembad filterpomp" is the device from what I would like to know how long it was "ON" today..
but it returns, as already said, the total of all days.

Thx for your help

LUA Script: Time how much a device is ON

Posted: Sunday 17 June 2018 23:13
by mvzut
This has always been the behavior of counters in Domoticz. It shows the daily increment in the dashboard, but I have never found a way to retrieve this number from the device in LUA.

I ended up maintaining the daily status in a separate device, I used a dummy device of the type "Custom Sensor" for this. I increase the value of this sensor every minute that the corresponding device is on (just like the total counter), and I set it back to 0 every night at 0:00h using a timer script.
Image

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

Posted: Monday 18 June 2018 10:14
by EricT
by mvzut » Sunday 17 June 2018 23:13

This has always been the behavior of counters in Domoticz. It shows the daily increment in the dashboard, but I have never found a way to retrieve this number from the device in LUA.
Same here, have been searching for it and couldn't find it either. :(
I ended up maintaining the daily status in a separate device, I used a dummy device of the type "Custom Sensor" for this. I increase the value of this sensor every minute that the corresponding device is on (just like the total counter), and I set it back to 0 every night at 0:00h using a timer script.
mvzut,

Can you share that script here please?

Thanks,

Eric

LUA Script: Time how much a device is ON

Posted: Monday 18 June 2018 16:54
by mvzut
EricT wrote:mvzut,

Can you share that script here please?

Thanks,

Eric
Sure!

To read & update the two devices shown in my post above, I use the following code in a time-based lua script:

Code: Select all

 -- Update PC Niels timer
TotalMinutesOn = otherdevices_svalues['PC Niels timer']
TodayMinutesOn = otherdevices_svalues['PC Niels vandaag']
if otherdevices['PC Niels'] == 'On' then
  TotalMinutesOn = TotalMinutesOn + 1
  TodayMinutesOn = TodayMinutesOn + 1
end
table.insert(commandArray, {['UpdateDevice'] = otherdevices_idx['PC Niels timer'] .. '|0|' .. TotalMinutesOn})
table.insert(commandArray, {['UpdateDevice'] = otherdevices_idx['PC Niels vandaag'] .. '|0|' .. TodayMinutesOn})
To reset the value of the day timer every night I use the following code (also in a time-based script):

Code: Select all

Time = os.date("%H:%M")
if Time == '00:00' then
  table.insert(commandArray, {['UpdateDevice'] = otherdevices_idx['PC Niels vandaag'] .. '|0|0'})
end
You may have to rewrite things a bit to make it compliant with the script suggested by the OP.

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

Posted: Monday 18 June 2018 17:26
by EricT
Hi mvzut,

Exellent, thank you very much! :D

Eric

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

Posted: Sunday 01 July 2018 21:47
by Ray-Man
Just a minor thing if you also want to time dimmers:
change

Code: Select all

if (otherdevices[vname] == 'On') then
to

Code: Select all

if (otherdevices[vname] ~= 'Off') then
Obvious to most I guess, but if you're not into LUA it might help....

Just my to cents.......

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

Posted: Friday 07 September 2018 15:03
by gajotnt
Using this script to easilly know how much time my pool pump has been runing for each day, but havent figured out how to set it up to send me a message when it has reach a certain time, have tried several ways none of it works on this dummy device.
tried with just 130, with 130 minutes, the type tried Today, tried Update.
And yes i have notifications working on other switches/counters
blocky.JPG
blocky.JPG (23.17 KiB) Viewed 8948 times
notifications.JPG
notifications.JPG (72.06 KiB) Viewed 8948 times

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

Posted: Friday 22 February 2019 16:24
by hemant5400z
Hi,

I'm trying to use this timer function with version 4.10265, but cannot seem to get is working.

I have:
1 switch called test; light/switch, Switch, on/off
1 utility called test (TMR), General, Counter Incremental, type is Time, counter divider 0, Meter Offset 0

I can see the event triggered in the log but the utility shows a "?" and keeps at 0. When i change the utility to counter instead of time I do see increase of the number but no time is showing just an integer.

Anyone who is able t help?
Cheers,
Hemant