From edeec3b6fa73815efb40b3daf009f928de83b20e Mon Sep 17 00:00:00 2001 From: CreasolTech Date: Wed, 8 May 2024 21:17:52 +0200 Subject: [PATCH] Added earthquake script --- alarm.lua | 4 +- config_fireAlarm.lua | 4 +- config_heatpump_emmeti.lua | 7 +- config_power.lua | 6 +- globalfunctions.lua | 6 +- power.lua | 42 ++++++++++- script_time_earthquake.lua | 125 ++++++++++++++++++++++++++++++++ script_time_heatpump_emmeti.lua | 55 +++++++++----- script_time_hotwater.lua | 95 ++++++++++++++++++++++++ script_time_nightLights.lua | 10 +-- script_time_rainWindCheck.lua | 10 ++- 11 files changed, 322 insertions(+), 42 deletions(-) create mode 100644 script_time_earthquake.lua create mode 100644 script_time_hotwater.lua diff --git a/alarm.lua b/alarm.lua index ed29444..886e77c 100644 --- a/alarm.lua +++ b/alarm.lua @@ -316,8 +316,8 @@ function lightsNext() end function lightsCheck() -- check that zAlarmLights exists, if not init it and init lights[] dict - timenow=os.date("*t") - secondsnow = timenow.sec + timenow.min*60 + timenow.hour*3600 + timeNow=os.date("*t") + secondsnow = timeNow.sec + timeNow.min*60 + timeNow.hour*3600 json=require("dkjson") if (uservariables['zAlarmLights']==nil) then lightsInit() -- init lights table diff --git a/config_fireAlarm.lua b/config_fireAlarm.lua index 5e99e9b..f1fe4ab 100644 --- a/config_fireAlarm.lua +++ b/config_fireAlarm.lua @@ -6,13 +6,13 @@ ROOMS={ {"Kitchen", "Temp_Cucina", 0.4, 30}, {"Living", "Temp_Soggiorno", 0.4, 30}, {"Office", "Temp_Studio", 0.6, 30}, - {"Laundry", "Temp_Lavanderia", 2, 32}, -- DS1820 sensor behind the washer machine socket + {"Laundry", "Temp_Lavanderia", 3, 32}, -- DS1820 sensor behind the washer machine socket {"Garage", "Temp_Garage", 0.6, 35}, -- {"Wallbox", "Temp_Wallbox", 0.3, 30}, {"Cellar", "TempRH_Cantina", 0.4, 24}, {"Bathroom", "Temp_Bagno", 1, 30}, {"Bedroom", "Temp_Camera", 0.4, 30}, - {"Bedroom2", "TempRH_Camera", 0.6, 30}, + {"Bedroom2", "TempRH_Camera", 2.5, 30}, {"BedroomVale", "Temp_Camera_Valentina",0.4, 30}, {"BedroomGuests", "Temp_Camera_Ospiti", 0.4, 30}, {"IroningRoom", "Temp_Stireria", 1.5, 35}, -- DS1820 sensor behind the power outlet connected to iron machine diff --git a/config_heatpump_emmeti.lua b/config_heatpump_emmeti.lua index 9a08bdc..cd54d99 100644 --- a/config_heatpump_emmeti.lua +++ b/config_heatpump_emmeti.lua @@ -2,8 +2,8 @@ -- Written by Creasol, https://creasol.it - linux@creasol.it -- -dofile "/home/pi/domoticz/scripts/lua/globalvariables.lua" -- some variables common to all scripts -dofile "/home/pi/domoticz/scripts/lua/globalfunctions.lua" -- some functions common to all scripts +dofile "scripts/lua/globalvariables.lua" -- some variables common to all scripts +dofile "scripts/lua/globalfunctions.lua" -- some functions common to all scripts -- Some constants TEMP_HISTERESIS=0.1 @@ -18,6 +18,7 @@ EVSTATE_DEV="EV State" -- EV state, used to know if vehicle is in chargin --GasHeater='GasHeater' -- Activate gas heater instead of heat pump when external temperature very low: set to '' if a boiler does not exist GasHeater='' -- it's not cheaper not greener than PDC => manually enabled only if PDC is not able to keep the temperature +GRID_VOLTAGE='Inverter - AC Voltage' -- Device measuring house voltage: if above 248V, heatpump can consume all available power powerMeter='PowerMeter Grid' -- device name of power meter, that measure consumed power from the electric grid (negative when photovoltaic produced more than house usage) inverterMeter='PV_PowerMeter' -- Inverter output power (photovoltaic). Set to '' if not available inverter2Meter='PV_Garden' -- Inverter for the 2nd renewable energy source. Set to '' if not available @@ -119,7 +120,7 @@ GHtimeMax=480 -- Minutes from midnight when GasHeater will be disabled GHdevicesToEnable={} -- Device to enable when gas heater is ON {'devicename1','devicename2'} DEBUG_LEVEL=E_INFO -DEBUG_LEVEL=E_DEBUG +--DEBUG_LEVEL=E_DEBUG TELEGRAM_LEVEL=E_CRITICAL DEBUG_PREFIX="HeatPump: " diff --git a/config_power.lua b/config_power.lua index f676579..0a1d425 100644 --- a/config_power.lua +++ b/config_power.lua @@ -37,13 +37,17 @@ HOYMILES_ID='solar/116493522530/cmd/limit_nonpersistent_absolute' -- MQTT name t --HOYMILES_ID='' -- MQTT name to set the output power limit using OpenDTU. '' to disable this function HOYMILES_LIMIT_MAX=1600 -- Max power in watt HOYMILES_TARGET_POWER=-6000 -- Target Power: 0 => no export. 50=import always at least 50W. -300=try to export always 300W +HOYMILES_LIMIT_PERC_DEV='PVgarden_Limit' +HOYMILES_PRODUCING_DEV='PVgarden_InverterProducing' +HOYMILES_RESTART_DEV='PVgarden_RestartInverter' -- Output device: use any name of your choice ledsGreen={'Led_Cucina_Green','Living_Led_Green','BagnoPT_LedG'} -- green LEDs that show power production -- ledsRed={'Led_Cucina_Red','Living_Led_Red','BagnoPT_LedR' } -- red LEDs that show power usage ledsRed={'Led_Cucina_Red','BagnoPT_LedR' } -- red LEDs that show power usage -ledsWhite={'Living_Led_White','Light_Night_Led','Led_Camera_White','Buzzer_Camera','Led_Camera_Ospiti_White','Led_Camera_Ospiti_WhiteLow'} -- White LEDs that will be activated in case of blackout. List of devices configured as On/Off switches +ledsWhite={'Living_Led_White','Light_Night_Led','Led_Camera_White','Led_Camera_Ospiti_White','Led_Camera_Ospiti_WhiteLow'} -- White LEDs that will be activated in case of blackout. List of devices configured as On/Off switches ledsWhiteSelector={'Led_Cucina_White','BagnoPT_LedW'} -- White LEDs that will be activated in case of blackout. List of devices configured as Selector switches +blackoutBuzzers={'Buzzer_Camera'} -- Audio alert in case of power outage HPMode='HeatPump_Mode' -- Selector switch for Off, Winter (heating), Summer (cooling) EVLedStatus={''} -- status indicator for the electric car charging (1 flash => more than 1kW, 2 flashes => more than 2kW, ...} diff --git a/globalfunctions.lua b/globalfunctions.lua index 88f8c34..e993505 100644 --- a/globalfunctions.lua +++ b/globalfunctions.lua @@ -91,14 +91,14 @@ end function peakPower() if (monthnow==nil) then monthnow = tonumber(os.date("%m")) end - if (timenow==nil) then timenow = os.date("*t") end + if (timeNow==nil) then timeNow = os.date("*t") end if ((monthnow>=11 or monthnow<=3)) then - if ((timenow.hour>=7 and timenow.hour<10) or (timenow.hour>=17 and timenow.hour<21)) then + if ((timeNow.hour>=7 and timeNow.hour<10) or (timeNow.hour>=17 and timeNow.hour<21)) then -- tonumber(otherdevices['Clouds_today'])<70) return true end else -- from April to October - if ((timenow.hour>=7 and timenow.hour<10) or (timenow.hour>=18 and timenow.hour<22)) then + if ((timeNow.hour>=7 and timeNow.hour<10) or (timeNow.hour>=18 and timeNow.hour<22)) then -- tonumber(otherdevices['Clouds_today'])<70) return true end diff --git a/power.lua b/power.lua index 78ffcd9..28ff095 100644 --- a/power.lua +++ b/power.lua @@ -9,6 +9,10 @@ -- -- At least a device with "Power" in its name has changed: let's go! + +DEBUG_LEVEL=E_WARNING +--DEBUG_LEVEL=E_DEBUG + dofile "scripts/lua/config_power.lua" -- configuration file timeNow=os.date("*t") @@ -23,6 +27,7 @@ function PowerInit() if (Power['ev']==nil) then Power['ev']=0 end -- used to force EV management now, without waiting 1 minute if (Power['EV']==nil) then Power['EV']=0 end -- EV Charge power if (Power['HL']==nil and HOYMILES_ID~='') then Power['HL']=HOYMILES_LIMIT_MAX end -- current limit value + if (Power['HS']==nil and HOYMILES_ID~='') then Power['HS']=0 end -- Inverter producing status (0=Off, 1=On) --if (PowerAux==nil) then PowerAux={} end end @@ -241,15 +246,36 @@ for devName,devValue in pairs(devicechanged) do end end if (HOYMILES_ID~='') then + -- set inverter limit to avoid exporting too much power to the grid (max 6000W in Italy, in case of single phase) local newlimit=Power['HL']+currentPower-HOYMILES_TARGET_POWER + -- log(E_DEBUG, "HOYMILES: Power[HL]="..Power['HL'].." currentPower="..currentPower.." HOYMILES_TARGET_POWER="..HOYMILES_TARGET_POWER.." newlimit="..newlimit) if (newlimit>HOYMILES_LIMIT_MAX) then newlimit=HOYMILES_LIMIT_MAX - elseif (newlimit<0) then - newlimit=0 + elseif (newlimit<100) then + newlimit=100 -- avoid turning off the inverter completely + end + if (tonumber(otherdevices[VOLTAGE_MAINS])>=250) then + log(E_WARNING,"HOYMILES: reduce max limit due to overvoltage") + newlimit=newlimit/2 end + local newlimitPerc=math.floor(newlimit*100/HOYMILES_LIMIT_MAX) if (newlimit~=Power['HL'] or timeNow.min==0 and timeNow.sec>45) then - log(E_WARNING,"HOYMILES: transmit newlimit="..newlimit) + log(E_INFO,"HOYMILES: currentPower="..currentPower.." target="..HOYMILES_TARGET_POWER.." => Transmit newlimit="..newlimit.." "..newlimitPerc.."%") os.execute('/usr/bin/mosquitto_pub -u '..MQTT_OWNER..' -P '..MQTT_PASSWORD..' -t '..HOYMILES_ID..' -m '..newlimit) + Power['HL']=newlimit + commandArray[#commandArray + 1]={['UpdateDevice']=otherdevices_idx[HOYMILES_LIMIT_PERC_DEV].."|0|".. newlimitPerc} + end + -- Now check that inverter is producing + if (otherdevices[HOYMILES_PRODUCING_DEV]=='Off') then + -- inverter not producing + if (Power['HS']==1 and tonumber(otherdevices[VOLTAGE_MAINS])>=240) then + -- inverter not producing due to overvoltage => restart it + log(E_WARNING,"HOYMILES: inverter not producing => restart now") + commandArray[HOYMILES_RESTART_DEV]='On' + end + Power['HS']=0 + else + Power['HS']=1 end end end @@ -397,6 +423,11 @@ for devName,devValue in pairs(devicechanged) do Power['BLS_'..k]='On' -- store in a variable that this led was activated by blackout check end end + for k,buzzer in pairs(blackoutBuzzers) do + if (otherdevices_svalues[buzzer]~=nil) then + commandArray[buzzer]="On for 10" + end + end else -- power restored for k,led in pairs(ledsWhite) do if (otherdevices[led]~=nil and otherdevices[led]~='0ff' and (Power['BL_'..k]==nil or Power['BL_'..k]=='On')) then @@ -410,6 +441,11 @@ for devName,devValue in pairs(devicechanged) do Power['BLS_'..k]=nil end end + for k,buzzer in pairs(blackoutBuzzers) do + if (otherdevices_svalues[buzzer]~=nil) then + commandArray[buzzer]="Off" + end + end end end end diff --git a/script_time_earthquake.lua b/script_time_earthquake.lua new file mode 100644 index 0000000..f4134fd --- /dev/null +++ b/script_time_earthquake.lua @@ -0,0 +1,125 @@ +-- Original script from User mojso: see https://www.domoticz.com/forum/viewtopic.php?t=41380 +-- This scripts gets earthquake info from www.seismicportal.eu for a defined area +-- Then updates dummy alert sensor with Time, Location, Magnitude, Depth, Distance +-- It takes the first entry (= the most recent one) and therefore runs every 5 minutes. +-- 21-03-2024 Version with some modifications by Jan Peppink, https://ict.peppink.nl +-- Make use of alert device in stead of text device. +-- Set alert color based on configurable distance. +-- Added links to source and map also in the device. +-- 2024-05-08: write as lua script by CreasolTech, using globalvariables and globalfunctions +-- Sends notifications by Telegram + +commandArray={} +local timeNow=os.date('*t') +--if ((timeNow.min % 5)~=0) then return commandArray end -- exec script every 5 minutes + +dofile "scripts/lua/globalvariables.lua" +dofile "scripts/lua/globalfunctions.lua" + +DEBUG_LEVEL=E_WARNING +DEBUG_LEVEL=E_DEBUG +DEBUG_PREFIX="EarthQuake: " +EARTHQUAKE_DEV="EarthQuake" -- Create this text device by yourself! +MAXRADIUS=4 -- used to restrict data from the earthquake source +MAXDISTANCE=600 -- Max distance in km +MINMAGNITUDE=2.5 -- Min Richter magnitude +TELEGRAMMAGNITUDE=3 -- Min magnitude to send notification on smartphone +LATITUDE=45.88 -- Your latitude +LONGITUDE=12.18 -- Your longitude + + +--globalvariables +-- Set to your environment and preference +local mailto = 'yourname@example.com' -- Set E-mail adres to sent to. +local alertIdx = n -- Set to the idx of the Virtual Alert sensor you have to create for this script + +if (otherdevices[EARTHQUAKE_DEV]==nil) then + log(E_ERROR,"Please create a text device named "..EARTHQUAKE_DEV) + return commandArray +end +local lastalertText = otherdevices[EARTHQUAKE_DEV] -- Holds string of the previous round. +local alertText = '' + +--Adjust these variables to get information about the place you want +local qMinmag = 2 +local lTimediff = 3600 -- your local time 3600 equal +1 UTC time + +-- Define distance for ALERTLEVEL colors +-- From dClose to radiusq ALERTLEVEL_GREY +local dClose = 750 -- From distance dCloser to dClose ALERTLEVEL_YELLOW +local dCloser = 500 -- From distance dClosest to dCloser ALERTLEVEL_ORANGE +local dClosest = 250 -- From distance 0 to closest ALERTLEVEL_RED + +-- Local Functions go here ============= +function titleCase( first, rest ) + return first:upper()..rest:lower() +end + +-- Calculate distance using Haversine formula +local function calculateDistance(lat1, lon1, lat2, lon2) + local R = 6371 -- Earth radius in kilometers + local dLat = math.rad(lat2 - lat1) + local dLon = math.rad(lon2 - lon1) + local a = math.sin(dLat / 2) * math.sin(dLat / 2) + math.cos(math.rad(lat1)) * math.cos(math.rad(lat2)) * math.sin(dLon / 2) * math.sin(dLon / 2) + local c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) + local distance = R * c + return distance +end + +-- Now start to do something ============ +local fd=io.popen('curl -s "https://www.seismicportal.eu/fdsnws/event/1/query?limit=10&lat='..LATITUDE..'&lon='..LONGITUDE..'&minradius=0&maxradius='..MAXRADIUS..'&format=json&minmag='..MINMAGNITUDE..'"') +local response=assert(fd:read('*a')) +-- response = json data with list of earthquakes +json=require("dkjson") +local q=json.decode(response) +local qMag = tonumber(q.features[1].properties.mag) +local qRegion = tostring(q.features[1].properties.flynn_region) +local qTimeString = tostring(q.features[1].properties.time) +local qLat = tonumber(q.features[1].properties.lat) +local qLon = tonumber(q.features[1].properties.lon) +local qDepth = tonumber(q.features[1].properties.depth) + +--local t = string.sub(cuando, 1,10) +local t = os.time{year=tonumber(qTimeString:sub(1,4)), +month=tonumber(qTimeString:sub(6,7)), +day=tonumber(qTimeString:sub(9,10)), +hour=tonumber(qTimeString:sub(12,13)), +min=tonumber(qTimeString:sub(15,16)), +sec=tonumber(qTimeString:sub(18,19))} +-- local atLocalTime = os.date('%H:%M %a %d %B %Y', t + lTimediff) +-- local atUTCtime = os.date('%H:%M %a %d %B %Y', t) +local atLocalTime = os.date('%d-%m-%Y %H:%M ', t + lTimediff) +local atUTCtime = os.date('%d-%m-%Y %H:%M ', t) +-- %d-%m-%Y %H:%M +qRegion = string.gsub(qRegion, "(%a)([%w_']*)", titleCase) + +local distance = calculateDistance(LATITUDE, LONGITUDE, qLat, qLon) +-- Round the distance to the nearest kilometer +local roundedDistance = math.floor(distance + 0.5) + +--Set and format the new alertText +local alertText = tostring( atLocalTime .. ' ' .. qRegion .. '\n' .. 'Mag: ' .. qMag .. '. Depth:' .. qDepth .. 'km Distance: ' .. roundedDistance .."km.\n"..'Location: Map') + +--[[ +--Set and format the new mail message +local message = tostring('Location: ' .. qRegion .. '
' .. +'Magnitude: ' .. qMag .. '
' .. +'Depth: ' .. qDepth .. 'km
' .. +'UTC Time: ' .. atUTCtime .. '
' .. +'Locale Time: ' .. atLocalTime .. '
' .. +'Distance: ' .. roundedDistance .. 'km.
'.. +'Coordinates: ' .. qLat .. ','.. qLon .. '
' .. +'Location' .. '
' .. +'Source' .. '
') +]] + +-- Only update and sent message when info has changed. and +if (alertText ~= lastalertText and roundedDistance <= MAXDISTANCE) then + commandArray['UpdateDevice']=otherdevices_idx[EARTHQUAKE_DEV]..'|'.. math.floor(qMag-1) ..'|'..alertText + local priority=E_INFO + if (qMag>=TELEGRAMMAGNITUDE) then + priority=E_CRITICAL + end + log(priority,"Mag="..qMag.." Dist="..roundedDistance.."km "..qRegion.."\nhttps://maps.google.com/?q=" .. qLat .. ',' .. qLon ) +end +return commandArray diff --git a/script_time_heatpump_emmeti.lua b/script_time_heatpump_emmeti.lua index 1faf09d..e50c99c 100644 --- a/script_time_heatpump_emmeti.lua +++ b/script_time_heatpump_emmeti.lua @@ -20,7 +20,7 @@ DEBUG_LEVEL=E_WARNING DEBUG_PREFIX="Power: " commandArray={} -dofile "/home/pi/domoticz/scripts/lua/config_heatpump_emmeti.lua" +dofile "scripts/lua/config_heatpump_emmeti.lua" -- Initialize the HP domoticz variable (json coded, within several state variables) function HPinit() @@ -83,8 +83,8 @@ function setOutletTemp(tempMin) end monthnow = tonumber(os.date("%m")) -timenow = os.date("*t") -minutesnow = timenow.min + timenow.hour * 60 +timeNow = os.date("*t") +minutesnow = timeNow.min + timeNow.hour * 60 -- check variables json=require("dkjson") @@ -120,7 +120,7 @@ else HPZinit() -- check that all variables in HP table are initialized end -if (timenow.min==1 or timenow.min==31) then +if (timeNow.min==1 or timeNow.min==31) then -- shift temperatures in HPZ['tn'] and compute new tempDerivate HPZ['t4']=HPZ['t3'] HPZ['t3']=HPZ['t2'] @@ -207,6 +207,9 @@ else end avgPower=math.floor(avgPower) prodPower=0-avgPower +if (GRID_VOLTAGE~='') then + gridVoltage=tonumber(otherdevices[GRID_VOLTAGE]) +end if (heatpumpMeter~='' and otherdevices[heatpumpMeter]~=nil) then -- heat pump power meter exists, returning value "usagePower;totalEnergy" @@ -265,6 +268,10 @@ if (EVSTATE_DEV~='') then if (HP['EV']<2) then HP['EV']=HP['EV']+1 end + -- Also, if EVUPDATE_DEV lastupdate is older than 24minutes, issue a EVUPDATE to refresh EV information + if (EVUPDATE_DEV~=nil and otherdevices_lastupdate[EVUPDATE_DEV]~=nil and timedifference(otherdevices_lastupdate[EVUPDATE_DEV])>1440) then + commandArray[EVUPDATE_DEV]='On' + end else -- not charging if (HP['EV']>0) then HP['EV']=HP['EV']-1 @@ -293,20 +300,20 @@ if (HPlevel~="Off") then -- diffMaxTh=0.1 -- if diffMax=20) then + if (timeNow.hour<3 or timeNow.hour>=20) then -- in the morning, or in the night, no problem if the temperature is far from setpoint diffMaxTh=diffMaxTh+0.1 log(E_INFO,"Morning or Night: diffMaxTh increased to "..diffMaxTh) end - if (timenow.yday>=41 and timenow.yday<320 and timenow.hour<9) then + if (timeNow.yday>=41 and timeNow.yday<320 and timeNow.hour<9) then if (tonumber(otherdevices['Clouds_today'])<=60) then -- during night, after 10 Feb with sunny weather => do not start heatpump if possible diffMaxTh=diffMaxTh+0.2 log(E_INFO,"Night, from 10 Feb to 15 Nov, and Sunny => increase diffMaxTh to "..diffMaxTh) end - if (timenow.yday>=71 and timenow.yday<305) then + if (timeNow.yday>=71 and timeNow.yday<305) then -- during night, between 10 Mar and 1 Nov => do not start heatpump in the night - diffMaxTh=diffMaxTh+0.3 + diffMaxTh=diffMaxTh+0.5 log(E_INFO,"Night, from 10 Mar to 1 Nov => increase diffMaxTh to "..diffMaxTh) end end @@ -317,6 +324,7 @@ if (HPlevel~="Off") then overlimitDiff=0.2 -- forced diffmax value prodPowerOn=300 -- minimum extra power to turn ON the heatpump gridPowerMin=300 -- minimum power from the grid, even when PV is producing + if (timeNow.yday>=41 and timeNow.yday<320) then gridPowerMin=0 end -- don't use power from grid in Spring and Autumn! TargetPowerMin=math.floor(510+(890/14)*(7-outdoorTemperature)) -- computed based on heat pump datasheet TargetPowerMax=3000 else @@ -354,7 +362,7 @@ if (HPlevel~="Off") then -- -- check temperature offset defined for each zone (used to reduce temperature during the night temperatureOffset=0 - if (timenow.hour < v[zone_start] or timenow.hour >= v[zone_stop]) then + if (timeNow.hour < v[zone_start] or timeNow.hour >= v[zone_stop]) then -- night: reduce the temperature setpoint temperatureOffset=v[zone_offset] end @@ -456,12 +464,15 @@ if (HPlevel~="Off") then log(E_INFO,"Use only power from secondary PV") else log(E_INFO,"Reduce diffMax to try exporting energy in the peak hours") - if ((timenow.month>=11 or timenow.month<3)) then + if ((timeNow.month>=11 or timeNow.month<3)) then diffMax=diffMax-0.2 -- in Winter else diffMax=diffMax-0.3 -- in Summer end end + elseif (timeNow.yday>=71 and timeNow.yday<305 and timeNow.hour<9) then + diffMax=diffMax-0.3 + log(E_INFO,"Night, from 10 Mar to 1 Nov => reduce diffMax to "..diffMax) else -- during the day, not in peak hours prodPower=prodPower+gridPowerMin -- makes the heat pump using at least gridPowerMin Watt from the grid, even while overheating @@ -531,7 +542,7 @@ if (HPlevel~="Off") then targetPower=math.floor(((12-HP['otmin'])^1.5)*22 + ((24-HP['otmax'])^1.6)*4) log(E_INFO,"targetPower="..targetPower.." computed based on otmin and otmax") if (diffMax>diffMaxTh+0.1) then - if (timenow.hour>=10 and timenow.hour<17) then + if (timeNow.hour>=10 and timeNow.hour<17) then -- increase power to recover the comfort state targetPower=math.floor(targetPower+(diffMax-diffMaxTh-0.1)*1000) log(E_INFO,"targetPower="..targetPower.." increased due to diffMax>diffMaxTh+0.1 (daylight)") @@ -543,7 +554,7 @@ if (HPlevel~="Off") then end -- if sunny, in the morning, reduce target power (then increase if photovoltaic produce more than house usage) - if (tonumber(otherdevices['Clouds_today'])<=50 and diffMax=10 and timenow.hour<17) then + if (timeNow.hour>=10 and timeNow.hour<17) then log(E_INFO,"targetPower+=300 between 10 and 17") targetPower=targetPower+300 - if (timenow.hour>=12) then + if (timeNow.hour>=12) then targetPower=targetPower+math.floor(diffMax*diffMax*1000) -- Adjust targetPower based on diffMax value log(E_INFO,"targetPower="..targetPower.." computed based on diffMax² after 12:00") end - elseif (timenow.hour<8 or timenow.hour>=20) then -- during the night reduce power if diffMax near zero + elseif (timeNow.hour<8 or timeNow.hour>=20) then -- during the night reduce power if diffMax near zero if (diffMax=0) then --rooms almost in temperature, in the night targetPower=TargetPowerMin @@ -577,10 +588,14 @@ if (HPlevel~="Off") then if (targetPower=75 and timenow.yday<310) then - if (targetPower>1500) then - targetPower=1500 - log(E_INFO,"targetPower="..targetPower.." limited to 1500 in Autumn and Spring") + if (timeNow.yday>=75 and timeNow.yday<310) then + if (targetPower>1800) then + if (gridVoltage<245) then + targetPower=1800 + log(E_INFO,"targetPower="..targetPower.." limited to 1800 in Autumn and Spring") + else + log(E_INFO,"targetPower="..targetPower.." NOT limited due to high grid voltage="..gridVoltage) + end end end end @@ -643,7 +658,7 @@ if (HPlevel~="Off") then HP['Level']=LEVEL_ON else -- Day -- diffMax>0, some power available (from grid or solar) => should I turn ON heating/cooling? - if (diffMax>=diffMaxTh or (timenow.hour>=9 and prodPower>targetPower)) then + if (diffMax>=diffMaxTh or (timeNow.hour>=9 and prodPower>targetPower)) then HP['Level']=LEVEL_ON end end diff --git a/script_time_hotwater.lua b/script_time_hotwater.lua new file mode 100644 index 0000000..7d3ccb6 --- /dev/null +++ b/script_time_hotwater.lua @@ -0,0 +1,95 @@ +-- script_time_hotwater.lua for Domoticz +-- Author: CreasolTech https://www.creasol.it + +-- This LUA script manage the Hot Water Heat Pump (controlled by Modbus) to optimize own consumption from photovoltaic + +dofile "scripts/lua/globalvariables.lua" -- some variables common to all scripts +dofile "scripts/lua/globalfunctions.lua" -- some functions common to all scripts + +DEBUG_LEVEL=E_INFO +DEBUG_LEVEL=E_DEBUG -- remove "--" at the begin of line, to enable debugging +DEBUG_PREFIX="HotWater: " + +commandArray={} + +log(E_DEBUG,"====================== "..DEBUG_PREFIX.." ============================") +dofile "scripts/lua/config_hotwater.lua" + + +-- in peak hours => OFF +-- between 11:00 and Sunset: +-- between 11:00 and 13:00 ON only if enough power is available from photovoltaic +-- after 13:00, ON + +setPoint=tonumber(otherdevices[HW_SETPOINT]) +setPointNew=setPoint +if (HW_MODE=='' or otherdevices[HW_MODE]==nil) then + log(E_ERROR, "Please create a selector switch with Off, On, Auto states") + return commandArray +end +mode=otherdevices[HW_MODE] +if (mode=='Off') then + if (setPoint>HW_SP_OFF) then + log(E_INFO,"Hot Water switched Off") + setPointNew=HW_SP_OFF + end +elseif (mode=='On') then + if (setPointHW_SP_OFF) then + setPointNew=HW_SP_OFF + end + log(E_DEBUG, "Peak => setPoint="..setPointNew) + else + if (timeofday["Nighttime"]) then + if (setPoint>HW_SP_OFF) then + setPoint=HW_SP_OFF + end + log(E_DEBUG, "Night time => setPoint="..setPointNew) + else + -- during the day + if (timeNow.hour>=13 or (timeNow.hour>=11 and timeNow.wday>=2 and timeNow.wday<=4)) then + -- after 13:00, or from Mon to Wed after 11:00 + gridPower=getPowerValue(otherdevices[GRID_POWER]) + hwPower=getPowerValue(otherdevices[HW_POWER]) + tempWaterBot=tonumber(otherdevices[HW_TEMPWATER_BOTTOM]) + log(E_DEBUG, "gridPower="..gridPower.." hwPower="..hwPower.." tempWaterBot="..tempWaterBot) + if (otherdevices[EVSEON_DEV]=='Off' and (gridPower<-500 or (hwPower>100 and gridPower<100))) then + -- available power to start heat pump, or heat pump already running with enough power available + setPointNew=HW_SP_NORMAL + if (timeNow.hour>=12 and otherdevices[EVSTATE_DEV]~='Ch') then + setPointNew=HW_SP_MAX + end + if (setPointNew>setPoint and hwPower<100) then + -- hot water heat pump is OFF + -- if HP_TEMPWATER_BOT + 15 > setPointNew => increase setPointNew to force start + if (tempWaterBot=(setPointNew-15)) then + setPointNew=tempWaterBot+15+1 + end + end + log(E_DEBUG, "Available power and EVSE is Off => setPoint="..setPointNew) + else + if (timeNow.hour<13) then + setPointNew=HW_SP_MIN + else + setPointNew=HW_SP_NORMAL + --setPointNew=HW_SP_MIN + end + log(E_DEBUG, "Power not available or EVSE is On => setPoint="..setPointNew) + end + end + end + end +end + +if (setPointNew ~= setPoint) then + commandArray[1]={['UpdateDevice']=tostring(otherdevices_idx[HW_SETPOINT])..'|1|'.. setPointNew} +end +return commandArray + + diff --git a/script_time_nightLights.lua b/script_time_nightLights.lua index d83d488..8464e0a 100644 --- a/script_time_nightLights.lua +++ b/script_time_nightLights.lua @@ -1,6 +1,6 @@ -dofile("/home/pi/domoticz/scripts/lua/globalvariables.lua") -- some variables common to all scripts -dofile("/home/pi/domoticz/scripts/lua/globalfunctions.lua") -- some functions common to all scripts +dofile("scripts/lua/globalvariables.lua") -- some variables common to all scripts +dofile("scripts/lua/globalfunctions.lua") -- some functions common to all scripts LightOnDeviceNames={'LightOut_Portico','LightOut_NordOvest','LightOut_Portico_Sud','LightOut_Est','LightOut_Nord'} -- device names of lights that should be turned on after sunset LightOffDeviceNames={'LightOut_Terrazzo','LightOut_NordOvest','LightOut1','LightOut_Portico','LightOut_Portico_Sud','LightOut_Est','LightOut2','LightOut3','LightOut_Nord'} -- device names of lights that should be turned off at sunrise @@ -22,8 +22,8 @@ function setMinutesOn() commandArray['Variable:vNightLightsOn'] = tostring(newvalue) end -timenow = os.date("*t") -minutesnow = timenow.min + timenow.hour * 60 +timeNow = os.date("*t") +minutesnow = timeNow.min + timeNow.hour * 60 if (uservariables['vNightLightsOn'] == nil) then telegramNotify('Error: variable vNightLightsOn not defined') setMinutesOn() @@ -64,7 +64,7 @@ elseif (minutesnow == minutesOff) then end ----------------------------- custom rules ------------------------ -- turn on the light outside bedroom when alarm is activated during the night -if (otherdevices['LightOut_Terrazzo']=='Off' and uservariables['alarmLevel']>1 and (timenow.hour>=23 or timenow.hour<4)) then +if (otherdevices['LightOut_Terrazzo']=='Off' and uservariables['alarmLevel']>1 and (timeNow.hour>=23 or timeNow.hour<4)) then commandArray['LightOut_Terrazzo']='On' end diff --git a/script_time_rainWindCheck.lua b/script_time_rainWindCheck.lua index 019daee..547cf70 100644 --- a/script_time_rainWindCheck.lua +++ b/script_time_rainWindCheck.lua @@ -48,8 +48,8 @@ MOSQUITTO_STOP2=120 -- stop at 120=02:00 TRASH_ALERT_TIME=1140 -- time when alert will be sent, in minutes: 19*60=1140 TRASH_DEV="Living_Buzzer" -- Buzzer or Led device to turn ON -dofile "/home/pi/domoticz/scripts/lua/globalvariables.lua" -- some variables common to all scripts -dofile "/home/pi/domoticz/scripts/lua/globalfunctions.lua" -- some functions common to all scripts +dofile "scripts/lua/globalvariables.lua" -- some variables common to all scripts +dofile "scripts/lua/globalfunctions.lua" -- some functions common to all scripts function RWCinit() -- check or initialize the RWC table of variables, that will be saved, coded in JSON, into the zRainWindCheck Domoticz variable @@ -341,7 +341,11 @@ VEHICLE_DISTANCE='Kia - eNiro distance' -- Vehicle distance in Km VEHICLE_ENGINE='Kia - eNiro engine ON' -- Vehicle engine On/Off VEHICLE_UPDATEREQ='Kia - eNiro update req.' -- Command to force vehicle update GATE_SUPPLY='Power_Apricancello' -- Gate power supply On/Off -local carDistance=tonumber(otherdevices[VEHICLE_DISTANCE]) -- actual vehicle distance from house + +local carDistance=0 -- actual vehicle distance from house +if (VEHICLE_DISTANCE~='' and otherdevices[VEHICLE_DISTANCE]~=nil) then + carDistance=tonumber(otherdevices[VEHICLE_DISTANCE]) -- actual vehicle distance from house +end if (timeNow.wday>=2 and timeNow.wday<=4 and (minutesNow==820 or minutesNow==1030 or minutesNow==1090) and carDistance>5) then log(E_DEBUG,"GeoFence: Request vehicle update") -- working days, time to come back home => force vehicle tracking