Page 1 of 1

Getting data variables (timer)

Posted: Thursday 09 April 2020 9:33
by tomes
Hi,
For several days I have been trying to solve the problem of analyzing the data (history) variable.
I have several devices that report their activity to the STATUS global variable.
The following reports look like this:

DEVICE1
STOP
DEVICE 2
STOP
DEVICE 3
STOP
DEVICE 2
STOP

The content of the variable changes from time to time. Never work two devices at the same time.
Now I want to analyze and make a decision based on the operating time of the devices:

1. If device works for more than 2 minutes do: domoicz.devices(123).switchOff().forSec(10)
2. If the same device has stopped working for less or equal than 5 seconds, skip the break and include it in the timer above and perform the above action.
3. If the next device ~ = previous device start timer 2 minutes again.
4. If the interval between the operation of the same device is longer than 5 seconds, start the timer 2 minutes again.

I made the script triggered by changing the STATUS variable.
I added a device that was triggered by a 2-minute timer. Unfortunately - it doesn't work properly. Maybe your experience will tell you how to solve this problem.
Tom

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 10:43
by waaren
tomes wrote: Thursday 09 April 2020 9:33 I made the script triggered by changing the STATUS variable.
I added a device that was triggered by a 2-minute timer. Unfortunately - it doesn't work properly. Maybe your experience will tell you how to solve this problem.
Happy to help but I don't fully understand what you try to achieve.
Maybe if you describe in more detail what the real life situation is, share the script you have sofar and point to he first point in the script that you face your issue with. Also domoticz log with log lines generated by your script could prove useful.

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 11:01
by tomes
Thank you for your answer. I'm sending my script. It doesn't report any errors, but it doesn't work either. Probably fatigue and little experience do not allow me to see obvious mistakes or look at the problem completely differently.

Code: Select all

	on = {
		variables = { 'ONAIR' },
		devices = { 294 }   -- device switch TIME_OUT to trigger 
	},
	data = { status = { history = true, maxItems = 3 } },
	execute = function(domoticz, device)
		local onair = domoticz.variables('ONAIR').value
		local max_time = 120  -- maxwork time
		local max_break = 10  -- time to switch on device - to stop all devices reporting to variable 'ONAIR'
		if (device.isVariable) then
            domoticz.data.status.add(onair)
		    if device.value ~= 'STOP' then
		        if domoticz.data.status.get(3).data ~= onair then
		            domoticz.devices(294).cancelQueuedCommands()
		            domoticz.devices(294).switchOn().checkFirst().silent()
		            domoticz.devices(294).switchOff().afterSec(max_time)
		        elseif domoticz.data.status.get(3).time.secondsAgo > 5 then  -- ignore pause > 5 sec
		            domoticz.devices(294).cancelQueuedCommands()
		            domoticz.devices(294).switchOn().checkFirst().silent()
		            domoticz.devices(294).switchOff().afterSec(max_time)
		        end
		    end
	    end
	    if (device.isDevice) and device.state == 'Off' and onair ~= 'STOP' then
	        domoticz.devices('TURN OFF').switchOn().forSec(max_break).silent()
	    end
	end
}

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 11:52
by waaren
tomes wrote: Thursday 09 April 2020 11:01 Thank you for your answer. I'm sending my script. It doesn't report any errors, but it doesn't work either. Probably fatigue and little experience do not allow me to see obvious mistakes or look at the problem completely differently.
Added some loglines and made some minor changes. Hoepfully it will help you locating the issue.

Code: Select all

