Need time (=> 1 minute) between two results  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
vandermark1977
Posts: 11
Joined: Sunday 11 December 2016 20:10
Target OS: -
Domoticz version:
Contact:

Need time (=> 1 minute) between two results

Post by vandermark1977 »

I have a script which controls my heatpump. The idea is that based on different temperatures and compressor frequencies it lowers the setpoint of the target temperature so the heatpump won't start too fast. The problem is that some steps follow each other too soon. It is triggered by the frequency of the compressor. I would like to add a delay in the form of: Only change when last change was => 1 minute ago. Can I add some sort of code to accomplish this? This is the code:

Code: Select all

return {
    on = {
        devices = { 
            49, --Pana Compressor_Freq
        }
    },
    data = {
        ---------------------------------------
        -- compressor state
        -- 1: compressor off
        -- 2: compressor startup
        -- 3: compressor relaxing
        -- 4: compressor continuous operation
        state = { initial = 1 }
    },
    logging = {
        level = domoticz.LOG_DEBUG, -- change to LOG_ERROR when OK - was LOG_DEBUG
        marker = scriptVar,
        },
    execute = function(domoticz, triggeredItem)
        local heatshift = domoticz.devices(82)
        local target_temp = domoticz.devices(66)
        local outlet_temp = domoticz.devices(65)
        local CompressorFreq = domoticz.devices(49)

        if(CompressorFreq.sValue == "0") then
            domoticz.log('State: compressor off', domoticz.LOG_INFO)
            domoticz.data.state = 1
            correction = 0
        elseif(domoticz.data.state == 1 or domoticz.data.state == 2) then
            domoticz.log('State: compressor startup', domoticz.LOG_INFO)
            domoticz.data.state = 2
            correction = outlet_temp.temperature - target_temp.temperature + heatshift.setPoint -1
            if(tonumber(CompressorFreq.sValue) > 22) then
                domoticz.data.state = 3
            end
        elseif(domoticz.data.state == 3) then
            domoticz.log('State: compressor relaxing', domoticz.LOG_INFO)
            correction = outlet_temp.temperature - target_temp.temperature + heatshift.setPoint -1
            if((outlet_temp.temperature - target_temp.temperature) >= 1) and (tonumber(CompressorFreq.sValue) < 23) then
                domoticz.data.state = 4
            end
        elseif(domoticz.data.state == 4) then
            domoticz.log('State: continuous operation', domoticz.LOG_INFO)
            if((outlet_temp.temperature - target_temp.temperature) >= 0) then
                domoticz.log('Continu met voorwaarde Shift+1 voldaan', domoticz.LOG_INFO)
                correction = heatshift.setPoint + 1
            else
                correction = heatshift.setPoint
                domoticz.log('Continu zonder aanpassing Shift', domoticz.LOG_INFO)
            end
        else
            domoticz.log('State: undefined', domoticz.LOG_INFO)
            domoticz.data.state = 1
            correction = 0
        end
        
        if correction < -5 then correction = -5 end
        if correction > 0 then correction = 0 end
        
        if heatshift.setPoint == correction then
            domoticz.log('No correction', domoticz.LOG_INFO)
        else
            domoticz.log('Correction set to ' .. tostring(correction), domoticz.LOG_INFO)
            heatshift.updateSetPoint(correction)
        end
    end
}
twoenter
Posts: 76
Joined: Sunday 17 February 2019 15:01
Target OS: NAS (Synology & others)
Domoticz version: 4.10
Contact:

Re: Need time (=> 1 minute) between two results

Post by twoenter »

Yes you can. Check the lastupdate of your device. See wiki of dzvents and search for lastupdate
Check my Domoticz tutorials, ESP8266 and Synology blog at https://www.twoenter.nl/blog
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need time (=> 1 minute) between two results

Post by waaren »

vandermark1977 wrote: Tuesday 26 January 2021 20:51 I have a script which controls my heatpump. The idea is that based on different temperatures and compressor frequencies it lowers the setpoint of the target temperature so the heatpump won't start too fast. The problem is that some steps follow each other too soon. It is triggered by the frequency of the compressor. I would like to add a delay in the form of: Only change when last change was => 1 minute ago. Can I add some sort of code to accomplish this?
What I use in similar situation is something like

in the persistent data section add a lastChange var

Code: Select all

    data = 
	{
        ---------------------------------------
        -- compressor state
        -- 1: compressor off
        -- 2: compressor startup
        -- 3: compressor relaxing
        -- 4: compressor continuous operation
        state = { initial = 1 },
	lastChange = { initial = 0 },
    },
And make setState a function that will check the last time the lastChange was updated ( os.time() returns the number of seconds sine 1/1/1970 )

Code: Select all

local minimalStatechangeTime = 60

		local function setState(state)
			if domoticz.data.lastChange < ( os.time() - minimalStatechangeTime ) then
				domoticz.data.state = state
				domoticz.data.lastChange = os.time()
				return true
			end
			return false
		end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
vandermark1977
Posts: 11
Joined: Sunday 11 December 2016 20:10
Target OS: -
Domoticz version:
Contact:

Re: Need time (=> 1 minute) between two results

Post by vandermark1977 »

Thanks @waaren, that looks like a simple and elegant solution. I will try to integrate it in the script and let you know on the results
vandermark1977
Posts: 11
Joined: Sunday 11 December 2016 20:10
Target OS: -
Domoticz version:
Contact:

Re: Need time (=> 1 minute) between two results

Post by vandermark1977 »

There is one issue: The 'state' is not the changing variable. I have another idea. Can i set a local variable which is just a simpel on/off switch. At the end when the result happens where minimum time of 1 minuite is needed I add the code:

Code: Select all

SimpleOnOffSwitch.toggleSwitch()
And as a extra condition for earlier rules I add:

Code: Select all

SimpleOnOffSwitch.lastUpdate.minutesAgo => 1
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Need time (=> 1 minute) between two results

Post by waaren »

vandermark1977 wrote: Wednesday 27 January 2021 9:52 Can i set a local variable which is just a simple on/off switch.
That's of course also possible. I usually prefer local persistent data above using devices or domoticz uservariables because devices and vars can be influenced by other scripts or GUI actions.
If you use a var or a device then consider using silent()
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
vandermark1977
Posts: 11
Joined: Sunday 11 December 2016 20:10
Target OS: -
Domoticz version:
Contact:

Re: Need time (=> 1 minute) between two results  [Solved]

Post by vandermark1977 »

I have added a dummy device with simple On/Off switch. This switch is not used anywhere.

New toggle:

Code: Select all

 local Toggle = domoticz.devices(148)
Extra result when script make change to temperature setpoint:

Code: Select all

Toggle.toggleSwitch()
And condition which i added in three lines:

Code: Select all

and (Toggle.lastUpdate.minutesAgo >= 1)
This is the new script now:

Code: Select all

return {
    on = {
        devices = { 
            49, --Pana Compressor_Freq
        }
    },
    data = {
        ---------------------------------------
        -- compressor state
        -- 1: compressor off
        -- 2: compressor startup
        -- 3: compressor relaxing
        -- 4: compressor continuous operation
        state = { initial = 1 }
    },
    logging = {
        level = domoticz.LOG_DEBUG, -- change to LOG_ERROR when OK - was LOG_DEBUG
        marker = scriptVar,
        },
    execute = function(domoticz, triggeredItem)
        local heatshift = domoticz.devices(82)
        local target_temp = domoticz.devices(66)
        local outlet_temp = domoticz.devices(65)
        local CompressorFreq = domoticz.devices(49)
        local Toggle = domoticz.devices(148)

        if(CompressorFreq.sValue == "0") then
            domoticz.log('State: compressor off', domoticz.LOG_INFO)
            domoticz.data.state = 1
            correction = 0
        elseif(domoticz.data.state == 1 or domoticz.data.state == 2) then
            domoticz.log('State: compressor startup', domoticz.LOG_INFO)
            domoticz.data.state = 2
            correction = outlet_temp.temperature - target_temp.temperature + heatshift.setPoint -1
            if(tonumber(CompressorFreq.sValue) >25) and (Toggle.lastUpdate.minutesAgo >= 1) then
                domoticz.data.state = 3
            end
        elseif(domoticz.data.state == 3) then
            domoticz.log('State: compressor relaxing', domoticz.LOG_INFO)
            correction = outlet_temp.temperature - target_temp.temperature + heatshift.setPoint -1
            if((outlet_temp.temperature - target_temp.temperature) >= 1) and (tonumber(CompressorFreq.sValue) < 26) and (Toggle.lastUpdate.minutesAgo >= 1) then
                domoticz.data.state = 4
            end
        elseif(domoticz.data.state == 4) then
            domoticz.log('State: continuous operation', domoticz.LOG_INFO)
            if((outlet_temp.temperature - target_temp.temperature) >= 0) and (Toggle.lastUpdate.minutesAgo >= 1) then
                domoticz.log('Continu met voorwaarde Shift+1 voldaan', domoticz.LOG_INFO)
                correction = heatshift.setPoint + 1
            else
                correction = heatshift.setPoint
                domoticz.log('Continu zonder aanpassing Shift', domoticz.LOG_INFO)
            end
        else
            domoticz.log('State: undefined', domoticz.LOG_INFO)
            domoticz.data.state = 1
            correction = 0
        end
        
        if correction < -5 then correction = -5 end
        if correction > 0 then correction = 0 end
        
        if heatshift.setPoint == correction then
            domoticz.log('No correction', domoticz.LOG_INFO)
        else
            domoticz.log('Correction set to ' .. tostring(correction), domoticz.LOG_INFO)
            heatshift.updateSetPoint(correction)
            Toggle.toggleSwitch()
        end
    end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest