LUA, send email when device has not report



  • I had lots of problem for my nodes that they sometimes didn't send data and I could never know when, I wanted to know when it happen just to know if I did something with Domoticz or something else I did.
    This script will read when the Device sent data last time and if it is more than 30 minutes Domoticz will send me an email, when the Device has sent within 30 minutes it send a new email. I now have 100% control when it is not working.
    I also add a function to write to a file, so I could see if it stopped working on same time of day or it was something else.
    I have written the file content to be possible to view in a PHP file, to see the OK and NOT OK easier

    PHP file looks like this

    <html>
    <head>
    <title>Title</title>
    <body bgcolor="white">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" / >
    </head>
    
    <?php
        $myfilename = "c:\progra~2\domoticz\sensor.txt";
        if(file_exists($myfilename)){
          echo file_get_contents($myfilename);
        }
    ?>
    </html>
    

    picture how it looks in PHP
    0_1466712672460_sensor.png

    Translation first row
    YYYY-MM-DD HH:MM:SS No data for 40 minutes from "device", last data was YYYY-MM-DD HH:MM:SS
    Translation seconds row
    Data from "device" is OK again, last data YYYY-MM-DD HH:MM:SS

    In Domoticz create a new Event, LUA and Device as trigger
    You also need to create a Variable(integer) with the same name as the Device, value=0
    Name your device a unique name, like "flopp" :) when it works you can rename it
    LUA code

    commandArray = {}
    local function update(cmd)
    vari = "Variable:"  .. cmd --add Variable: before device
    t1 = os.time() --get date/time right now in seconds
    t3  = os.date("%Y-%m-%d %H:%M:%S") --get date/time right now in normal format
    s = otherdevices_lastupdate[cmd] --read last date/time for device
    --print(cmd)
    --print(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)
    
    t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
    difference = (os.difftime (t1, t2)) --compare right now with device
    
    if (difference > 2400) and (uservariables[cmd] == 0) then --if device date/time is more than 30 minutes
        file = io.open("sensor.txt", "a") -- Opens a file named sensor.txt(stored under Domoticz folder) in append mode
        commandArray['SendEmail']=''..cmd..' äldre än 40 min#'..s..' är senaste tid#abc@hotmail.com' --send mail
        commandArray[vari] = "1" --set variable to 1
        --write to opened file
        file:write(t3 .. " Ingen data på 40 min från ")
        file:write("<font color=red>")
        file:write(cmd .."</font>")
        file:write(", senaste data " .. s .."<br>","\n")
        file:close() --closes the open file
        
    elseif (difference < 2400) and (uservariables[cmd] == 1) then --if device date/time is less than 30 minutes
        file = io.open("sensor.txt", "a")
        commandArray[vari] = "0"
        commandArray['SendEmail']=''..cmd..' ok igen#'..s..' är senaste tid#abc@hotmail.com'
        file:write(t3 .. " Data från ")
        file:write("<font color=blue>")
        file:write(cmd .."</font>")
        file:write(" är OK igen, senaste data " .. s .. "<br>","\n")
        file:close()
    end 
    
    end
    
    update ('Ute_V') --device name can be temperature or voltage
    update ('AppleTV')
    update ('PS3')
    update ('Stereo')
    update ('Captest')
    update ('Captest_V')
    
    return commandArray
    

Log in to reply
 

Looks like your connection to MySensors Forum was lost, please wait while we try to reconnect.