Page 1 of 1

Count not correct

Posted: Friday 15 May 2020 15:31
by MeAlbert
I have a Heat Pump that has to defrost when the condenser is freezing in. This is recorded in Domoticz with a dummy switch. (off or on)
I want to count the number of defrost in a day in a counter. I found an example of a swith count in the manual like ths:

Code: Select all

return {
        on = {
         devices = { 'MySwitch' }
        },
        data = {
            counter = { initial = 0 }
        },
        execute = function(domoticz, switch)
          if (domoticz.data.counter == 5) then
             domoticz.notify('The switch was pressed 5 times!')
             domoticz.data.counter = 0 -- reset the counter
          else
             domoticz.data.counter = domoticz.data.counter + 1
          end
        end
    }
I changed the code to:

Code: Select all

return {
        on = {
         devices = { 55 }
        },
        execute = function(domoticz,device )
    	    local numberDefrosts = domoticz.devices(78)
            numberDefrosts.updateCounter(numberDefrosts.counter + 1)
    end
}

In my opinion simple and should work. It works BUT the numbers are not correct. I had 2 defrosts this night and the counter now is at 9. This morning it was 4. The switch logging looks like this:

Code: Select all

2020-05-15 07:03:07	Off	MQTT
2020-05-15 06:58:08	Off	MQTT
2020-05-15 06:53:07	Off	MQTT
2020-05-15 06:48:08	Off	MQTT
2020-05-15 06:47:21	Off	MQTT
2020-05-15 06:43:07	On	MQTT
2020-05-15 06:41:05	On	MQTT
2020-05-15 06:38:08	Off	MQTT
2020-05-15 06:33:08	Off	MQTT
2020-05-15 06:28:07	Off	MQTT
2020-05-15 06:23:07	Off	MQTT
2020-05-15 06:18:07	Off	MQTT
2020-05-15 06:13:08	Off	MQTT
2020-05-15 06:08:08	Off	MQTT
2020-05-15 06:03:09	Off	MQTT
Rest of the day all Off.
I us version 2020.1 on a Pi

Re: Count not corrcet

Posted: Friday 15 May 2020 16:17
by waaren
MeAlbert wrote: Friday 15 May 2020 15:31 I have a Heat Pump that has to defrost when the condenser is freezing in. This is recorded in Domoticz with a dummy switch. (off or on)
...
In my opinion simple and should work. It works BUT the numbers are not correct.

Code: Select all

on = {
         devices = { 55 }
        },
This section triggers the script every time device 55 is updated. You should provide extra code in the execute section of the script if you want the counter only to be updated when the device gets an 'On' signal.

Re: Count not corrcet

Posted: Friday 15 May 2020 18:27
by MeAlbert
Ok. If I take an if statement that triggers when it is on how do I prevent the double count when the switch is on for 10 minutes?
It should only count the switch goes form Off to On. Do I use a persistent value like this to make this possible or is there a simpler method?

Code: Select all

return {
	on = {
		devices = {	55 }
		},
	data = { eerder = {initial = 'On' }
	    },
	execute = function(domoticz, device)
	    local numberDefrosts = domoticz.devices(78)
	    if (domoticz.data.eerder == 'Off' and device.state == 'On') then
	    numberDefrosts.updateCounter(numberDefrosts.counter + 1)
	    domoticz.data.eerder = device.state
    else
        domoticz.data.eerder = device.state
        numberDefrosts.updateCounter(0)
    end
end

Re: Count not corrcet

Posted: Friday 15 May 2020 23:50
by waaren
MeAlbert wrote: Friday 15 May 2020 18:27 Ok. If I take an if statement that triggers when it is on how do I prevent the double count when the switch is on for 10 minutes?
It should only count the switch goes form Off to On. Do I use a persistent value like this to make this possible or is there a simpler method?
I would not know of a working method without having access to the previous state. Persistent data is probably the easiest way of doing that.

Code: Select all

return
{
    on =
    {
        devices =
        {
            55,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },


    data =
    {
        eerder =
        {
            initial = 'initial',
        },
    },

    execute = function(domoticz, device)
        local numberDefrosts = domoticz.devices(78)
        if domoticz.data.eerder ~= device.state then
            domoticz.data.eerder = device.state
            if device.state == 'On' then
                numberDefrosts.updateCounter(numberDefrosts.counter + 1)
            else
                numberDefrosts.updateCounter(0) -- Not sure I understand why you reset the counter ?                
            end
        end
    end
}

Re: Count not correct  [Solved]

Posted: Saturday 16 May 2020 9:26
by MeAlbert
Waaren thanks.

Code: Select all

else
    numberDefrosts.updateCounter(0) -- Not sure I understand why you reset the counter ?                
end
I used this line to prevent red devices because they are not updated every hour. Did not realize that I reset the counter to 0 again.
Will change to numberDefrosts.updateCounter(numberDefrosts.counter) and move the else statement one if down. Then it should be all right I think.

Thanks for the help.

Code: Select all

        if domoticz.data.eerder ~= device.state then
            domoticz.data.eerder = device.state
            if device.state == 'On' then
                numberDefrosts.updateCounter(numberDefrosts.counter + 1)
            end
        else
          numberDefrosts.updateCounter(numberDefrosts.counter)                
        end