Howto set selector switch by dzvents.  [Solved]

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

Moderator: leecollings

Post Reply
jkimmel
Posts: 129
Joined: Monday 25 November 2013 17:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Mallorca
Contact:

Howto set selector switch by dzvents.  [Solved]

Post by jkimmel »

It is about heating and cooling.
I have some scripts dealing with heating and cooling.
For each room exists a selector switch actually is set by time control. Times are set for heating only.

There is also a switch (WP) switching between heating, cooling and off.

I would like to have times set by script depending on state of WP, so two different sets for heating and cooling

Here is one of my sripts:

Code: Select all

-- assumptions:
-- the setpoint is set by a selector dummy device where the values are numeric temperatures
-- but you can easily change it to a setpoint device

local BOILER_DEVICE = 'FB 21 Heizkreis Salon' -- switch device
local SETPOINT_DEVICE = 'Solltemperatur Salon' -- selector dummy device
local TEMPERATURE_SENSOR = 'Salon'
local CHANGE_DEVICE = 'Modus Waermepumpe' -- selector dummy device
local HEATPUMP_RETURN = 'WP Ruecklauf' -- controls dewpoint of walls
local SMOOTH_FACTOR = 3
local LOGGING = true

return {
   on = {
      timer = {
         'every minute',
      },
      devices = {
         TEMPERATURE_SENSOR,
         SETPOINT_DEVICE,
         CHANGE_DEVICE
      }
   },
   data = {
      temperatureReadings = { history = true, maxItems = SMOOTH_FACTOR }
   },
   --logging = {
    --level = domoticz.LOG_DEBUG,
    --marker = "Hey wz"
   --},
   active = true,
   execute = function(domoticz, device, triggerInfo)

      local avgTemp
      local temperatureReadings = domoticz.data.temperatureReadings
      local sensor = domoticz.devices(TEMPERATURE_SENSOR)
      local current = sensor.temperature
      local boiler = domoticz.devices(BOILER_DEVICE)
      local setpoint = domoticz.devices(SETPOINT_DEVICE)
      local change = domoticz.devices(CHANGE_DEVICE)
   	  local sensor_HP = domoticz.devices(HEATPUMP_RETURN)
   	  local returnT = sensor_HP.temperature	   
   	  local dewpoint = sensor.dewPoint
   

      -- first check if the sensor got a new reading or the setpoint was changed:
    if (triggerInfo.type == domoticz.EVENT_TYPE_DEVICE) then

        if (sensor.changed) then
            -- sensor just reported a new reading
            -- add it to the readings table

            if (current ~= 0 and current ~= nil) then
               temperatureReadings.add(current)
            else
               -- no need to be here, weird state detected
               domoticz.log('Strange sensor reading.. skiping', domoticz.LOG_ERROR)
               return
            end

        if (domoticz.devices(SETPOINT_DEVICE).changed) then
            -- a new setpoint was set
            if LOGGING then domoticz.log('Setpoint was set to ' .. setpoint.state, domoticz.LOG_DEBUG) end
        end
        
        if (domoticz.devices(CHANGE_DEVICE).changed) then
            -- a new Modus was set
            if LOGGING then domoticz.log('Modus was set to ' .. change.state, domoticz.LOG_DEBUG) end
         
        end

      -- now determine what to do
        if  (setpoint.state == nil) or (setpoint.state == 'Off') or (change.state == nil) or (change.state == 'Off') then
            if LOGGING then domoticz.log('Modus  or Setpoint was set off', domoticz.LOG_DEBUG) end
            boiler.switchOff()
            return -- we're done here
        end

        local setpointValue = tonumber(setpoint.state)

        -- determine at which temperature the boiler should be
        -- switched on
      
        -- don't use the current reading but average it out over
        -- the past <SMOOTH_FACTOR> readings (data smoothing) to get rid of noise, wrong readings etc
        local avgTemp = temperatureReadings.avg(1, SMOOTH_FACTOR, current) -- fallback to current when history is empty

        if LOGGING then
            domoticz.log('Average: ' .. avgTemp, domoticz.LOG_DEBUG)
            domoticz.log('Setpoint: ' .. setpointValue, domoticz.LOG_DEBUG)
            domoticz.log('Current boiler state: ' .. boiler.state, domoticz.LOG_DEBUG)
        end
      
        if (change.state == 'Heizen') then	
		
      	    if (avgTemp >= setpointValue and boiler.state == 'On') then
        	    if LOGGING then domoticz.log('Target temperature reached, boiler off', domoticz.LOG_DEBUG) end
                boiler.switchOff()
     	
            elseif (avgTemp < setpointValue and boiler.state == 'Off') then
     	            if LOGGING then domoticz.log('Temperature to low, boiler On', domoticz.LOG_DEBUG) end
     	            boiler.switchOn()
     	     end       
     	            
        elseif (change.state == 'Kuehlen') then   
       
         	if ((avgTemp <= dewpoint or returnT < (dewpoint - 2)) and boiler.state == 'On') then
        	    if LOGGING then domoticz.log('Target temperature reached, boiler off', domoticz.LOG_DEBUG) end
                boiler.switchOff()
                
     	    elseif (returnT > dewpoint and avgTemp > setpointValue and boiler.state == 'Off') then
     	        if LOGGING then domoticz.log('Target temperature not yet reached, boiler on', domoticz.LOG_DEBUG) end
     	        boiler.switchOn()
     		else
     	   	 return
            end 
        end
    end
end
end
 }
If somebody thinks this script could be more elegant, help is appreciated
Rfxcom
Raspi 4
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Howto set selector switch by dzvents.

Post by emme »

once you hace a domoticz.EVENT_TYPE_DEVICE you can use device.name (device is the parameter used as second arg in execute) to trigger the event based on the device name ;)

switch selector devices can be setted by using device.setLevel(xx) where XX is the level 0 to 100 step 10
The most dangerous phrase in any language is:
"We always done this way"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Howto set selector switch by dzvents.

Post by waaren »

jkimmel wrote: Thursday 25 July 2019 13:38 I have some scripts dealing with heating and cooling.
For each room exists a selector switch actually is set by time control. Times are set for heating only.
There is also a switch (WP) switching between heating, cooling and off.
I would like to have times set by script depending on state of WP, so two different sets for heating and cooling
Sorry but I seem to miss your requirement. What is the script supposed to do when triggered by the timer ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Howto set selector switch by dzvents.

Post by emme »

sorry, my frist question is...
does this script works like expected?
because there are several issues (not really errors) with it.
you use to call same devices with 2 different names and I'm a bit unclear about the 2 section and their use.

My understanding is that at first you analyze the status anche changes just for log purpose, then you analyze the temps and switches and take action....

Can you clarify exactely what are you expect from it in the different cases?

Another thing: from my point of view, if you trigger every minute it does not need to be triggered by switch/temp change... since you're not monitoring a nuclear reactor, a max of 60secs to adapt switches can be handled with no issues.
more... if temp change the script runs, maybe after 10secs is starts back for time run... so you run it twice while once was enoght...
I would suggest you to choose to trigger every minute OR by temp/switch change.

ciao
M
The most dangerous phrase in any language is:
"We always done this way"
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest