return { on = { timer = { 'at *:00', 'at *:01', 'at *:02' }, httpResponses = { 'triggerStates' }, customEvents = { 'GetSpotswitch', }, }, logging = { level = domoticz.LOG_INFO, marker = 'SpotSwitch', }, execute = function(domoticz, item) if (item.isHTTPResponse) then if (item.ok) then if (item.isJSON) then local result_table = item.json local DevName, state, device, scene if (item.trigger == 'triggerStates') then for key,value in pairs(result_table.States) do DevName = 'SpotSwitch' .. result_table.States[key].index if (domoticz.utils.deviceExists(DevName)) then state = string.lower(result_table.States[key].state) device = domoticz.devices(DevName) if string.find('on', state) then if ( device._state~="On") then device.switchOn() domoticz.log('Updated ' .. device.name..': ' .. device.state) end end if string.find('off', state) then if ( device._state~="Off") then device.switchOff() domoticz.log('Updated ' .. device.name..': ' .. device.state) end end end if (domoticz.utils.groupExists(DevName)) then state = string.lower(result_table.States[key].state) group = domoticz.groups(DevName) if (string.find('on', state)) then if ( group._state~="On") then group.switchOn() domoticz.log('Updated ' .. group.name..': ' .. group.state) end end if (string.find('off', state)) then if ( group._state~="Off") then group.switchOff() domoticz.log('Updated ' .. group.name..': ' .. group.state) end end end end if result_table.Prices~=nil then DevName = 'GasPrice' if domoticz.utils.deviceExists(DevName) then device = domoticz.devices(DevName) device.updateCustomSensor(result_table.Prices.gas) end DevName = 'ElectricityPrice' if domoticz.utils.deviceExists(DevName) then device = domoticz.devices(DevName) device.updateCustomSensor(result_table.Prices.electricity) end DevName = 'ElectricityAverage' if domoticz.utils.deviceExists(DevName) then device = domoticz.devices(DevName) device.updateCustomSensor(result_table.Prices.average) end end end else domoticz.log('HTTP is not json', domoticz.LOG_ERROR) end else domoticz.log('There was a problem handling the request: ' .. url, domoticz.LOG_ERROR) domoticz.log(item, domoticz.LOG_ERROR) end elseif (item.isCustomEvent) then -- get the states from the SpotSwitch server change the key to yours domoticz.openURL({ url = 'https://spotswitch.net/api/?key=123456-not.registed.example.only@kampmation.eu', method = 'GET', callback = 'triggerStates', }) else -- delay to spread the server load, change the time to your zipcode divided by 166, for example 1217WD, it is 7,331325 so use 7 domoticz.emitEvent('GetSpotswitch', '').afterSec(13) end end }