-- demo device script -- script names have three name components: script_trigger_name.lua -- trigger can be 'time' or 'device', name can be any string -- domoticz will execute all time and device triggers when the relevant trigger occurs -- -- copy this script and change the "name" part, all scripts named "demo" are ignored. -- -- Make sure the encoding is UTF8 of the file -- -- ingests tables: devicechanged, otherdevices,otherdevices_svalues -- -- device changed contains state and svalues for the device that changed. -- devicechanged['yourdevicename']=state -- devicechanged['svalues']=svalues string -- -- otherdevices and otherdevices_svalues are arrays for all devices: -- otherdevices['yourotherdevicename']="On" -- otherdevices_svalues['yourotherthermometer'] = string of svalues -- -- Based on your logic, fill the commandArray with device commands. Device name is case sensitive. -- -- Always, and I repeat ALWAYS start by checking for the state of the changed device. -- If you would only specify commandArray['AnotherDevice']='On', every device trigger will switch AnotherDevice on, which will trigger a device event, which will switch AnotherDevice on, etc. -- -- The print command will output lua print statements to the domoticz log for debugging. -- List all otherdevices states for debugging: -- for i, v in pairs(otherdevices) do print(i, v) end -- List all otherdevices svalues for debugging: -- for i, v in pairs(otherdevices_svalues) do print(i, v) end -- -- TBD: nice time example, for instance get temp from svalue string, if time is past 22.00 and before 00:00 and temp is bloody hot turn on fan. require "scripts/lua/functions" time = os.time() annee = os.date('%Y',time) mois = os.date('%m',time) heures = tonumber(os.date('%H',time)) minutes = tonumber(os.date('%M',time)) secondes = tonumber(os.date('%S',time)) jour = tonumber(os.date('%w',time)) jour_s = os.date('%d',time) heurmin = heures * 60 + minutes PUISSANCE_DELESTAGE = 750 -- Watts function update_meter(device, id, power, energy, index) commandArray[index] = {['UpdateDevice'] = id .. "|0|" .. power .. ";" .. energy} end local function round(num, dec) --print("Round "..tostring(num)) return ( math.floor( tonumber(num) * 10^dec ) / 10^dec ) end local function updatenum(dev, value1) --local cmd = string.format("%d|0|%d", otherdevices_idx[dev], math.floor(value1)) local cmd = tostring(otherdevices_idx[dev]).."|0|"..tostring(round(value1, 3)) table.insert (commandArray, { ['UpdateDevice'] = cmd } ) end function getConsommationHC(idx) date=annee..'-'..mois..'-'..jour_s --date_end=annee..'-'..mois..'-'..math.floor(jour_s + 1) -- query = 'select max(value) - min(value) from Meter mc where DeviceRowID =' ..idx -- .. ' and date like "' .. date ..'%"' -- .. ' and (date >= "' .. date_deb.. ' 21:29:59" or date <= "'.. date_deb ..' 05:30:00")' query = 'select sum(hc) from (select max(value) - min(value) as hc from Meter mc where DeviceRowID =' ..idx .. ' and date like "' .. date ..'%"' .. ' and (date >= "' .. date.. ' 21:29:59"' .. ' and date < "' .. date ..' 23:59:59")' .. ' union all' .. ' select max(value) - min(value) as hc from Meter mc where DeviceRowID =' .. idx .. ' and date like "' .. date ..'%"' .. ' and (date < "' .. date ..' 05:30:00")' .. ' and (date > "' .. date ..' 00:00:00")' .. ') as c' --query = 'select max(value) - min(value) from Meter mc where DeviceRowID =' ..idx -- .. ' and date like "' .. date ..'%"' -- .. ' and not(date > "' .. date.. ' 05:29:59" and date <= "'.. date ..' 21:30:00")' print("query="..query) local bSuccess, aRows, iCount = executeQuery(idx, query) if aRows[1][1] then print("Retour executeQuery "..tostring(bSuccess)..' '..aRows[1][1]) return aRows[1][1] end print('retour à 0') return 0 end function getConsommationHP(idx) date=annee..'-'..mois..'-'..jour_s query = 'select max(value) - min(value) from Meter mc where DeviceRowID =' ..idx .. ' and date like "' .. date ..'%"' .. ' and (date > "' .. date.. ' 05:29:59" and date <= "'.. date ..' 21:30:00")' --print("query="..query) local bSuccess, aRows, iCount = executeQuery(idx, query) --print("Retour executeQuery "..tostring(bSuccess)..' '..aRows[1][1]) return aRows[1][1] end function deleteWrongData(idx) -- delete from MultiMeter where DeviceRowid = 1135 and date >= "2021-06-02 00:00:00" -- and date like "2021-06-02 00:00%"; date=annee..'-'..mois..'-'..jour_s query = 'delete from MultiMeter where DeviceRowID =' ..idx .. ' and date like "' .. date ..' 00:00%"' .. ' and (date >= "' .. date.. ' 00:00:00")' print("query delete="..query) local bSuccess, aRows, iCount = executeQuery(idx, query) end commandArray = {} -- ================================================================== -- INTENSITE_GENERALE -- ================================================================== if (devicechanged['Oregon_Conso']) then local tab = getValuesInTab('Oregon_Conso') print("Conso "..otherdevices['Oregon_Conso']) values = split(otherdevices['Oregon_Conso'], ";") if (tonumber(values[1]) < 0) then if (tonumber(values[1]) == -0.1) then watt = - tonumber(values[2]) else watt = tonumber(values[1]) * 100 - tonumber(values[2]) end else watt = (tonumber(values[1]) * 100 + tonumber(values[2])) --/ 1.42 end watt = watt - 80 print("Conso "..tostring(watt)) --print("mesure"..otherdevices['INTENSITE_GENERALE']) amp = watt / 230 --math.max(0,round(otherdevices['INTENSITE_GENERALE'],3) - 0.54) * 7 / 8 watt_conso = watt --* 0.9459 updatenum('INTENSITE_GENERALE', amp) print("consommation Amp="..tostring(amp).." conso="..tostring(watt_conso)) updatenum('CONSOMMATION_GENERALE', round(watt_conso,0)) local tab = getValuesInTab('SolaireProduction') local watt_solaire = tonumber(tab[1]) local watt_solaire_jour = otherdevices_svalues['SolaireProduction'] print("Suivi "..tostring(watt_conso - watt_solaire)) if (minutes%5 == 0) then date = annee..'-'..mois..'-'..jour_s counter_conso = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['Consommation_Apparente']..' order by Date desc limit 1;"') counter_conso=tonumber(counter_conso) or 0 print('counter_conso='..counter_conso) value_conso = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['Consommation_Apparente']..' order by Date desc limit 1;"') value_conso=tonumber(value_conso) or 0 print('value_conso='..value_conso) counter_solar = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['SolaireProduction']..' order by Date desc limit 1;"') counter_solar=tonumber(counter_solar) or 0 print('counter_solar='..counter_solar) value_solar = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['SolaireProduction']..' order by Date desc limit 1;"') value_solar=tonumber(value_solar) or 0 print('value_solar='..value_solar) counter_injection = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['INJECTION']..' order by Date desc limit 1;"') counter_injection=tonumber(counter_injection) or 0 print('counter_injection='..counter_injection) value_injection = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['INJECTION']..' order by Date desc limit 1;"') value_injection=tonumber(value_injection) or 0 print('value_injection='..value_injection) counter_radiateur = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['Injection_Radiateur']..' order by Date desc limit 1;"') counter_radiateur=tonumber(counter_radiateur) or 0 print('counter_radiateur='..counter_radiateur) value_radiateur = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['Injection_Radiateur']..' order by Date desc limit 1;"') value_radiateur=tonumber(value_radiateur) or 0 print('value_radiateur='..value_radiateur) cumul_solar = value_solar - counter_solar cumul_injection = value_injection - counter_injection consommation = value_conso - counter_conso cumul_radiateur = value_radiateur - counter_radiateur print('conso='..consommation) print('cumul_solar='..cumul_solar) print('cumul_injection='..cumul_injection) print('cumul_radiateur='..cumul_radiateur) if (cumul_solar > 0) then print("auto consommation="..tostring( round((100 * (cumul_solar - cumul_injection) / cumul_solar),1)) ) commandArray['AUTO_CONSOMMATION'] = otherdevices_idx['AUTO_CONSOMMATION'] ..'|0|' .. tostring( round((100 * (cumul_solar - cumul_injection) / cumul_solar),1) ) updatenum('AUTO_CONSOMMATION', round((100 * (cumul_solar - cumul_injection) / cumul_solar),1) ) end if (consommation > 0) then print("auto production="..tostring(100 * cumul_solar / (cumul_solar + consommation))) updatenum('AUTO_PRODUCTION', round(100 * cumul_solar / (cumul_solar + consommation),1)) end updatenum('COUVERTURE', round(100 * value_solar / (value_conso + value_solar),1)) if (cumul_solar > 0) then updatenum('Consommation_Efficace', round(100 *((cumul_solar - cumul_radiateur) / cumul_solar), 1)) else updatenum('Consommation_Efficace', 0) end end end if (devicechanged['Oregon_Solaire']) then values2 = split(otherdevices['Mesure_Courant'], ";") print("Solaire production 2"..otherdevices['Mesure_Courant']) local tab = getValuesInTab('Oregon_Solaire') print("Solaire "..otherdevices['Oregon_Solaire']) values = split(otherdevices['Oregon_Solaire'], ";") if (tonumber(values[1]) < 0) then if (tonumber(values[1]) == -0.1) then watt = - tonumber(values[2]) else watt = tonumber(values[1]) * 100 - tonumber(values[2]) end else watt = math.max(0, (tonumber(values[1]) * 100 + tonumber(values[2]))) end print("Solaire "..tostring(watt)) watt = tonumber(values2[1]) if (watt > 1000) then watt = 0; end --updatenum('SolaireIntensite', round(watt / 230,3)) --updatenum('INTENSITE_GENERALE', round(watt / 230,3)) watt = math.abs(watt) --- 56 --if (watt < 0 or (heures <= 7 or heures >= 21)) then -- watt = 0 --end --print("Intensite solaire "..devicechanged['SolaireIntensite']) amp = math.abs(watt / 230); --math.max(0, round(otherdevices['SolaireIntensite'],3) - 0.54) *1.18 updatenum('SolaireIntensite', amp) print("# 2 # production amp="..tostring(amp).." watt="..tostring(watt)) updatenum('SolaireProduction', round(watt,1)) watt_solaire = math.abs(watt) local tab = getValuesInTab('CONSOMMATION_GENERALE') local watt_conso = tonumber(tab[1]) local tab_r = getValuesInTab('Consommation_Apparente') local watt_reel = tonumber(tab_r[1]) if (watt_reel > 0) then if watt_solaire > 0 then updatenum('COUVERTURE_INSTANTANEE', round(100 * (watt_solaire / (watt_solaire + watt_reel)), 1)) else updatenum('COUVERTURE_INSTANTANEE', 0) end else updatenum('COUVERTURE_INSTANTANEE', 100) end end if (devicechanged['Oregon_Conso_Reelle']) then local tab = getValuesInTab('Oregon_Conso_Reelle') print("Conso réelle "..otherdevices['Oregon_Conso_Reelle']) values = split(otherdevices['Oregon_Conso_Reelle'], ";") if (tonumber(values[1]) < 0) then if (tonumber(values[1]) == -0.1) then watt = - tonumber(values[2]) else watt = tonumber(values[1]) * 100 - tonumber(values[2]) end else watt = math.max(0, (tonumber(values[1]) * 100 + tonumber(values[2]))) end watt = 20 + watt * 1.10 print("Conso réelle "..tostring(watt)) updatenum('Consommation_Apparente', round(watt,0)) values = split(otherdevices['SolaireProduction'], ";") print("Solaire production "..otherdevices['SolaireProduction']) watt_solaire = values[1] values2 = split(otherdevices['Mesure_Courant'], ";") print("Solaire production 2"..otherdevices['Mesure_Courant']) watt_solaire = values[1] --+ values2[1] print("Watt solaire "..watt_solaire) if (watt < 0) then updatenum('INJECTION', - round(watt,0)) updatenum('TOTAL_INJECTION', - round(watt,0)) --updatenum('Consommation_Apparente', 0) updatenum('TOTAL_AUTOCONSOMMATION', round(watt_solaire - math.abs(watt), 0)) else updatenum('INJECTION', 0) updatenum('TOTAL_INJECTION', 0) updatenum('TOTAL_AUTOCONSOMMATION', round(watt_solaire,0)) end -- ========================================================= --Calculate what the house is consuming -- Get current date & time t1 = os.time() local currentDate = os.date("*t"); -- sets up currentDate.[table] -- (currentDate.year [full], .month [1-12], .day [1-31], .hour [0-23], .min [0-59], .sec [0-59], .wday [0-6 {Sun-Sat}]) sCurrentTime = currentDate.year .. "-" .. currentDate.month .. "-" .. currentDate.day .. " " .. currentDate.hour .. ":" .. currentDate.min .. ":" .. currentDate.sec counter_solar = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['SolaireProduction']..' order by Date desc limit 1;"') counter_solar=tonumber(counter_solar) or 0 print('counter_solar='..counter_solar) value_solar = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['SolaireProduction']..' order by Date desc limit 1;"') value_solar=tonumber(value_solar) or 0 print('value_solar='..value_solar) --------------------------------------------------------------------------------------- -- Solaire 2 date = annee..'-'..mois..'-'..jour_s counter_solar2 = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select counter from Meter_Calendar where DeviceRowID='.. otherdevices_idx['Mesure_Courant'].. ' and date like "' .. date ..'%"'.. ' order by Date desc limit 1;"') counter_solar2=tonumber(counter_solar2) or 0 print('counter_solar2='..counter_solar2) value_solar2 = os.capture('sqlite3 '..'/opt/domoticz/domoticz.db'..' " select Value from Meter where DeviceRowID=='.. otherdevices_idx['Mesure_Courant'].. ' and date like "' .. date ..'%"'.. ' order by Date desc limit 1;"') value_solar2=tonumber(value_solar2) or 0 print('value_solar2='..value_solar2) -------------------------------------------------------------------------------------- watt_p1 = getConsommationHP(1123) or 0 watt_p2 = getConsommationHC(1123) or 0 local cmd = '1135|0|' --USAGE1= energy usage meter tariff 1, This is an incrementing counter --USAGE2= energy usage meter tariff 2, This is an incrementing counter --RETURN1= energy return meter tariff 1, This is an incrementing counter --RETURN2= energy return meter tariff 2, This is an incrementing counter --CONS= actual usage power (Watt) --PROD= actual return power (Watt) .. tostring(math.floor(watt_p1)) .. ';' .. tostring(math.floor(watt_p2)) .. ';' -- + value_solar2 - counter_solar2 .. tostring(math.floor(value_solar - counter_solar + value_solar2 - counter_solar2)) .. ';' .. '0;' .. tostring(math.floor(math.max(0,watt) + watt_solaire)).. ';' .. tostring(math.floor(watt_solaire)) --math.floor(ConsumoEnergyBalance / 1000)) --round(watt,0)) print('Consommation_Reelle_2 '..cmd) if heures == 23 and minutes == 59 and secondes >= 45 then -- ignored else table.insert (commandArray, { ['UpdateDevice'] = cmd } ) end if heures == 00 and minutes == 05 and secondes <= 10 then deleteWrongData(1135) end print('when light=' .. tostring(whenLight())) -- ========================================================= local cmd = '1136|0|' --USAGE1= energy usage meter tariff 1, This is an incrementing counter --USAGE2= energy usage meter tariff 2, This is an incrementing counter --RETURN1= energy return meter tariff 1, This is an incrementing counter --RETURN2= energy return meter tariff 2, This is an incrementing counter --CONS= actual usage power (Watt) --PROD= actual return power (Watt) .. '0;' .. '0;' .. '0;' .. '0;' .. tostring(math.floor(math.max(0,watt))).. ';' .. '0' --math.floor(ConsumoEnergyBalance / 1000)) --round(watt,0)) table.insert (commandArray, { ['UpdateDevice'] = cmd } ) updatenum('Non_Couvert_2', math.floor(math.max(0,watt))) -- ========================================================= local debut_injection = -20 local fin_injection = 10 if tonumber(watt_solaire) < 50 or uservariables["Dark"] == "True" then commandArray['Dimmer'] = 'Set Level: 30' else if (watt < debut_injection and tonumber(watt_solaire) > 150) then print("Allumage radiateur pour éviter l'injection " .. tostring(lastUpdateOfDevice('RadiateurBureau'))) if watt < debut_injection then local quick_value=math.floor(math.abs(watt / (PUISSANCE_DELESTAGE / 100))) print("quick_value plus="..tostring(quick_value)) os.execute('curl "http://192.168.125.11/plus?value=' .. tostring(quick_value).. '"'); else commandArray['Dimmer'] = 'Set Level: 40' end else print("Pas de consommation necessaire") if (watt > 150) then commandArray['Dimmer'] = 'Set Level: 30' else if (watt > fin_injection) then local quick_value=math.floor(math.abs(watt / (PUISSANCE_DELESTAGE / 100))) print("quick_value minus="..tostring(quick_value)) os.execute('curl "http://192.168.125.11/minus?value=' .. tostring(quick_value).. '"'); else commandArray['Dimmer'] = 'Set Level: 50' end end end end command = 'curl -s "http://192.168.125.11"|grep "

"|awk -F">" \'{print $2}\'|awk -F"<" \'{print $1}\'' local handle = io.popen(command) local result = handle:read("*a") handle:close() print("result niveau d'injection= "..result) updatenum('Injection_Radiateur', tonumber(result) * PUISSANCE_DELESTAGE / 100) if (tonumber(result) == 1) then commandArray['Dimmer'] = 'Set Level: 30' end end return commandArray