How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

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

Moderator: leecollings

Post Reply
pippya
Posts: 4
Joined: Thursday 13 July 2023 11:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by pippya »

Hi I would like to know the utilisation of our Squash Courts at peak times - between 5pm and 7pm.

I already calculate and capture the percent usage of the courts over a whole day, but we now need to know the utilisation at peak times.

Background and this is working fine.
1. Tasmota device measure Lux level and updates a virtual lux level sensor every 5mins
2. Dzvents looks at lux level and determines whether light is on or off and updates a virtual light switch (pic examples attached)
3. A couple more Dzvents then update an incremental counter and work out the percent usage for the day

all working ok , but how do I work out utilisation for a specific time period ?
Can you advise on best approach? if a script dzvents is preferred

Do I modify my existing scripts ? if so i can post the incremental counter example
Do I calculate time ON from the already captured data in the light sensor at the end of the day ?
I did try an additional incremental counter but the rollover to next day meant first time it ran, it calculated elapsed time since it finished end of previous day.

Regards Phillip
Attachments
court3-2.jpg
court3-2.jpg (18.99 KiB) Viewed 799 times
court3-1.png
court3-1.png (37.16 KiB) Viewed 799 times
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by willemd »

Create a device which is set to zero at the start of the peak time.
Update the device in the same way as your already existing incremental counter.
Calculate the percentage at the end of the peak time.
pippya
Posts: 4
Joined: Thursday 13 July 2023 11:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by pippya »

Thanks for replying

current incremental counter is below

assume I will need a trigger similar to this ?
timer = {'at 17:00-19:00 every 5 minutes'},

can you provide an example of how I zero everyday at start of peak time and how I then trigger a calculation at the end of the peak time ?

I tried to post the dzvent script this was not allowed, added a pic instead, is this due to me being a newcomer ?
Attachments
code.jpg
code.jpg (104.12 KiB) Viewed 751 times
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by willemd »

Here are examples of the timer rules you can use:
https://www.domoticz.com/wiki/DzVents:_ ... gger_rules

In you script you can test which of the timers (=item in your script) fired the script and then take action accordingly.
So if the "at 17:00" timer triggered the script, then you do the reset and if the "at 19:00" triggered the script, then you make the calcuation.
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by jvdz »

pippya wrote: Monday 17 July 2023 11:03 I tried to post the dzvent script this was not allowed, added a pic instead, is this due to me being a newcomer ?
Did you try posting it in a codebox? (</>)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
pippya
Posts: 4
Joined: Thursday 13 July 2023 11:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by pippya »

Hi yes I did use the code box button and tried again

I received this message

"You can’t post image, email or url links that are external to this domain. Please remove domoticz., dz., item., dz., data., dz., data., os., dz., data., os., item., lastUpdate., os., dz., data., dz., data. and iCounter."
pippya
Posts: 4
Joined: Thursday 13 July 2023 11:50
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by pippya »

will try and work with subsets of the code, apologies not being able to use code box

at the moment I have this as a trigger
########
on =
{
devices =
{
43, -- court 3 light idx43 device for which you want to record the On time.
},
},
########
will change to this
#######
on =
{
timer = {'at 17:05-18:55 every 5 minutes',
'at 17:00',
'at 19:00',
},
},
#######

identify which timer triggered the event with this: ?

if timer.trigger == 'at 17:00' then
-- setup for initial use

elseif timer.trigger == 'at 19:00' then
-- finalise and calculate

is somebody able to explain the 3 code steps here please ?
if item.active then
if dz.data.lastOn == 0 then dz.data.lastOn = os.time() end
return
else
if dz.data.lastOn == 0 then
secondsActive = os.time() - item.lastUpdate.dDate
else
secondsActive = os.time() - dz.data.lastOn
dz.data.lastOn = 0
end

regards phillip
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by willemd »

You will have to write the code yourself , here is the logic.

I think you could also use domoticz.devices(yourIDXnr).lastUpdate.secondsAgo

So:
1) reset a counter device to zero at 17:00.
If the light is already on at 17:00, make sure to record the number of seconds since it was switched on (in a persistent variable for example) so you can subtract that later.
2) Every time the light changes from on to off, determine secondsago and add to counter device.
If the light was already on at 17:00 make sure to subtract the number of seconds of the persistent variable, otherwise the seconds before 17:00 are also counted. But only do this during the first update (if you set the persistent variable to 0 after first use, then this is solved automatically)
3) at 19:00, if the light is still on, add the seconds to the counter device otherwise don't update the counter
and then divide the total of the counter device (=seconds on during the two hours) by 7200, multiply by 100, and then you have the percentage.
User avatar
jvdz
Posts: 2330
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How do I Calculate Seconds ON for a device between 5pm and 7pm daily?

Post by jvdz »

pippya wrote: Tuesday 18 July 2023 9:39 Hi yes I did use the code box button and tried again

I received this message

"You can’t post image, email or url links that are external to this domain. Please remove domoticz., dz., item., dz., data., dz., data., os., dz., data., os., item., lastUpdate., os., dz., data., dz., data. and iCounter."
It is correct that in the first couple of posts you can't have URL's pointing to other sites than the forum website as an anti spamming measure. Just change the url in the code so it isn't recognized as outside url.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest