Lua - Powermonitor with Ping Using Telegram

From Domoticz
Jump to: navigation, search

DRAFT TO BE COMPLETED

Purpose

I live in a village where despite the recent expense to put all the cables in the village underground, our electricty supply comes to the village via overhead lines, which are susceptible to trip due to a number of safety related reasons. Sometimes they reset in a second, other times they need manual intervention so a 10 minute power cut every 3 months is not unknown. Recently, my residual current detectors (RCD) seem to have become rather sensitive and so while I can detect an external power cut by detecting 0 amps on the incoming supply using my Owl energy monitor, this doesn't tell me when one of my RCDs trip.

I have a Raspberry Pi WiFi access point powered from the downstairs mains which is on all the time, so by pinging this device I have a simple way of seeing if the power is off downstairs either due to external power cut or an RCD trip

Dependencies

Raspberry Pi, Raspbian (current 14/9/9), Telegram

Domoticz Setup - switches, variables, version

Domoticz v1.1970, 1 user varaible DownPowerFail - 0 = power on, 1 = power off

Installation instructions

Create the user variable DownPowerFail and set to 0. Create the script in lua scripts directory - change the ip address and change mail_name in the 2 calls to telegram to your telegram name.

=Script (comments to be completed)=
 -- ~/domoticz/scripts/lua/script_time_downpowermonitor.lua
 function timedifference(s)
    year = string.sub(s, 1, 4)
    month = string.sub(s, 6, 7)
    day = string.sub(s, 9, 10)
    hour = string.sub(s, 12, 13)
    minutes = string.sub(s, 15, 16)
    seconds = string.sub(s, 18, 19)
    t1 = os.time()
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    difference = os.difftime (t1, t2)
    return difference
 end
 
 function timeCount(numSec)
    local nSeconds = numSec
    if nSeconds == 0 then
       return  "0seconds"
    else
       local sTime = ""
       local nHours = math.floor(nSeconds/3600)
       local nMins = math.floor(nSeconds/60 - (nHours*60))
       if(nHours > 0) then
          sTime = sTime .. nHours
          if(nHours == 1) then
             sTime = sTime .. "hour "
          else
             sTime = sTime .. "hours "
          end
       end
       if(nMins > 0) then
          sTime = sTime .. nMins
          if(nMins == 1) then
             sTime = sTime .. "minute "
          else
             sTime = sTime .. "minutes "
          end
       end
       local nSecs = math.floor(nSeconds - nHours*3600 - nMins *60)
       if(nSecs > 0) then
          sTime = sTime .. nSecs
          if(nSecs == 1) then
             sTime = sTime .. "second"
          else
             sTime = sTime .. "seconds"
          end
       end
       return sTime
    end
 end
 
 commandArray = {}
 
 print('Checking Downstairs Power')
 DownPowerFail = tonumber(uservariables['DownPowerFail'])
 ping_success=os.execute('ping -c1 192.168.1.8')
 if ping_success then
 --      print('Ping success')
    if(DownPowerFail == 1) then
       interval = timedifference(uservariables_lastupdate['DownPowerFail'])
       print('Downstairs Power Restored after ' .. timeCount(interval))
       os.execute('/home/pi/telegram/scripts/generic/telegram.sh msg telegram_name "Downstairs Power Restored - back in contact with study WiFi after ' .. timeCount(interval) .. '"')
       commandArray['Variable:DownPowerFail'] = '0'
    end
 else
 --      print('Ping fail')
    if(DownPowerFail == 0) then
       commandArray['Variable:DownPowerFail'] = '1'
       print('Downstairs Power Failed')
       os.execute('/home/pi/telegram/scripts/generic/telegram.sh msg telegram_name "Downstairs Power Failed - lost contact with study WiFi"')
    end
 end
 
 return commandArray

Example