Plans(Timers) switching ON/OFF on Selector Switch

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

Moderator: leecollings

Post Reply
Kamil
Posts: 8
Joined: Saturday 26 January 2019 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Newest β
Location: PL
Contact:

Plans(Timers) switching ON/OFF on Selector Switch

Post by Kamil »

Hello everybody,
is it possible to turn ON and OFF plans on selector switch using dzVents script?
For example I have one selector switch named "Heater" and I created plans for it.
On level "0" I want to have option that will disable all plans events on this switch and on level "10" it will enable them. The rest of levels will set temperature on it (temperature handling I already got).

For example:

Code: Select all

return {
	on = {
		devices = {
			'Heater'
		}
	},
	execute = function(domoticz, device)
		if domoticz.device('Heater').switchSelector(0) then
		
		-- !!! and here I don't now how to appeal to plans !!!
		  
	end
}
Anyone can help?
Thank You in advance.
Last edited by Kamil on Sunday 17 February 2019 19:44, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Plans switching ON/OFF on Selector Switch

Post by waaren »

Kamil wrote: Sunday 17 February 2019 14:25 is it possible to turn ON and OFF plans on selector switch using dzVents script?
For example I have one selector switch named "Heater" and I created plans for it.
On level "0" I want to have option that will disable all plans events on this switch and on level "10" it will enable them. The rest of levels will set temperature on it (temperature handling I already got).
can you elaborate a bit on the plans stuff and what you how you set the temperatures ?

Generic approach could be something like

Code: Select all

