Page 1 of 1

time resolution

Posted: Wednesday 13 January 2021 22:33
by BartSr
Hi,
As far as I can see, if you want to do some calculations using time the resolution is 1 second.
Is there a way to get better resolution? e.g. hundreds of second?
KR
-Bart

Re: time resolution

Posted: Thursday 14 January 2021 0:24
by waltervl
For calculations in dzVents you can also use milliseconds, see https://www.domoticz.com/wiki/DzVents:_ ... nd_methods

eg compare(time), millisecondsAgo

I suppose in LUA this also can be done.

Re: time resolution

Posted: Thursday 14 January 2021 0:44
by waaren
BartSr wrote: Wednesday 13 January 2021 22:33 As far as I can see, if you want to do some calculations using time the resolution is 1 second.
Is there a way to get better resolution? e.g. hundreds of second?
I would not know how to do it in classic Lua but in dzVents you can work with milliseconds

Code: Select all


return
{
    on =
    {
        timer =
        {
            'every minute'
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'currentTime',
    },

    execute = function(dz)
        dz.log(_G.globalvariables.currentTime,dz.LOG_DEBUG)
        dz.log(dz.time.milliseconds,dz.LOG_DEBUG)
    end
}



Code: Select all

2021-01-14 00:41:00.322  Status: dzVents: Info: currentTime: ------ Start internal script: Script #1:, trigger: "every minute"
2021-01-14 00:41:00.324  Status: dzVents: Debug: currentTime: 2021-01-14 00:41:00.278
2021-01-14 00:41:00.324  Status: dzVents: Debug: currentTime: 278
2021-01-14 00:41:00.326  Status: dzVents: Info: currentTime: ------ Finished Script #1

Re: time resolution

Posted: Friday 15 January 2021 20:03
by BartSr
Hi,
This is the code which I expect that has now and then failures due to timing
------------------------------------------------------------------------------------------------------------------------

Code: Select all

-- ~/domoticz/scripts/lua/script_device_pirs.lua
-- Each of the motion sensors in Domoticz follow this name convention:
-- PIRxyzSwitchName or PIRxyzGroupName
-- x speicifies when the PIR controls - a=all day, n=nighttime, d=daytime, s=Dummy switch set in Domoticz which
--    has to be on for the PIR to operate - Dummy-SwitchName or Dummy-GroupName
--    custom-timers can be set by creating a timed dummy switch in Domoticz
-- y specifies what the PIR is controlling - r = room (single Domoticz) switch and g = group
-- z specifies how long the ligth will stay on for in minutes, so z =5 turns the switch or the group on for 5 minutes
-- N.B. be carefully as currently there is little error checking so wrongly named PIRs in Domoticz may cause an error
-- N.B. one wrongly named PIR may stop the script, check log for any issues

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

--
commandArray = {}

tc = next(devicechanged)



if (tc == 'Bew1-trap') or (tc == 'Bew2-trap') then
    
     if timeofday['Daytime'] then
         
      -- overdag het traplicht niet aan doen
      
        return
      end
  
    -- tijdstip waarop lamp is aangezet
    ccc = uservariables['TraplichtAanTijdstip']

    -- aantal verstreken seconden sinds lamp is aangezet
    x=timedifference(ccc)

    -- als er weer een schakelcommando komt binnen 60 seconden na het eerste commando: doe verder niets
    if x < 60 then
        return
    end    
  
    f=tostring(os.date('%Y-%m-%d %H:%M:%S'))
 

    commandArray  ['Variable:TraplichtAanTijdstip']= (f)

    -- licht werkt met bistabiel relais ; bij 1e commando gaat licht aan; bij 2e uit
    commandArray['Lamp-Trap'] = 'On REPEAT 2 INTERVAL 60' -- command will be repeated 2 times with 60 seconds interval
end

return commandArray
--------------------------------------------------------

I'm sorry as I don't know how to put the code in a "code window"

Remarks are in dutch language.
Brief explanation:
I have a lighting for the stairs which is controlled by a so called bi-stable relay. That means that you give a short pulse to the relay coil to change the status of the relay contacts.
And another pulse for changing the status again.

I have two movement-sensor (433Mhz) (Bew1 and Bew2) once there is one of them activated the light (Lamp-trap) goes on.
But sometimes it seems that (due to timing ?) the function of the relay has been inverted so once the sensor is activated the light goes off and after the requested "on "time the relay goes on iso off.

Any idea this is timing problem or something others?

TIA
-Bart

Re: time resolution

Posted: Friday 15 January 2021 21:47
by waaren
BartSr wrote: Friday 15 January 2021 20:03 I'm sorry as I don't know how to put the code in a "code window"
click on the marked button and paste your code between the inserted tags.
code.png
code.png (39.39 KiB) Viewed 462 times