Hello all,
I've been trying to find this for a quite of time now, can't seem to find it.
I am looking for a method in which I can calculate how many times a day a door was opened (I have door sensors which send on/off states to my domitcz setup, works like a charm). Furthermore I would like to calculate the time over a day to see how long that particular door was open.
Did any off you did this already and if yes, care to share?
Thanks for the great support and help is much appreciated.
Best regards,
How many times and how long was a door open
Moderator: leecollings
-
- Posts: 6
- Joined: Sunday 17 May 2015 16:35
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: How many times and how long was a door open
Almost the same of what I do for my gas heater:
This shows a table with all on/off times, the time between them and a total time my gas heater was on during the last 24 hours.
Code: Select all
echo '
<div>
<table class="brander">';
$datas=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=lightlog&idx=80',true,$ctx),true);
$status='';$tijdprev=$time;$totalon=0;
if(!empty($datas['result'])){
foreach($datas['result'] as $data){
//if($status!=$data['Status']){
$status=$data['Status'];
$level=$data['Level'];
$tijd=strtotime($data['Date']);
if($tijd<$eendag)break;
$period=($tijdprev-$tijd);
if($status=='On'){$totalon=$totalon+$period;$style="color:#FF4400";}else{$style="color:#1199FF";}
$tijdprev=$tijd;
echo '
<tr>
<td style="'.$style.'">'.$data['Date'].'</td>
<td style="'.$style.'"> '.$status.' </td>
<td style="'.$style.'"> '.convertToHours($period).'</td>
</tr>';
//}
}
}
echo '
</table>
</div>
<div class="fix" style="top:18px;left:320px;width:60px;">'.convertToHours($totalon).'</div>';
-
- Posts: 6
- Joined: Sunday 17 May 2015 16:35
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: How many times and how long was a door open
Thanks for your quick response. However I am not using php and was looking something in the direction of LUA or a python or a sh script. I am not a star in converting this in one of those languages, but with an example in that way I can fix it..Egregius wrote:Almost the same of what I do for my gas heater:This shows a table with all on/off times, the time between them and a total time my gas heater was on during the last 24 hours.Code: Select all
echo ' <div> <table class="brander">'; $datas=json_decode(file_get_contents('http://127.0.0.1:8080/json.htm?type=lightlog&idx=80',true,$ctx),true); $status='';$tijdprev=$time;$totalon=0; if(!empty($datas['result'])){ foreach($datas['result'] as $data){ //if($status!=$data['Status']){ $status=$data['Status']; $level=$data['Level']; $tijd=strtotime($data['Date']); if($tijd<$eendag)break; $period=($tijdprev-$tijd); if($status=='On'){$totalon=$totalon+$period;$style="color:#FF4400";}else{$style="color:#1199FF";} $tijdprev=$tijd; echo ' <tr> <td style="'.$style.'">'.$data['Date'].'</td> <td style="'.$style.'"> '.$status.' </td> <td style="'.$style.'"> '.convertToHours($period).'</td> </tr>'; //} } } echo ' </table> </div> <div class="fix" style="top:18px;left:320px;width:60px;">'.convertToHours($totalon).'</div>';
Regards.
- Egregius
- Posts: 2592
- Joined: Thursday 09 April 2015 12:19
- Target OS: Linux
- Domoticz version: v2024.7
- Location: Beitem, BE
- Contact:
Re: How many times and how long was a door open
Who wants lua or anything if you have php? 

- emme
- Posts: 909
- Joined: Monday 27 June 2016 11:02
- Target OS: Raspberry Pi / ODroid
- Domoticz version: latest
- Location: Milano, Italy
- Contact:
Re: How many times and how long was a door open
you can use a device script that could make 2 operations:
once the door goes Open, it will store the timestamp (_lastupdate) to a variable
once it goes Closed, it will compare (time difference) the stored timestamp with the current _lastupdate
have a sting variable called LastKnownOpenDoor and try using this script as a script_device_door.lua or a device event:

once the door goes Open, it will store the timestamp (_lastupdate) to a variable
once it goes Closed, it will compare (time difference) the stored timestamp with the current _lastupdate
have a sting variable called LastKnownOpenDoor and try using this script as a script_device_door.lua or a device event:
Code: Select all
local doorDevName = 'Your Door Sensor Name'
local doorStaus = otherdevices[doorDevName]
function timeDiff(dName,dType)
if dType == 'v' then
updTime = uservariables_lastupdate[dName]
elseif dType == 'd' then
updTime = otherdevices_lastupdate[dName]
end
t1 = os.time()
year = string.sub(updTime, 1, 4)
month = string.sub(updTime, 6, 7)
day = string.sub(updTime, 9, 10)
hour = string.sub(updTime, 12, 13)
minutes = string.sub(updTime, 15, 16)
seconds = string.sub(updTime, 18, 19)
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
tDiff = os.difftime(t1,t2)
return tDiff
end
commandArray = {}
if devicechanged[doorDevName] then
if doorStatus = 'On' then -- or 'Open' based on the sensor type
uservariables['LastKnownOpenDoor'] = otherdevices_lastupdate[doorDevName]
else
timeOpen = timeDiff('LastKnownOpenDoor','v')
print('Door '..doorDevName..' was open for '..timeOpen..' seconds')
end if
return commandArray
who doesn't know how to code with phpEgregius wrote:Who wants lua or anything if you have php?









The most dangerous phrase in any language is:
"We always done this way"
"We always done this way"
-
- Posts: 6
- Joined: Sunday 17 May 2015 16:35
- Target OS: Raspberry Pi / ODroid
- Domoticz version:
- Contact:
Re: How many times and how long was a door open
Guys, I know. I am a weak excuse. Good thing we have you guys!
Anyhow thanks mucho!
Anyhow thanks mucho!
Who is online
Users browsing this forum: No registered users and 1 guest