You can do it in LUA as well.
First create a function that calculates the time difference:
Code: Select all
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
Then you could calculate how long something is on by using this code:
Code: Select all
if (devicechanged['TVof yourkids'] == 'On')
then
difference_in_days = math.floor(difference / 86400)
difference_in_hours = math.floor((difference - difference_in_days * 86400) / 3600)
difference_in_minutes = math.floor(((difference - difference_in_days * 86400) - (difference_in_hours * 3600)) / 60)
print('<b style="color:Red">>> Devices ' .. difference_in_days .. ' days, ' .. difference_in_hours .. ' hours and ' .. difference_in_minutes .. ' minutes ago</b>')
Then you could update a virtual text sensor or something with the values in days / hours / minutes.