return
{   
    on =
    {
        variables =
        {
            'ONAIR',
        },
        devices =
        {
           294,
        },   -- device switch TIME_OUT to trigger
    },
   
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'onair',
    },
   
    data =
    {
        status =
        {
            history = true, maxItems = 3,
        },
    },
   
    execute = function(dz, item)
        local onair = dz.variables('ONAIR').value
        local timeOut = dz.devices(294)
        local turnOff = dz.devices('TURN OFF')

        local max_time = 120  -- maxwork time
        local max_break = 10  -- time to switch on device - to stop all devices reporting to variable 'ONAIR'

        if dz.data.status.size > 0 then
            dz.log(dz.data.status.getOldest().data,dz.LOG_DEBUG)
        else
            dz.log('No historic data available yet.',dz.LOG_DEBUG)
        end
       
        if item.isVariable then
            dz.data.status.add(onair)
           
            if onair.value ~= 'STOP' then
                if dz.data.status.getOldest().data ~= onair then
                    timeOut.cancelQueuedCommands()
                    timeOut.switchOn().checkFirst().silent()
                    timeOut.switchOff().afterSec(max_time)
                elseif dz.data.status.getOldest().time.secondsAgo > 5 then  -- ignore pause > 5 sec
                    timeOut.cancelQueuedCommands()
                    timeOut.switchOn().checkFirst().silent()
                    timeOut.switchOff().afterSec(max_time)
                end
            end
        end
       
        if item.isDevice and item.state == 'Off' and onair ~= 'STOP' then
            turnOff.cancelQueuedCommands()
            turnOff.switchOn().checkFirst().silent()
            turnOff.switchOff().silent().afterSec(max_break)
        end       
    end
}

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 12:11
by tomes
Thanks for the corrections. In fact, it looks better now.
However, I think it will still not work. The problem is probably not reading the data.status variable but the timeOut switch support. Maybe it can be solved somehow more easily than by creating additional devices?
Tom

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 12:38
by waaren
tomes wrote: Thursday 09 April 2020 12:11 Maybe it can be solved somehow more easily than by creating additional devices?
Could be but I stil have no idea what your requirement is. So again my question
Maybe if you describe in more detail what the real life situation is,

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 15:22
by tomes
I manage an amateur radio network. Remote relay stations connect to one server. Domoticz also works on this server. Each of the relays, receiving a signal transmitted by users' radios, sends its unique identifier to the domoticz. E.g:
if user U1 transmits via RPT1 repeater, domoticz receives the variable "ONAIR" with the value RPT1. When the user finishes sending, the variable changes to STOP.
If the user U2 transmits from the RPT2 repeater, domoticz receives the variable "ONAIR" with the value RPT2.

People can talk continuously for 15 minutes blocking the network. I would like to set a limit of 2 minutes. After this time the turnOff.switchOn () switch is activated which blocks the user to (max_break).

I also want to add functionality: a transmission pause shorter than 5 seconds will not extend the broadcasting time of one user by another 2 minutes. I can't identify a specific user - it's analog transmission. However, I can assume that if after a break of less than 5 seconds the value of the 'ONAIR' variable is the same as before the break, it is still transmitted by the same user. So he still has time: 2 minutes minus talk time + break time.
I know this is very complicated, but maybe it will let you give me a simpler solution than the one I'm working on.

Tom

Re: Getting data variables (timer)  [Solved]

Posted: Thursday 09 April 2020 17:01
by waaren
tomes wrote: Thursday 09 April 2020 12:11 Thanks for the corrections. In fact, it looks better now.
However, I think it will still not work. The problem is probably not reading the data.status variable but the timeOut switch support. Maybe it can be solved somehow more easily than by creating additional devices?
Tom
Thx for the explanation. I think I understand now what you try to do.
I corrected one line below. Can you share what you see in the logfile and point to the log-lines where you see or miss something unexpected ?

Code: Select all

return
{   
    on =
    {
        variables =
        {
            'ONAIR',
        },
        devices =
        {
           294,
        },   -- device switch TIME_OUT to trigger
    },
   
    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'onair',
    },
   
    data =
    {
        status =
        {
            history = true, maxItems = 3,
        },
    },
   
    execute = function(dz, item)
        local onair = dz.variables('ONAIR').value
        local timeOut = dz.devices(294)
        local turnOff = dz.devices('TURN OFF')

        local max_time = 120  -- maxwork time
        local max_break = 10  -- time to switch on device - to stop all devices reporting to variable 'ONAIR'

        if dz.data.status.size > 0 then
            dz.log(dz.data.status.getOldest().data,dz.LOG_DEBUG)
        else
            dz.log('No historic data available yet.',dz.LOG_DEBUG)
        end
       
        if item.isVariable then
            dz.data.status.add(onair)
           
            if onair ~= 'STOP' then
                if dz.data.status.getOldest().data ~= onair then
                    timeOut.cancelQueuedCommands()
                    timeOut.switchOn().checkFirst().silent()
                    timeOut.switchOff().afterSec(max_time)
                elseif dz.data.status.getOldest().time.secondsAgo > 5 then  -- ignore pause > 5 sec
                    timeOut.cancelQueuedCommands()
                    timeOut.switchOn().checkFirst().silent()
                    timeOut.switchOff().afterSec(max_time)
                end
            end
        end
       
        if item.isDevice and item.state == 'Off' and onair ~= 'STOP' then
            turnOff.cancelQueuedCommands()
            turnOff.switchOn().checkFirst().silent()
            turnOff.switchOff().silent().afterSec(max_break)
        end       
    end
}

Re: Getting data variables (timer)

Posted: Thursday 09 April 2020 21:05
by tomes
Ok, now it looks like it works. I still have to watch for any errors.
Again thank you very much Waaren for help.
Tom