LUA Script: Time how much a device is ON

Moderator: leecollings

User avatar
ycahome
Posts: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

LUA Script: Time how much a device is ON

Post 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 11978 times
Timer device:
yianniss5tmr.JPG
yianniss5tmr.JPG (12.04 KiB) Viewed 11978 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: 110
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 »

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: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

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

Post 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)".
Wob76
Posts: 110
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 »

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: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

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

Post 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?
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

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

Post 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...:)
Wob76
Posts: 110
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 »

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: 110
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 »

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 11812 times
I am running Domoticz Version: 3.8280
Wob76
Posts: 110
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 »

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: 248
Joined: Sunday 12 February 2017 10:55
Target OS: Linux
Domoticz version: lat Beta
Contact:

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

Post 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.
Wob76
Posts: 110
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 »

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

JanAtHome
Posts: 38
Joined: Wednesday 10 January 2018 16:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 38153
Location: Raalte Holland
Contact:

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

Post by JanAtHome »

Thanks
Verry useful, works fine
heistje
Posts: 23
Joined: Saturday 21 January 2017 13:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Belgium
Contact:

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

Post 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 9853 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
Bullseye on RPI 4
Aeotec Z-Wave Plus Z-Stick Gen5
Zwave-JS-UI
RFXtrx433E
Eth8020
P1 USB
Shellys
ESP8266
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

LUA Script: Time how much a device is ON

Post 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
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

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

Post 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
if it was easy everybody would do it
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

LUA Script: Time how much a device is ON

Post 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.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
EricT
Posts: 30
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

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

Post by EricT »

Hi mvzut,

Exellent, thank you very much! :D

Eric
if it was easy everybody would do it
Ray-Man
Posts: 29
Joined: Friday 12 December 2014 19:03
Target OS: Linux
Domoticz version: 2021.1
Location: Zwolle
Contact:

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

Post 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.......
gajotnt
Posts: 70
Joined: Monday 06 February 2017 12:48
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: Azores
Contact:

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

Post 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 8682 times
notifications.JPG
notifications.JPG (72.06 KiB) Viewed 8682 times
hemant5400z
Posts: 80
Joined: Monday 05 November 2018 17:41
Target OS: Linux
Domoticz version:
Contact:

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

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest