Page 1 of 1

Heating control script not acting as expected

Posted: Sunday 16 August 2020 22:35
by HvdW
Hi,
I have this heating control script.

Code: Select all

-- assumptions:
-- the setpoint is set by a dummy device (not a selector type) 

local CV_SWITCH = 'CV_switch_auto' -- switch device
local TEMP_SETPOINT = 'Thermostaat_setpoint' -- selector dummy device
local TEMP_SENSOR_1 = 'TFA1'
local TEMP_SENSOR_2 = 'Oregon'
local BAT_THRESHOLD = 30
local LOGGING = true

return 
{
    on = 
    {
        timer = 
        {
            'every 15 minutes'
        },
    },

    --LOG levell: This is the log level you want for this script. Can be domoticz.LOG_INFO, domoticz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG or domoticz.LOG_ERROR
    --marker: A string that is prefixed before each log message. That way you can easily create a filter in the Domoticz log to see just these messages.

    logging =   
    {
        level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR,
        --level = LOGGING and domoticz.LOG_ERROR,
        marker = 'dzVents heating',
    },

    execute = function(dz)

        -- collect all input data
        local boiler_switch_state = dz.devices(CV_SWITCH).state           
        local temp_1 = dz.devices(TEMP_SENSOR_1).temperature  
        local temp_2 = dz.devices(TEMP_SENSOR_2).temperature
        local bat_1 = dz.devices(TEMP_SENSOR_1).batteryLevel
        local bat_2 = dz.devices(TEMP_SENSOR_2).batteryLevel
        local setpointValue = dz.devices(TEMP_SETPOINT).setPoint              
        local average_temp = 40  -- very high so the heating will not fire
        local temp_diff = temp_1 - temp_2
        
        -- info only on log level = LOG_DEBUG
        dz.log('CV_SWITCH      : ' .. boiler_switch_state, dz.LOG_DEBUG)
        dz.log('Temp_1         : ' .. temp_1, dz.LOG_DEBUG)
        dz.log('Temp_2         : ' .. temp_2, dz.LOG_DEBUG)
        dz.log('Bat_1          : ' .. bat_1, dz.LOG_DEBUG)
        dz.log('Bat_2          : ' .. bat_2, dz.LOG_DEBUG)
        dz.log('Setpoint_value : ' .. setpointValue, dz.LOG_DEBUG)
        
        -- test if conditions are met and set acting values
        if ((temp_1 - temp_2 > 1.1) or (temp_2 - temp_1 > 1.1)) then
            dz.log('Temperature difference between sensors is more than 1.1 degrees : ' .. temp_diff, dz.LOG_DEBUG)        
            dz.notify('Notifyer', 'Temperature difference between sensors is more than 1.1 degrees' .. temp_diff , dz.PRIORITY_HIGH)
        elseif (bat_1 < BAT_THRESHOLD) then
            dz.log('Battery level of temperature sensor 1 = ' .. bat_1, dz.LOG_DEBUG)        
            dz.notify('Notifyer', 'Temperature sensor 1 ' .. bat_1 .. 'battery level low.',dz.PRIORITY_HIGH)
        elseif (bat_2 < BAT_THRESHOLD) then
            dz.log('Battery level of temperature sensor ' .. bat_2, dz.LOG_DEBUG)        
            dz.notify('Notifyer', 'Temperature sensor 2 ' .. bat_2 .. 'battery level low.',dz.PRIORITY_HIGH)
        else 
            average_temp = (temp_1 + temp_2)/2
            dz.log('Average temperature sensors value is : ' .. average_temp, dz.LOG_DEBUG)
        end

        -- now determine what to do
        if (average_temp >= setpointValue) then
            dz.devices(CV_SWITCH).switchOff()
            dz.log('Target temperature reached, boiler off')
        elseif (average_temp <= (setpointValue -1))  then
            dz.log('Average temperature more than 1 degree below setpointValue, switchOn during 10 minutes')
            dz.devices(CV_SWITCH).switchOn().forMin(10)
        elseif ((average_temp > (setpointValue -1)) and (average_temp < setpointValue)) then
            dz.log('Average temperature less than 1 degree below setpointValue, switchOn during 5 minutes')
            dz.devices(CV_SWITCH).switchOn().forMin(5)
        else  -- not predicted situation
            dz.log('Unpredicted situation, boiler off')
            dz.devices(CV_SWITCH).switchOff()            
        end

    end
}
What I expect the script to do is whenever I switch the heating on manually with the CV_switch_auto and the temperature is above the setpoint, is that it does not switch on
However what it does is wait till 0, 15, 30, 45 minutes (the interval) to test.
So when it is toggled to 'on' it starts burning.
What am I overlooking in the above script.


I made another manual switch which toggles the heating on during 5 minutes (in case it is chilly in the house)

Code: Select all

return {
   on = {
      devices = { 'CV_switch_manual' }
   },
   execute = function(domoticz, TriggeredItem)
     if (TriggeredItem.active) then  -- state == 'On'
            --domoticz.notify('Boiler switch is switched on manually', domoticz.PRIORITY_LOW)
			TriggeredItem.switchOff().afterMin(5) -- if it is a switch
			--domoticz.notify('The Boiler switch ' .. TriggeredItem.name .. ' will be switched off after 5 minutes')
     end

   end
}
This one works as expected.

I know there are many ways of firing the heating with hysteresis and everything.
The script I made initiates heating and then waits 5 or 10 minutes to give the heat in the radiators the chance to heat up the room.
That's why this system has a 15 minutes interval for probing the temperature in the room.

Re: Heating control script not acting as expected

Posted: Sunday 16 August 2020 23:54
by waaren
HvdW wrote: Sunday 16 August 2020 22:35 What I expect the script to do is whenever I switch the heating on manually with the CV_switch_auto and the temperature is above the setpoint, is that it does not switch on
However what it does is wait till 0, 15, 30, 45 minutes (the interval) to test.
Not sure I understand your question but the first script only triggers every 15 minutes. It does not trigger on any decvice update like the second script.

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 1:08
by HvdW
There is a switch CV_switch_auto which triggers every 15 minutes.
There is the other switch CV_switch_manual.

Just in case I accidentally switch CV_switch_auto on, I'd like the script (the first script) to check the temperature and make the decision to switch on or not.
The way I've done it now it switches on no matter what temperature there is in the room.

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 7:44
by waaren
HvdW wrote: Monday 17 August 2020 1:08 The way I've done it now it switches on no matter what temperature there is in the room.
Please share the loglines from script1. That might help in finding why it always switches

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 9:05
by HvdW

Code: Select all

 2020-08-17 09:00:00.543 Status: dzVents: Info: dzVents heating: ------ Start internal script: CV:, trigger: "every 15 minutes"
2020-08-17 09:00:00.567 Status: dzVents: Debug: dzVents heating: Processing device-adapter for CV_switch_auto: Switch device adapter
2020-08-17 09:00:00.568 Status: dzVents: Debug: dzVents heating: Processing device-adapter for TFA1: Temperature+humidity device adapter
2020-08-17 09:00:00.569 Status: dzVents: Debug: dzVents heating: Processing device-adapter for Oregon: Temperature device adapter
2020-08-17 09:00:00.570 Status: dzVents: Debug: dzVents heating: Processing device-adapter for Thermostaat_setpoint: Thermostat setpoint device adapter
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: CV_SWITCH : Off
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Temp_1 : 24.299999237061
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Temp_2 : 23.89999961853
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Bat_1 : 100
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Bat_2 : 100
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Setpoint_value : 19.2
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Average temperature sensors value is : 24.099999427795
2020-08-17 09:00:00.571 Status: dzVents: Debug: dzVents heating: Constructed timed-command: Off
2020-08-17 09:00:00.571 Status: dzVents: Info: dzVents heating: Target temperature reached, boiler off
2020-08-17 09:00:00.571 Status: dzVents: Info: dzVents heating: ------ Finished CV
2020-08-17 09:00:00.571 Status: dzVents: Info: ------ Start internal script: Luftdaten:, trigger: "every 10 minutes"
2020-08-17 09:00:00.571 Status: dzVents: Info: ------ Finished Luftdaten
2020-08-17 09:00:00.576 Status: EventSystem: Script event triggered: /home/hein/domoticz/dzVents/runtime/dzVents.lua
2020-08-17 09:00:01.088 Status: dzVents: Info: Handling httpResponse-events for: "luftdatenRetrieved"
2020-08-17 09:00:01.088 Status: dzVents: Info: ------ Start internal script: Luftdaten: HTTPResponse: "luftdatenRetrieved"
2020-08-17 09:00:01.089 Status: dzVents: Info: ------ Finished Luftdaten
2020-08-17 09:00:03.819 (Slimme meter) P1 Smart Meter (Stroom)
2020-08-17 09:00:05.817 (Dummy) Light/Switch (CV_switch_auto)
2020-08-17 09:00:05.811 Status: User: Admin initiated a switch command (13/CV_switch_auto/On)
2020-08-17 09:00:09.268 (RFXCOM) Temp (Oregon)
2020-08-17 09:00:13.812 (Slimme meter) P1 Smart Meter (Stroom)
2020-08-17 09:00:17.051 (Dummy) Light/Switch (CV_switch_auto)
2020-08-17 09:00:17.226 (RFXCOM) Temp + Humidity (TFA1)
2020-08-17 09:00:17.031 Status: User: Admin initiated a switch command (13/CV_switch_auto/Off) 
Here it is.
Because average temp is 24.09 and setpoint is 19.2 I would expect the script to check and switch off immediately.

On the other hand I can understand that the script was just acting on it's predicted time and that switching the switch doesn't alter the fact that the script will start again in due time.
So the script needs to get another triggger outside these 15 minutes to execute and test.

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 9:14
by waaren
HvdW wrote: Monday 17 August 2020 9:05 Because average temp is 24.09 and setpoint is 19.2 I would expect the script to check and switch off immediately.
According to the log that is what the the script does. It switches device 'CV_switch_auto' to Off

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 10:36
by HvdW

Code: Select all

2020-08-17 09:00:05.811 Status: User: Admin initiated a switch command (13/CV_switch_auto/On)
2020-08-17 09:00:09.268 (RFXCOM) Temp (Oregon)
2020-08-17 09:00:13.812 (Slimme meter) P1 Smart Meter (Stroom)
2020-08-17 09:00:17.051 (Dummy) Light/Switch (CV_switch_auto)
2020-08-17 09:00:17.226 (RFXCOM) Temp + Humidity (TFA1)
2020-08-17 09:00:17.031 Status: User: Admin initiated a switch command (13/CV_switch_auto/Off)
Indeed, the first part is the normal behaviour.
In the second part I switch the switch myself and then nothing happens.

Re: Heating control script not acting as expected  [Solved]

Posted: Monday 17 August 2020 11:01
by waaren
HvdW wrote: Monday 17 August 2020 10:36 Indeed, the first part is the normal behavior.
In the second part I switch the switch myself and then nothing happens.
If you want the script to also trigger on a device update of CV_switch_auto, you need to change the script to

Code: Select all

-- assumptions:
-- the setpoint is set by a dummy device (not a selector type)

local CV_SWITCH = 'CV_switch_auto' -- switch device
local TEMP_SETPOINT = 'Thermostaat_setpoint' -- selector dummy device
local TEMP_SENSOR_1 = 'TFA1'
local TEMP_SENSOR_2 = 'Oregon'
local BAT_THRESHOLD = 30
local LOGGING = true

return
{
    on =
    {
        timer =
        {
            'every 15 minutes',
        },
        devices =
        {
            CV_SWITCH,
        },
    },

    --LOG levell: This is the log level you want for this script. Can be domoticz.LOG_INFO, domoticz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG or domoticz.LOG_ERROR
    --marker: A string that is prefixed before each log message. That way you can easily create a filter in the Domoticz log to see just these messages.

    logging =
    {
        level = LOGGING and domoticz.LOG_DEBUG or domoticz.LOG_ERROR,
        --level = LOGGING and domoticz.LOG_ERROR,
        marker = 'dzVents heating',
    },

    execute = function(dz)

        -- collect all input data
        local boiler_switch_state = dz.devices(CV_SWITCH).state
        local temp_1 = dz.devices(TEMP_SENSOR_1).temperature
        local temp_2 = dz.devices(TEMP_SENSOR_2).temperature
        local bat_1 = dz.devices(TEMP_SENSOR_1).batteryLevel
        local bat_2 = dz.devices(TEMP_SENSOR_2).batteryLevel
        local setpointValue = dz.devices(TEMP_SETPOINT).setPoint
        local average_temp = 40  -- very high so the heating will not fire
        local temp_diff = temp_1 - temp_2

        -- info only on log level = LOG_DEBUG
        dz.log('CV_SWITCH      : ' .. boiler_switch_state, dz.LOG_DEBUG)
        dz.log('Temp_1         : ' .. temp_1, dz.LOG_DEBUG)
        dz.log('Temp_2         : ' .. temp_2, dz.LOG_DEBUG)
        dz.log('Bat_1          : ' .. bat_1, dz.LOG_DEBUG)
        dz.log('Bat_2          : ' .. bat_2, dz.LOG_DEBUG)
        dz.log('Setpoint_value : ' .. setpointValue, dz.LOG_DEBUG)

        -- test if conditions are met and set acting values
        if ((temp_1 - temp_2 > 1.1) or (temp_2 - temp_1 > 1.1)) then
            dz.log('Temperature difference between sensors is more than 1.1 degrees : ' .. temp_diff, dz.LOG_DEBUG)
            dz.notify('Notifyer', 'Temperature difference between sensors is more than 1.1 degrees' .. temp_diff , dz.PRIORITY_HIGH)
        elseif (bat_1 < BAT_THRESHOLD) then
            dz.log('Battery level of temperature sensor 1 = ' .. bat_1, dz.LOG_DEBUG)
            dz.notify('Notifyer', 'Temperature sensor 1 ' .. bat_1 .. 'battery level low.',dz.PRIORITY_HIGH)
        elseif (bat_2 < BAT_THRESHOLD) then
            dz.log('Battery level of temperature sensor ' .. bat_2, dz.LOG_DEBUG)
            dz.notify('Notifyer', 'Temperature sensor 2 ' .. bat_2 .. 'battery level low.',dz.PRIORITY_HIGH)
        else
            average_temp = (temp_1 + temp_2)/2
            dz.log('Average temperature sensors value is : ' .. average_temp, dz.LOG_DEBUG)
        end

        -- now determine what to do
        if (average_temp >= setpointValue) then
            dz.devices(CV_SWITCH).switchOff()
            dz.log('Target temperature reached, boiler off')
        elseif (average_temp <= (setpointValue -1))  then
            dz.log('Average temperature more than 1 degree below setpointValue, switchOn during 10 minutes')
            dz.devices(CV_SWITCH).switchOn().forMin(10)
        elseif ((average_temp > (setpointValue -1)) and (average_temp < setpointValue)) then
            dz.log('Average temperature less than 1 degree below setpointValue, switchOn during 5 minutes')
            dz.devices(CV_SWITCH).switchOn().forMin(5)
        else  -- not predicted situation
            dz.log('Unpredicted situation, boiler off')
            dz.devices(CV_SWITCH).switchOff()
        end

    end
}

Re: Heating control script not acting as expected

Posted: Monday 17 August 2020 22:04
by HvdW
IS THAT ALL?
GREAT!
THANKS @waaren!