IFTTT api
Posted: Saturday 17 August 2019 11:51
Last thing on my domotica to do list is to update the IFTTT api script (see included).
My domoticz configuration works with google home and json calls and a script IFTTTVAR made by @waaren.
This works 100%.
Main functionality is to update a user variable and a IFTTT script reads the uservariable and executes switch or group actions based on the data in the uservar. The structure of the uservar is for example: denon_volume/Off/off/undefined
I like to use
- a formal json format for this
- possibility (see above) to do more device or group actions based on one json message.
Some possible datastructures (please adjust these to confirm to json standards)
Functionality: group on/off, on/off based on ids and names, delays, repeats after etc. and more actions in one body
{
{"object":"Schakelgroepen","onoff":"On","item_name":"Sfeerverlichting binnen"},
{"object":"lantaarn","onoff":"On",afterMin:"1",forMin:"10"},
{"object":"usb lamp","onoff":"On","item_name":"Flame"},
{ "object":"denon_volume","onoff":"On","item_name":"40","stype":"2"}
}
Problem is that these wishes are to complex to program (for me).
My skills dont reach further then making easy scripts and adjust complex scripts made by pro's like you and using them more then 100% with more then 50 objects...
simular scripts
https://www.domoticz.com/forum/viewtopi ... 59&t=28655
Current working code
My domoticz configuration works with google home and json calls and a script IFTTTVAR made by @waaren.
This works 100%.
Main functionality is to update a user variable and a IFTTT script reads the uservariable and executes switch or group actions based on the data in the uservar. The structure of the uservar is for example: denon_volume/Off/off/undefined
I like to use
- a formal json format for this
- possibility (see above) to do more device or group actions based on one json message.
Some possible datastructures (please adjust these to confirm to json standards)
Functionality: group on/off, on/off based on ids and names, delays, repeats after etc. and more actions in one body
{
{"object":"Schakelgroepen","onoff":"On","item_name":"Sfeerverlichting binnen"},
{"object":"lantaarn","onoff":"On",afterMin:"1",forMin:"10"},
{"object":"usb lamp","onoff":"On","item_name":"Flame"},
{ "object":"denon_volume","onoff":"On","item_name":"40","stype":"2"}
}
Problem is that these wishes are to complex to program (for me).
My skills dont reach further then making easy scripts and adjust complex scripts made by pro's like you and using them more then 100% with more then 50 objects...
simular scripts
https://www.domoticz.com/forum/viewtopi ... 59&t=28655
Current working code
Code: Select all
local iftttvar = "iftttvar"
return
{
on =
{ variables = { iftttvar },
},
logging =
{
level = domoticz.LOG_ERROR,
marker = 'ifttt',
},
execute = function(dz, item, info)
local validBaseTypes = { device=true, group=true, scene=true, uservariable=false, camera=false }
local validActions = { ON=true, OFF=true }
local result = dz.utils.stringSplit(item.value,'/') -- aangepast was geen local
local name = result[1]
local action = string.upper(result[2])
local parm1 = result[3]
local stype = result[4]
local logcode = 3
local messageTable = {}
local add = 'add'
local del = 'del'
local chg = 'chg'
local function globalMessage(action, message, logcode)
if logcode == nil then logcode = 3 end
if action == add and logcode > 0 then
messageTable = dz.helpers.addMessage(dz, item, info, message, messageTable, logcode)
elseif action == del then
dz.globalData.mylogging = message
dz.devices('timerlog').updateText(message)
elseif action == chg then
dz.helpers.dumpMessages(dz, messageTable)
end
end
if parm1 == nil then parm1 = 'undefined' end -- par1 is de selectornaam of selectorlevel
if stype == nil then stype = 'undefined' end -- stype is of par naam 1 of level is 2
local function getBaseType(name)
for i, tuple in ipairs(_G.domoticzData) do
if tuple.name == name then
return tuple.baseType, tuple.id
end
end
end
local baseType, idx = getBaseType(name)
if baseType and validBaseTypes[baseType] then
local target
if baseType == 'device' then
target = dz.devices(name)
elseif baseType == 'group' then
target = dz.groups(name)
elseif baseType == 'scene' then
target = dz.scenes(name)
validActions.OFF = nil -- No Off action for scenes
end
if validActions[action] then
if action == 'ON' then
if parm1 ~= 'undefined' then
if stype == '1' or stype == 'undefined' then
globalMessage(add, ' IFTTT AAN: ,settings: ' .. tostring(item.value), logcode)
target.switchSelector(parm1) --parm selector_name
elseif stype == '2' then
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchSelector(tonumber(parm1)) --parm selector_id
else -- foute waarde
globalMessage(add, ' IFTTT AAN: foute waarde, settings: ' .. tostring(item.value), logcode)
end
else
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchOn()
end
elseif action == 'OFF' then
globalMessage(add, ' IFTTT AAN: settings: ' .. tostring(item.value), logcode)
target.switchOff()
else -- no other actions implemented yet
globalMessage(add, 'IFTTT AAN: foute action waarde, settings: ' .. action, logcode)
end
else
globalMessage(add, 'IFTTT AAN: action: ' .. action .. ' is not implemented for ' .. baseType .. ' Name ' .. name .. '!', logcode)
end
elseif baseType then
globalMessage(add, ' IFTTT AAN: onbekende basetype: ' .. baseType, logcode) -- dz.LOG_ERROR evt ook doorgeven
else
globalMessage(add, ' IFTTT AAN: name not found!: ' .. name, logcode)
end
globalMessage(chg) -- dump
end
}