return
		{
			on		 =	{
							devices =   {
											'Heater'
										}
						},
					 
		logging		 =	{ 
							level   =  domoticz.LOG_DEBUG,   
							marker  =  "heater", 
						},

	execute = function(dz, device)

		heaterLevel = device.level

		local function logWrite(str,level)
			dz.log(tostring(str),level or dz.LOG_DEBUG)
		end
        
        local function makeActionTable(actionsString) 
            local t = {}
            local i = 0
            for str in actionsString:gmatch("[^|%s]+") do
                t[i*10] = str
                i = i + 1
            end
            return t                
        end
        
        local actionTable = makeActionTable(device.levelActions)
		logWrite(device.name .. " level: " .. heaterLevel  .. "; level Name: " .. device.levelName .. "; level Action: " .. actionTable[heaterLevel])

		if	  heaterLevel == 0 then
			logWrite("Action for level " .. heaterLevel )
		elseif  heaterLevel == 10 then   
			logWrite("Action for level " .. heaterLevel )
		elseif  heaterLevel == 20 then   
			logWrite("Action for level " .. heaterLevel )
		elseif  heaterLevel == 30 then   
			logWrite("Action for level " .. heaterLevel )
		end
	end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kamil
Posts: 8
Joined: Saturday 26 January 2019 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Newest β
Location: PL
Contact:

Re: Plans switching ON/OFF on Selector Switch

Post by Kamil »

Well...I'm using Bluetooth thermostats, so I'm connecting to them using external library and script.
For example:
script:///home/pi/eq3/eq3.exp XX:XX:XX:XX:XX:XX temp 5.0
Switch events.jpg
Switch events.jpg (165.89 KiB) Viewed 1215 times
so temperature settings on thermostat is solved by this script calling from Selector Switch(dummy switch) settings.
Now I need just dzVents for disabling plans (Timers) on this switch (for example when I'm going to holiday). I do not want to unselect every single event in switch Timers.
Timers.jpg
Timers.jpg (107.37 KiB) Viewed 1215 times
So the question is how to call to Timers from Selector Switch in dzVents to turn them On and OFF with just one click (best way using one of Selector levels)?

Is my explanation clear?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Plans(Timers) switching ON/OFF on Selector Switch

Post by waaren »

Yes is, clear.
create two extra levels in Heater device, named "Disable timers" and "Enable timers". These are used to trigger the actions from this script.

Code: Select all

-- manageTimers

local httpResponses = "manageTimers"

return {
    on      =   {   
                    devices         =   { "Heater"   }, 
                    httpResponses   =   { httpResponses .. "*"   } 
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
               
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(url, response, delay)
            local response  = response or ""
            local delay     = delay or 0
            dz.openURL({    url = url,
                            method = "GET",
                            callback = response }).afterSec(delay)
        end
      
        local function actOnTimers(rt,action,id)
            for key in ipairs(rt) do
                logWrite(rt[key].DeviceRowID)
                if tonumber(rt[key].DeviceRowID) == tonumber(id) then
                    triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?type=command&param=" .. action .. "timer&idx=" .. rt[key].TimerID)
                    logWrite(action:gsub("^%l", string.upper) .. "d " .. rt[key].TimerTypeStr .." timer (id:" .. rt[key].TimerID .. ") for ".. rt[key].DevName,dz.LOG_FORCE )
                end
            end
        end    
        
        local function makeJSON(action,idx)
            return dz.settings['Domoticz url'] .. "/json.htm?type=schedules&filter=device",httpResponses .. "_" .. action .. "_" .. idx
        end
        
        local function extractFromResponse(str)
            return str:match("_([^_]+)"), str:match("%d+") -- Extract action and idx from response
        end
        
        if not item.isHTTPResponse then
            if item.levelName:find("able timers") then      
                triggerJSON(makeJSON(item.levelName:lower():match("%a+"),item.id))    -- add action and device id to callback and get all deviceTimers
            end    
        elseif item.ok then                                      -- statusCode == 2xx
            logWrite(item.trigger)
            actOnTimers(item.json.result,extractFromResponse(item.trigger)) -- get action and device id from callback and act on it
        else 
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Kamil
Posts: 8
Joined: Saturday 26 January 2019 15:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Newest β
Location: PL
Contact:

Re: Plans(Timers) switching ON/OFF on/by Selector Switch

Post by Kamil »

waaren wrote: Monday 18 February 2019 1:45 Yes is, clear.
create two extra levels in Heater device, named "Disable timers" and "Enable timers". These are used to trigger the actions from this script.

Code: Select all

-- manageTimers

local httpResponses = "manageTimers"

return {
    on      =   {   
                    devices         =   { "Heater"   }, 
                    httpResponses   =   { httpResponses .. "*"   } 
                },
                
    logging =   {   
                    level           =   domoticz.LOG_ERROR,
                    marker          =   httpResponse   
                },
                
    execute = function(dz, item)
               
        local function logWrite(str,level)
            dz.log(tostring(str),level or dz.LOG_DEBUG)
        end
        
        local function triggerJSON(url, response, delay)
            local response  = response or ""
            local delay     = delay or 0
            dz.openURL({    url = url,
                            method = "GET",
                            callback = response }).afterSec(delay)
        end
      
        local function actOnTimers(rt,action,id)
            for key in ipairs(rt) do
                logWrite(rt[key].DeviceRowID)
                if tonumber(rt[key].DeviceRowID) == tonumber(id) then
                    triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?type=command&param=" .. action .. "timer&idx=" .. rt[key].TimerID)
                    logWrite(action:gsub("^%l", string.upper) .. "d " .. rt[key].TimerTypeStr .." timer (id:" .. rt[key].TimerID .. ") for ".. rt[key].DevName,dz.LOG_FORCE )
                end
            end
        end    
        
        local function makeJSON(action,idx)
            return dz.settings['Domoticz url'] .. "/json.htm?type=schedules&filter=device",httpResponses .. "_" .. action .. "_" .. idx
        end
        
        local function extractFromResponse(str)
            return str:match("_([^_]+)"), str:match("%d+") -- Extract action and idx from response
        end
        
        if not item.isHTTPResponse then
            if item.levelName:find("able timers") then      
                triggerJSON(makeJSON(item.levelName:lower():match("%a+"),item.id))    -- add action and device id to callback and get all deviceTimers
            end    
        elseif item.ok then                                      -- statusCode == 2xx
            logWrite(item.trigger)
            actOnTimers(item.json.result,extractFromResponse(item.trigger)) -- get action and device id from callback and act on it
        else 
            logWrite("Could not get (good) data from domoticz. Error (" .. (item.statusCode or 999) .. ")"  ,dz.LOG_ERROR)
            logWrite(item.data)
        end
    end
}
Good morning Waaren!
Thank You for Your response!
Looks really promising. Can You show in Your code where is connection between script and those two levels in selector switch?
Or please give me small explanation what exactly happening here:

Code: Select all

        local function actOnTimers(rt,action,id)
            for key in ipairs(rt) do
                logWrite(rt[key].DeviceRowID)
                if tonumber(rt[key].DeviceRowID) == tonumber(id) then
                    triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?type=command&param=" .. action .. "timer&idx=" .. rt[key].TimerID)
                    logWrite(action:gsub("^%l", string.upper) .. "d " .. rt[key].TimerTypeStr .." timer (id:" .. rt[key].TimerID .. ") for ".. rt[key].DevName,dz.LOG_FORCE )
                end
            end
        end
Thank You in advance!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Plans(Timers) switching ON/OFF on Selector Switch

Post by waaren »

If you are not yet familiar with dzvents then please look at the 10 lines using dzVents with Domoticz for an intro to dzVents and howto get this script active in domoticz.

The connection is in the line

Code: Select all

if item.levelName:find("able timers") then
So if the device is switched to either "Disable timers" or to "Enable timers" it will do the required.

Code: Select all

        local function actOnTimers(rt,action,id)
            for key in ipairs(rt) do
                logWrite(rt[key].DeviceRowID)
                if tonumber(rt[key].DeviceRowID) == tonumber(id) then
                    triggerJSON(dz.settings['Domoticz url'] .. "/json.htm?type=command&param=" .. action .. "timer&idx=" .. rt[key].TimerID)
                    logWrite(action:gsub("^%l", string.upper) .. "d " .. rt[key].TimerTypeStr .." timer (id:" .. rt[key].TimerID .. ") for ".. rt[key].DevName,dz.LOG_FORCE )
                end
            end
        end

rt: the table from domoticz with all timers for all devices
action: "enable" or "disable"
id: id of your selector device

Looping through the table this function looks for timer records with the id of your selector device and for every found one, it triggers an API to domoticz to enable or disable it.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests