This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Scripting / Resident / MODBUS RS-485 energy meter clock update
#1
Hi,


I am using the below script to update the clock of my energy meters connected to RS-485 bus (I have 3 units) and each time I want to update the clock, I have to manually trigger the script. 

   

I have enclosed a capture where these scripts are located:

   

(the below code is for my first power meter):

Code:
require('luamodbus')
mb = luamodbus.rtu()
mb:open('/dev/RS485-1', 19200, 'E', 8, 1, 'H')
mb:connect()
mb:setslave(1) -- Enter the correct Modbus slave address of the kWh meter

-- Function to get value as bits from registers
function toBits(num,bits)
    -- returns a table of bits, most significant first.
    bits = bits or select(2,math.frexp(num))
    local t={} -- will contain the bits        
    for b=bits,1,-1 do
        t[b]=math.fmod(num,2)
        num=(num-t[b])/2
    end
    return table.concat(t)
end

-- Get current data as table
now = os.date('*t')

-- Set date and time to command interface
datanumber = 1003
reservedalways = ''
mbyear = now.year     -- 2000–2099 Year
mbmonth = now.month   -- 1–12 Month
mbday = now.day       -- 1–31 Day
mbhour = now.hour     -- 23 Hour
mbminute = now.min    -- 0–59 Minute
mbsecond = now.sec    -- 0–59 Second
reserved_1 = ''       -- (Reserved)

-- Write command to the command interface
mb:writeregisters(5249, datanumber, reservedalways, mbyear, mbmonth, mbday, mbhour, mbminute, mbsecond, reserved_1) --max 123 parameters entered by , , , ,
--Register 5250 = Requested command, 5251 = Not used, 5252 = parameter 1

-- Get results of command interface action to set date and time
r1, r2 = mb:readregisters(5374, 2)
log ("Result Command: " .. r1 .. ": Set date and time")
if r2 == 0 then
  log ("Result = valid operation, success") 
elseif r2 == 3000 then
  log ("Result = invalid operation, failed")
elseif r2 == 3001 then
  log ("Result = invalid parameter(s), failed")
elseif r2 == 3002 then
  log ("Result = invalid number of parameters, failed")
elseif r2 == 3002 then
  log ("Result = invalid number of parameters, failed")
elseif r2 == 3007 then
  log ("Result = operation not perfomed, failed")
end
log ("Result Code: " .. r2) -- 0 = valid operation, 3000 = invalid Operation, 3001 = Invalid Parameter, 3002 = Invalid number of parameters, 3007 = Operation not perfomed

-- Read result from meter:

-- Get year
year = mb:readregisters(1844, 1)

-- Get Month, Weekday, Day
month_weekday_day = mb:readregisters(1845, 1)
month_weekday_day_bits = toBits(month_weekday_day,16)
month = tonumber (string.sub(month_weekday_day_bits, 5, 8), 2)
wday = tonumber (string.sub(month_weekday_day_bits, 9, 11), 2)
day = tonumber (string.sub(month_weekday_day_bits, -5), 2)

-- Get Hour, Minute
hour_minute = mb:readregisters(1846, 1)
hour_minute_bits = toBits(hour_minute,16)
hour = tonumber (string.sub(hour_minute_bits, 4, 8), 2)
minute = tonumber (string.sub(hour_minute_bits, -6), 2)

-- Get Seconds (in milliseconds)
second = mb:readregisters(1847, 1) / 1000

-- Set Human readable weekdays
wdaytable = {
  'sunday',
  'monday',
  'tuesday',
  'wednesday',
  'thursday',
  'vriday',
  'saterday',
}

-- Log result as human readable string
log('current date in meter 1 is: ' .. wdaytable[wday] .. ' ' .. day .. '-' .. month .. '-20' .. string.format("%02d", year) .. ' ' .. string.format("%02d", hour) .. ':' .. string.format("%02d", minute) .. ':' .. string.format("%02d", second)) 

mb:close()

-- Self disable of script
script.disable(_SCRIPTNAME)


Can you please guide me regarding how I can set this script to automatically update the clock of my power meters, let's say, once per day? The script goes offline after each triggering and I cannot set it to be active (I might have added it on a wrong place and it should not be in Resident scripts).


Thank you,
Andreas
Reply
#2
Use scheduled script and remove the last line that disables the script itself.
Reply
#3
(31.03.2019, 12:44)admin Wrote: Use scheduled script and remove the last line that disables the script itself.

I'll fix it now.

Regarding the below part of the code, I see some typos:

Code:
-- Set Human readable weekdays
wdaytable = {
  'sunday',
  'monday',
  'tuesday',
  'wednesday',
  'thursday',
  'vriday',
  'saterday',
}

Is it correlated to something that part? I would rename two days into friday and saturday.
Reply
#4
This I'd only for debug logs, it does not affect script operation.
Reply


Forum Jump: