P1 smart meter log sending a day

Moderator: leecollings

Post Reply
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

P1 smart meter log sending a day

Post by kozzen »

How can i get a log detail every day from the day before automaticly on mail in a file ?

(so from 00.00 to 23:59 and 59 seconds ( so from the hole day) off the power (usage 1/2 and return 1/2) and the gas )
User avatar
waltervl
Posts: 5855
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: P1 smart meter log sending a day

Post by waltervl »

As a file needs some extra work but the log as text in an email be similar as example DzVents script in viewtopic.php?t=27982

Why would you need something like this? You can make reports from the report function of Domoticz.
If you want to use these files to make your own database you could also use the DataPush options like https://www.domoticz.com/wiki/HttpLink
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Wednesday 03 March 2021 20:13 How can i get a log detail every day from the day before automaticly on mail in a file ?
(so from 00.00 to 23:59 and 59 seconds ( so from the hole day) off the power (usage 1/2 and return 1/2) and the gas )
below dzVents script will send yesterdays log detail of the P1 energy meter as attached file in JSON format.

__________________________________________________________________________________________________________________________
When not yet familiar with dzVents please start with reading Get started Before implementing (~ 5 minutes). Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents enabled' is checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
___________________________________________________________________________________________________________________________

Code: Select all

--[[

        Script to send history data as mail attachment

        requires:
            dzVents >= 3.1.0 (domoticz V2020.2 build >= 12771 )
            installed mail on os level (linux only)

        History:
        20210303 initial release

]]--

local scriptVar = 'mailHistory'

return
{
    on =
    {
        timer =
        {
          'at 00:02', -- shortly after midnight to ensure shortlog contains 24 hours of yesterdays data.
        },

        shellCommandResponses =
        {
            scriptVar,
        },
        httpResponses =
        {
            scriptVar,
        },
    },

    data =
    {
        'tempFile',
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all ok
        marker = scriptVar,
    },

    execute = function(dz, item)

        --- Your settings below this line

        local powerIDX = 35  -- change to idx of your P1 energy device
        local emailTo = '[email protected]'
        local subject = 'power log'

        --- No changes required below this line

        local function getDeviceLog(idx)
            dz.openURL(
            {
                url = dz.settings['Domoticz url'] .. '/json.htm?type=graph&sensor=counter&range=day&idx=' .. powerIDX,
                callback = scriptVar,
            })
        end

        local function selectRecords(t)
            local yesterday = os.date("%Y-%m-%d",os.time()-24*60*60)
            local res = {}

            for index, record in ipairs(t) do
                if record.d:find(yesterday, 1, true) then
                    table.insert(res, record)
                end
            end
            return dz.utils.toJSON(res)
        end

        local function makeFile(t)
            dz.data.tempFile = os.tmpname()
            file = io.open(dz.data.tempFile, 'w')
            file:write( selectRecords(t))
            file:close()
        end

        local function mailFile(f)
            dz.executeShellCommand(
            {
                command = 'mail -A ' .. f .. ' -s "' .. subject .. '" '  .. emailTo .. ' < /dev/null 2> /dev/null ',
                callback = scriptVar,
                timeout = 20,
            })
        end

        -- main
        if item.isTimer then
            getDeviceLog(powerIDX)
        elseif item.isHTTPResponse then
            if item.json then
                makeFile(item.json.result)
                mailFile(dz.data.tempFile)
            else
                dz.log('Problem retrieving data ' , dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
            end
        elseif item.isShellCommandResponse then
            if not(item.ok) then
                dz.log('Problem with shellCommand ' , dz.LOG_ERROR)
                dz.log(item, dz.LOG_DEBUG)
            else
                os.execute('rm ' .. dz.data.tempFile)
            end
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: P1 smart meter log sending a day

Post by kozzen »

waaren wrote: Thursday 04 March 2021 1:19
local function mailFile(f)
dz.executeShellCommand(
{
command = 'mail -A ' .. f .. ' -s "' .. subject .. '" ' .. emailTo .. ' < /dev/null 2> /dev/null ',
callback = scriptVar,
timeout = 20,
})
end
i'm getting the folowing error

2021-03-04 20:43:00.683 Error: dzVents: Error: (3.0.2) mailHistory: An error occurred when calling event handler Script #1
2021-03-04 20:43:00.683 Error: dzVents: Error: (3.0.2) mailHistory: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:84: attempt to call a nil value (field 'executeShellCommand')

emailto is changed into my email adresses
POWERidx is changed to 1
Last edited by kozzen on Thursday 04 March 2021 20:53, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Thursday 04 March 2021 20:51 i'm getting the folowing error

2021-03-04 20:43:00.683 Error: dzVents: Error: (3.0.2) mailHistory: An error occurred when calling event handler Script #1
2021-03-04 20:43:00.683 Error: dzVents: Error: (3.0.2) mailHistory: ...domoticz/scripts/dzVents/generated_scripts/Script #1.lua:84: attempt to call a nil value (field 'executeShellCommand')

emailto is changed into my email adresses
In the comments...

Code: Select all

requires:
            dzVents >= 3.1.0 (domoticz V2020.2 build >= 12771 )
            installed mail on os level (linux only)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: P1 smart meter log sending a day

Post by kozzen »

How can i update to this 3.1.0, already used check for update or ./updaterelease
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Thursday 04 March 2021 21:44 How can i update to this 3.1.0, already used check for update or ./updaterelease
After making a backup of your domoticz directory (without the backup sub directory) you can do from within the domoticz directory

Code: Select all

 sudo ./updatebeta
This will bring your version to the latest build including the latest dzVents version.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: P1 smart meter log sending a day

Post by kozzen »

waaren wrote: Thursday 04 March 2021 21:48
kozzen wrote: Thursday 04 March 2021 21:44 How can i update to this 3.1.0, already used check for update or ./updaterelease
After making a backup of your domoticz directory (without the backup sub directory) you can do from within the domoticz directory

Code: Select all

 sudo ./updatebeta
This will bring your version to the latest build including the latest dzVents version.
thanks, did the update bubt now getting the following error in my mailbox or in the log
2021-03-04 21:56:00.947 Error: dzVents: Error: (3.1.5) mailHistory: Problem with shellCommand
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Thursday 04 March 2021 22:03
2021-03-04 21:56:00.947 Error: dzVents: Error: (3.1.5) mailHistory: Problem with shellCommand
Can you show all log lines from the start of the script until this error and the debug lines after it ?

The domoticz log will show what mail command is send from domoticz to the shell. If you have that command working from the CLI, domoticz should also work.

It should look like below (with a different file name in /tmp and a different Email address).

Code: Select all

mail -A /tmp/lua_Ob8JBf -s "power log" [email protected] < /dev/null 2> /dev/null
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: P1 smart meter log sending a day

Post by kozzen »

2021-03-04 21:56:00.417 Status: dzVents: Info: mailHistory: ------ Start internal script: Script #1:, trigger: "at 21:56"
2021-03-04 21:56:00.418 Status: dzVents: Debug: mailHistory: OpenURL: url = http://127.0.0.1:8080/json.htm?type=gra ... =day&idx=1
2021-03-04 21:56:00.418 Status: dzVents: Debug: mailHistory: OpenURL: method = GET
2021-03-04 21:56:00.418 Status: dzVents: Debug: mailHistory: OpenURL: post data = nil
2021-03-04 21:56:00.418 Status: dzVents: Debug: mailHistory: OpenURL: headers = nil
2021-03-04 21:56:00.418 Status: dzVents: Debug: mailHistory: OpenURL: callback = mailHistory
2021-03-04 21:56:00.418 Status: dzVents: Info: mailHistory: ------ Finished Script #1
2021-03-04 21:56:00.419 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-03-04 21:56:00.668 Status: dzVents: Info: Handling httpResponse-events for: "mailHistory"
2021-03-04 21:56:00.668 Status: dzVents: Info: mailHistory: ------ Start internal script: Script #1: HTTPResponse: "mailHistory"
2021-03-04 21:56:00.791 Status: dzVents: Debug: mailHistory: ExecuteShellCommand: command = mail -A /tmp/lua_C851pY -s "power log" [email protected] < /dev/null 2> /dev/null
2021-03-04 21:56:00.791 Status: dzVents: Debug: mailHistory: ExecuteShellCommand: callback = mailHistory
2021-03-04 21:56:00.791 Status: dzVents: Debug: mailHistory: ExecuteShellCommand: timeout = 20
2021-03-04 21:56:00.791 Status: dzVents: Debug: mailHistory: ExecuteShellcommand: path = /home/pi/domoticz/scripts/dzVents/data/
2021-03-04 21:56:00.793 Status: dzVents: Info: mailHistory: ------ Finished Script #1
2021-03-04 21:56:00.793 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2021-03-04 21:56:00.946 Status: dzVents: Info: Handling shellcommandResponse-events for: "mailHistory"
2021-03-04 21:56:00.946 Status: dzVents: Info: mailHistory: ------ Start internal script: Script #1: ShellCommandResponse: "mailHistory"
2021-03-04 21:56:00.948 Status: dzVents: Debug: mailHistory: {["dump"]=function, ["errorText"]="", ["timeoutOccurred"]=false, ["isHTTPResponse"]=false, ["isScene"]=false, ["isCustomEvent"]=false, ["statusText"]="", ["isSystem"]=false, ["trigger"]="mailHistory", ["baseType"]="shellcommandResponse", ["isXML"]=false, ["isHardware"]=false, ["isDevice"]=false, ["hasLines"]=false, ["data"]="", ["isJSON"]=false, ["isVariable"]=false, ["ok"]=false, ["isGroup"]=false, ["isTimer"]=false, ["isSecurity"]=false, ["callback"]="mailHistory", ["statusCode"]=127, ["isShellCommandResponse"]=true, ["shellCommandResponse"]="mailHistory"}
2021-03-04 21:56:00.949 Status: dzVents: Info: mailHistory: ------ Finished Script #1
2021-03-04 21:57:01.643 Status: Notification sent (Email)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Thursday 04 March 2021 22:21 mail -A /tmp/lua_C851pY -s "power log" [email protected] < /dev/null
What do do see when you give this command from the CLI ?
What do you see when do a cat tmp/lua_C851pY

returncode 127 is most of the time "command not found"
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kozzen
Posts: 13
Joined: Saturday 27 February 2021 7:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: P1 smart meter log sending a day

Post by kozzen »

when i type this

Code: Select all

mail -A /tmp/lua_C851pY -s "power log" [email protected] < /dev/null 2> /dev/null
i get nothing no fault

Code: Select all

cat tmp/lua_C851pY
i get permission denied
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: P1 smart meter log sending a day

Post by waaren »

kozzen wrote: Thursday 04 March 2021 23:26 when i type this

Code: Select all

mail -A /tmp/lua_C851pY -s "power log" [email protected] < /dev/null 2> /dev/null
i get nothing no fault

Code: Select all

cat tmp/lua_C851pY
i get permission denied
I suggested something else

Code: Select all

mail -A /tmp/lua_C851pY -s "power log" [email protected] < /dev/null 


if cat tmp/lua_C851pY gives you a permission denied then use

Code: Select all

sudo cat tmp/lua_C851pY


Best to first get the mail command with attachment from the Command Line fixed. There are multiple ways to set that up.
To do this is not specific domoticz so you might want to look at specialized Linux forums to get that working.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest