Tags: 
User library

AN06 is a pair of Lua scripts that send, once per day, an e-mail report showing how many times was each output switched, and how much energy the respective appliance consumed in terms of kWh and price (€ / $ / CZK ...). Receive a report every day about the electricity consumed in a day and in a week at each output (electrical socket). 

Do you have any questions?

Supported devices: NETIO 4All, PowerPDU 4C, NETIO 4 (without consumption data),

 

The scripts, Script A (Daily report) and Script B (Switch counter), send a daily overview of individual inputs (consumption [Wh / kWh], price of the energy consumed, number of times the output was switched). The e-mail reports contains details for the past 24h, as well as the average for the past 7 days for comparison.

 

Script A (Daily report) periodically sends the e-mail and reads the consumption. Script B (Switch counter) detects and counts the switching events for each output.

 


Sample of AN06 email:

 

Configuring e-mail in NETIO 4x

In the NETIO 4x device, e-mail parameters need to be set in the web configuration.

 

In the Settings – Email section of the web interface, set the parameters as follows:

  • SMTP server
    • Address of the SMTP server of your e-mail provider (such as smtp.google.com, smtp.seznam.cz)
  • Enable SMTP authentication
    • Checked
    • Username - your e-mail address
    • Password - password associated with your e-mail address
    • Enable TLS encryption - checked
  • To
    • E-mail address
  • Use custom sender address
    • Optional
  • Send daily reports about NETIO 4All health
    • Optional (does not affect AN06)
    • PWhen checked, the following report is sent periodically. This report is completely unrelated to the AN06 periodic e-mails.
       

Note: These are the recommended settings. Different e-mail service providers may require different settings. We recommend checking the settings against your provider’s recommended settings.

 

Sample daily report sent by the NETIO 4x device:

Daily report for device myNetio, date 2019-07-21.

 

== SYSTEM LOAD ==

 

    Hour   System load   Incomming traffic

------------------------------------------

   0 - 1             -           -

   1 - 2             -           -

   2 - 3             -           -

   3 - 4             -           -

   4 - 5             -           -

   5 - 6             -           -

   6 - 7             -           -

   7 - 8             -           -

   8 - 9             -           -

  9 - 10             -           -

 10 - 11             -           -

 11 - 12             -           -

 12 - 13             -           -

 13 - 14             -           -

 14 - 15             -           -

 15 - 16             -           -

 16 - 17             -           -

 17 - 18             -           -

 18 - 19             -           -

 19 - 20             -           -

 20 - 21             -           -

 21 - 22             -           -

 22 - 23             -           -

 23 - 24             -           -

 

 

 

== SYSTEM LOGS ==

Errors: 0

Warnings: 0

 

=== System ===

 

 

=== Manager ===

 

 

=== Networking ===

 

 

=== Users ===

 

 

=== Storage ===

 

 

=== Devices ===

 

Creating the rules for scripts A and B

In the Actions section of NETIO 4 web administration, click Create Rule to add a rule

 

Skript A (Daily report)

1) Fill in the following parameters:

  • Enabled: checked
  • Name: Daily report (user-defined)
  • Description: Daily report about NETIO (user-defined)
  • Trigger: System started up
  • Schedule: Always

 

2) Copy the following code and paste it into the field for the Lua script:

 

------------NETIO AN06------------

------------Section 1------------
local electricityPrice = "0.12" -- price for kWh
local currency = "$" -- currency
local emailReport = "my.email@gmail.com" -- your email
---------End of Section 1---------

local emailTime = "01:00:00"

local initialEnergy = {}
local weekStats = {}

for i=1,7 do
  weekStats[i] = {energy={0,0,0,0},switches={0,0,0,0}}
end

function initialize()
  local stringTime = os.date("%X")
  local time = (3600*tonumber(stringTime:sub(1,2)) + 60*tonumber(stringTime:sub(4,5)) + tonumber(stringTime:sub(7,8)))
  
  local emTime = (3600*tonumber(emailTime:sub(1,2)) + 60*tonumber(emailTime:sub(4,5)) + tonumber(emailTime:sub(7,8)))
  
  for i=1,4 do
    initialEnergy[i] = devices.system["output" .. i .. "_cumulatedConsumption"]
  end

  delay(86400-time+emTime,function() dailyReport() end)
end

function dailyReport()
  local day = tonumber(os.date("%w"))
  -- os.date("%w") returns 0 for Sunday
  if day == 0 then day = 7 end
  
  local prevDay = day - 1
  if prevDay == 0 then prevDay = 7 end
  
  -- get data about switching
  if _G.weekSwitches[prevDay].switches == nil then
    weekStats[prevDay].switches = {0,0,0,0}
  else
    weekStats[prevDay].switches = _G.weekSwitches[prevDay].switches
    -- reset counter for previous day
    _G.weekSwitches[prevDay].switches = {0,0,0,0}
  end
  
  
  for i=1,4 do
    if devices.system["output" .. i .. "_cumulatedConsumption"] ~= nil then
      weekStats[prevDay].energy[i] = devices.system["output" .. i .. "_cumulatedConsumption"] - initialEnergy[i]
      initialEnergy[i] = devices.system["output" .. i .. "_cumulatedConsumption"]
    else
      weekStats[prevDay].energy[i] = 0
      initialEnergy[i] = 0
    end
  end

  -- formating email
  local text = "AN06 - Daily report from NETIO device\n"
  text = text .. os.date("%c") .. "\n"
  text = text .. "----------------------------------\n"
  text = text .. "Last 24 hours:\n"
  
  for i=1,4 do
    text = text .. "Output" .. i .. ": "
    text = text .. weekStats[prevDay].energy[i] .. "[Wh], "
    local price = computePrice(weekStats[prevDay].energy[i])
    text = text .. price .. currency .. ", "
    text = text .. weekStats[prevDay].switches[i] .. "x switched\n"
  end
  
  text = text .. "----------------------------------\n"
  text = text .. "Last 7 days:\n"
  
  for i=1,4 do
    text = text .. "Output" .. i .. ": "
    local sumEnergy = 0
    local sumSwitches = 0
    for j=1,7 do
      sumEnergy = sumEnergy + weekStats[j].energy[i]
      sumSwitches = sumSwitches + weekStats[j].switches[i]
    end
    text = text .. sumEnergy .. "[Wh], "
    local price = computePrice(sumEnergy)
    text = text .. price .. currency .. ", "
    text = text .. sumSwitches .. "x switched\n"
  end
    
  logf("Sending daily report, day: %d",day)
  mail(emailReport,"NETIO daily report",text,5)
  delay(86400,function() dailyReport() end)
end

function computePrice(energy)
  local price = string.gsub(electricityPrice,'%.','')
  return tonumber(price)*energy/100000 
end

log("Mail skript started")
-- Measurment variables are not available after power up, some delay is necessary
delay(1,function() initialize() end)

To finish creating the rule, click Create Rule at the bottom of the screen.

            

Skript B (Switch counter)

1) Fill in the following parameters:

  • Enabled: checked
  • Name: Switch counter (user-defined)
  • Description: Switching counter for daily report (user-defined)
  • Trigger: DO state changed
  • Schedule: Always

2) Copy the following code and paste it into the field for the Lua script:

 

 
------------NETIO AN06------------
local day = tonumber(os.date("%w"))
-- os.date("%w") returns 0 for Sunday
if day == 0 then day = 7 end

-- Initializing
if _G.weekSwitches == nil then
  _G.weekSwitches = {}
  for i=1,7 do
    _G.weekSwitches[i] = {switches={0,0,0,0}}
  end
  --Script is triggered once on power up for every output
  _G.weekSwitches[day] = {switches={-1,-1,-1,-1}}
end

-- Incrementing counter
for k in pairs(event.args) do
  _G.weekSwitches[day].switches[tonumber(k:sub(7,7))] = _G.weekSwitches[day].switches[tonumber(k:sub(7,7))] + 1
end

3) To finish creating the rule, click Create Rule at the bottom of the screen.

 

After creating the scripts, both scripts should be shown in the list of scripts.

 

Method of operation

  • Script A (Daily report) periodically sends the e-mail and reads the consumption.
  • Script B (Switch counter) detects each event of switching an output at the device, and counts the events in a global variable.
  • When the device is started, Script A (Daily report) is initialized and the current values of energy consumption counters are read. The script sends the first periodic e-mail at 1:00 am. The e-mail contains a report about the consumption at each power output, the number of switching events, and the price of the energy consumed. In addition to previous day’s data, information concerning the past 7 days is also included
  • The script uses a global variable, _G.weekSwitches, to keep track of the number of switching events. When creating other scripts, make sure to avoid a name conflict with this variable.

 

Setting the variable

  • electricityPrice
    • This variable contains the unit price for one kWh.
    • The variable needs to be entered as a string.
    • Two decimal places are mandatory.
    • The calculated price is rounded down to the nearest integer.
    • For example, to specify 12 cents per kWh: local electricityPrice = "12.00"
  • currency
    • Specifies the currency in which electricityPrice is expressed.
    • For example, to specify ¢: local currency = "¢"
  • email
    • Recipient’s e-mail address to which the reports are sent.
    • It is a string so it has to be enclosed in double quotes.
    • For example, to send the reports to my.email@gmail.com: local email =  "my.email@gmail.com"

 

 

FAQ

1) Is it possible to receive the reports at another time instead of 1 AM?

Yes, simply modify the emailTime variable. Make sure to keep the same format. For example, to specify 3:30pm: local emailTime= "15:30:00".

 

2) Is it possible get the daily reports from a NETIO power socket device without the energy metering function?

Yes. The e-mail will just show zero consumption and zero price.

 

3) What happens with the email if the NETIO device is offline at the specified time?

In this case, the e-mail is not sent. NETIO must be able to connect to the SMTP server at the time of sending the e-mail.

 

4) If the power is interrupted, is the consumption data lost?

Yes. If there is a power outage, all data is reset. The data for the past 7 days are only recorded as from the last restart.
 

5) Can the NETIO device send an e-mail whenever it is started?

Yes. For a ready-made script and configuration instructions, see AN05.

 


 

Supported FW versions:

3.1.0 and higher

 


 

Supported devices:

Ask for a price or technical parameters

For device testing use name/password demo/demo