Domoticz + P1 monitor share device Topic is solved

Client tools or tools that can connect with Domoticz. Tools for Windows, iOS, Android, Linux etc.

Moderator: leecollings

piwpiw
Posts: 3
Joined: Tuesday 11 August 2020 20:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Gelderland
Contact:

Domoticz + P1 monitor share device

Post by piwpiw »

Dear All,

I have a Raspberry Pi 3 Model B Plus Rev 1.3 that runs P1 monitor from https://www.ztatz.nl/ and I wanted also to install Domoticz on the same device. This all went fine and both tools are running ok. I can add hardware in Domoticz and it looks great. :)

I would like to share the P1 connection now so that I have the smart meter info available in both tools.

How to manage this best? (not buying a https://www.iungo.nl/nl/bestellen/i/453 ... t-splitter splitter or building one http://womp3-14.vijfwal.nl/p2013a.html)

P1 monitor saves the data (telegram) every x seconds to /p1mon/mnt/ramdisk/p1msg.txt but how to get that to the Domoticz plugin correct ?
If I can somehow import this file that would be the quickest option I assume.

Or should I use MQTT ?
I hava a Mosquitto MQTT broker also running on the same device (however extra resources are in use then)

I have searched the forum but did not see anyone trying to do something like this yet (at least documented it).

Who can provide the most efficient methode to solve this issue using one device?

Looking forward to seeing a solution.

Thanks for reading...

Chrs P.

Telegram data in the file : p1msg.txt

/ISK5\2M550E-1012

1-3:0.2.8(50)
0-0:1.0.0(200812103125S)
0-0:96.1.1(4530303433303037313339323238373138)
1-0:1.8.1(001435.864*kWh)
1-0:1.8.2(001732.770*kWh)
1-0:2.8.1(000000.000*kWh)
1-0:2.8.2(000000.000*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(00.332*kW)
1-0:2.7.0(00.000*kW)
0-0:96.7.21(00007)
0-0:96.7.9(00004)
1-0:99.97.0(2)(0-0:96.7.19)(180507091451S)(0000000171*s)(190201093010W)(0000004910*s)
1-0:32.32.0(00007)
1-0:32.36.0(00001)
0-0:96.13.0()
1-0:32.7.0(229.7*V)
1-0:31.7.0(001*A)
1-0:21.7.0(00.226*kW)
1-0:22.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303339303031383138393438313138)
0-1:24.2.1(200812103004S)(01435.951*m3)
!EF97
User avatar
FireWizard
Posts: 1889
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Domoticz + P1 monitor share device

Post by FireWizard »

Hi,

As the P1 monitor has an APi at http://<IP address>/json/apiV1basic.php, I think the easiest way is to use this API. With Node Red you can read this API and with MQTT you push the data to Domoticz.

However the data is a little different from the RAW protocol and so not all sensors may available, compared with a direct connection between P1 meter and Domoticz.

Regards
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

piwpiw wrote: Wednesday 12 August 2020 10:47 I would like to share the P1 connection now so that I have the smart meter info available in both tools.
P1 monitor saves the data (telegram) every x seconds to /p1mon/mnt/ramdisk/p1msg.txt but how to get that to the Domoticz plugin correct ?
If I can somehow import this file that would be the quickest option I assume.
There are multiple ways of handling this and it depends on your personal preference what will be the easiest method.
My preference would be be to use dzVents as it does not require any external programs.
See below for a kind of proof of concept where dzvents reads the telegram text file and store the values in a Lua table. From there is quite simple to store the data in virtual devices. (P1 energy, P1 gas, etc)
The frequency of reading the data can vary from every 10 seconds to once an hour and everything in between. Happy to help if you are interested in developing this approach further.

Code: Select all

return
{
    on = 
    {
        timer = 
        {
            'every minute',
        },
        devices = 
        {
            'readTelegram', -- Only used during test
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'P1 text', 
    },

    execute = function(dz, item)
      
        local function readFile(fn)
            if not(dz.utils.fileExists(fn)) then return {} end
            
            lines = {}
            for line in io.lines(fn) do 
                if line:find(":") then
                    lines[line:match(':(%d+.%d+.%d+)')] = dz.utils.round(tonumber(line:match('%b()%s*%c*$'):match('%d+%.*%d*') or -1),1)
                end
            end
            return lines
        end
    
        -- main code
        -- dz.utils.dumpTable(readFile('/tmp/telegram'))
        dz.utils.dumpTable(readFile('/p1mon/mnt/ramdisk/p1msg.tx'))

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
piwpiw
Posts: 3
Joined: Tuesday 11 August 2020 20:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Gelderland
Contact:

Re: Domoticz + P1 monitor share device

Post by piwpiw »

Hello Waaren,

Your approach indeed skips the MQTT and that makes it easier and less resources are used.... However I would like your help in setting this up.
I have no experience with dzvents / lua ...
I played with merecat (pico webserver) to listen on a new port and served the p1msg.txt as index.html and use the ' p1 smart meter with lan interface' so to see if that would do somehing but that failed :)

Chrs P.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

piwpiw wrote: Wednesday 12 August 2020 18:00 I would like your help in setting this up.
OK.
Please let me know what your required frequency is and which information from your smartmeter you need in domoticz. If possible please use PM so we don't have a private conversation in the public part of the forum. When everything executes OK I will share the script here.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
piwpiw
Posts: 3
Joined: Tuesday 11 August 2020 20:43
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Gelderland
Contact:

Re: Domoticz + P1 monitor share device

Post by piwpiw »

.

With the great help of Waaren we managed to get it to work and now on my P1 monitor from https://www.ztatz.nl installed image I also added the Domoticz tool via the command line via (curl -L install.domoticz.com | sudo bash). Domoticz now reads the data from the P1 monitor tool that is written to a ramdisk (/p1mon/mnt/ramdisk/p1msg.txt) ever x seconds

Code: Select all

scriptVar = 'readTelegram'

return
{
    on = 
    {
        timer = 
        {
            'every minute',
        },
        devices = 
        {
            scriptVar, -- Only used during test
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar, 
    },

    execute = function(dz, item)

        local power = dz.devices('Power') -- change to name of your virtual smart meter (P1 smart Meter (Electric))
        local gas = dz.devices('Gas') -- change to name of your virtual smart meter (Gas)
        local l1 = dz.devices('Usage l1')  -- change to name of your virtual Usage L1 (Usage Electric)
        local volt = dz.devices('Voltage') -- change to name of your virtual voltage device
        local ampere = dz.devices('Amperage') -- change to name of your virtual amperage device (1 phase)

        local smartFile = '/p1mon/mnt/ramdisk/p1msg.txt' --  full qualified file name
        -- local smartFile = '/tmp/telegram'
        
        local frequency = 6 -- every 60 / frequency seconds
        local P1Reference =
        {
             ['l1Power'] = '22.7.0',
             ['tariff'] = '96.14.0',
             ['usage2'] = '1.8.2',
             ['usage1'] = '1.8.1',
             ['returned1'] = '2.8.1',
             ['returned2'] = '2.8.2',
             ['powerReceived'] = '2.7.0',
             ['gas'] = '24.2.1',
             ['powerUsage'] = '21.7.0',
             ['powerDelivery'] = '1.7.0',
             ['voltage'] = '32.7.0',
        }
        
        local debug = _G.logLevel == dz.LOG_DEBUG

        local function repeatScript(frequency)
            local interval = math.floor(60/frequency)
            for secondsDelay = interval, (60 - interval), interval do
                dz.emitEvent(scriptVar).afterSec(secondsDelay)
           end
        end

        local function readFile(fn)
            
            local f = io.open(fn, 'rb' )
            local completeFile = f:read('*a')
            f:close()
            
            local lines = {}
            local rawLines = dz.utils.stringSplit(completeFile,'\n\r')
            for _, line in ipairs(rawLines) do 
                if line:find(':') then
                    lines[line:match(':(%d+.%d+.%d+)')] = math.floor((line:match('%b()%s*%c*$'):match('%d+%.*%d*') or -1) * 1000) 
                end
            end
            -- dz.utils.dumpTable(lines)
            return lines
            
        end
    
        local function updateDevices(t)
            power.updateP1  (
                                t[P1Reference.usage1] or 0,
                                t[P1Reference.usage2]  or 0,
                                t[P1Reference.returned1]  or 0,
                                t[P1Reference.returned2]  or 0,
                                t[P1Reference.powerUsage]  or 0,
                                t[P1Reference.powerDelivery]  or 0
                            )

            if t[P1Reference.gas] then gas.updateGas(t[P1Reference.gas]) end
            if t[P1Reference.powerUsage] then l1.updateEnergy(t[P1Reference.powerUsage]) end
            if t[P1Reference.voltage] then volt.updateVoltage(t[P1Reference.voltage] / 1000) end
            if t[P1Reference.powerUsage] and t[P1Reference.voltage] then ampere.updateCurrent(dz.utils.round(t[P1Reference.powerUsage] /  ( t[P1Reference.voltage]  / 1000 ) ,2 ) )  end

        end 
        
        local function showResults(t)
            if not(debug) then return end
            for code, value in pairs(t) do
                for name, refCode in pairs(P1Reference) do 
                    if refCode == code then 
                        dz.log(name .. ' ==>> ' .. value, dz.LOG_DEBUG)
                        break
                    end
                end
            end
        end

        -- main code
        if not(item.isCustomEvent) then
            repeatScript(frequency)
        end

        local actual = readFile(smartFile)
        showResults(actual)
        updateDevices(actual)

    end
}
This script needs to reside in the folder /home/root/domoticz/scripts/dzVents/scripts/ and needs to have the lua extention. In my case it was : p1-file.lua

After that some adjustments are needed in the settings / hardware which you can see in the attached image.
Beginners mistake only one is actualy needed as all virtual switches can be generted on one.
The virtual switches need the correct name as mentioned in the script:
  • Power - type [P1 smart Meter (Electric)]
  • Gas - type P1 [smart Meter (Electric)]
  • Usage l1 - type [Usage Electric]
  • Voltage - type [voltage device]
  • Amperage - type [amperage device (1 phase)]
Again many thanks to Waaren for the help and patient to get this working !!

Rgds P.

Ps I added a small example image on how it will look like.

.
Attachments
hardware.jpg
hardware.jpg (33 KiB) Viewed 4159 times
examp1.jpg
examp1.jpg (42.69 KiB) Viewed 4159 times
User avatar
gizmocuz
Posts: 2546
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Domoticz + P1 monitor share device

Post by gizmocuz »

Small note, you do not have to create 4 dummy hardware devices, one is enough (Call it 'Dummy')
Quality outlives Quantity!
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Hi,
I also copied the lua script thanks for the hard work. I have no experience with LUA.
But when the script runs I see errors in the log window and its not updating the devices. I created Power, Amperage, Gas, Voltage, Usage l1. Is it possible that the scrip is not working because my meter has three phases (L1, L2 and L3)

domoticz/scripts/dzVents/scripts/p1-file.lua:29: unexpected symbol near '['

Thanks.

Regards, R.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Thursday 03 September 2020 22:49
But when the script runs I see errors in the log window and its not updating the devices. I created Power, Amperage, Gas, Voltage, Usage l1. Is it possible that the scrip is not working because my meter has three phases (L1, L2 and L3)

domoticz/scripts/dzVents/scripts/p1-file.lua:29: unexpected symbol near '['
Can you try with this one and share what you see in the log?
The script was not created with 3 phases in mind by maybe it can be amended for this without much effort.

Code: Select all

scriptVar = 'readTelegram'

return
{
    on = 
    {
        timer = 
        {
            'every minute',
        },
        devices = 
        {
            scriptVar, -- Only used during test
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar, 
    },

    execute = function(dz, item)

        local power = dz.devices('Power') -- change to name of your virtual smart meter (P1 smart Meter (Electric))
        local gas = dz.devices('Gas') -- change to name of your virtual smart meter (Gas)
        local l1 = dz.devices('Usage l1')  -- change to name of your virtual Usage L1 (Usage Electric)
        local volt = dz.devices('Voltage') -- change to name of your virtual voltage device
        local ampere = dz.devices('Amperage') -- change to name of your virtual amperage device (1 phase)


        local smartFile = '/p1mon/mnt/ramdisk/p1msg.txt' --  full qualified file name
        -- local smartFile = '/tmp/telegram'
        
        local frequency = 6 -- every 60 / frequency seconds
        local P1Reference =
        {
             ['l1Power'] = '22.7.0',
             ['tariff'] = '96.14.0',
             ['usage2'] = '1.8.2',
             ['usage1'] = '1.8.1',
             ['returned1'] = '2.8.1',
             ['returned2'] = '2.8.2',
             ['powerReceived'] = '2.7.0',
             ['gas'] = '24.2.1',
             ['powerUsage'] = '21.7.0',
             ['powerDelivery'] = '1.7.0',
             ['voltage'] = '32.7.0',
        }
        
        local debug = _G.logLevel == dz.LOG_DEBUG

        local function repeatScript(frequency)
            local interval = math.floor(60/frequency)
            for secondsDelay = interval, (60 - interval), interval do
                dz.emitEvent(scriptVar).afterSec(secondsDelay)
           end
        end

        local function readFile(fn)
            
            local f = io.open(fn, 'rb' )
            local completeFile = f:read('*a')
            f:close()
            
            local lines = {}
            local rawLines = dz.utils.stringSplit(completeFile,'\n\r')
            for _, line in ipairs(rawLines) do 
                if line:find(':') then
                    lines[line:match(':(%d+.%d+.%d+)')] = math.floor((line:match('%b()%s*%c*$'):match('%d+%.*%d*') or -1) * 1000) 
                end
            end
            dz.utils.dumpTable(lines)
            return lines
            
        end
    
        local function updateDevices(t)
            power.updateP1  (
                                t[P1Reference.usage1] or 0,
                                t[P1Reference.usage2]  or 0,
                                t[P1Reference.returned1]  or 0,
                                t[P1Reference.returned2]  or 0,
                                t[P1Reference.powerUsage]  or 0,
                                t[P1Reference.powerDelivery]  or 0
                            )

            if t[P1Reference.gas] then gas.updateGas(t[P1Reference.gas]) end
            if t[P1Reference.powerUsage] then l1.updateEnergy(t[P1Reference.powerUsage]) end
            if t[P1Reference.voltage] then volt.updateVoltage(t[P1Reference.voltage] / 1000) end
            if t[P1Reference.powerUsage] and t[P1Reference.voltage] then ampere.updateCurrent(dz.utils.round(t[P1Reference.powerUsage] /  ( t[P1Reference.voltage]  / 1000 ) ,2 ) )  end

        end 
        
        local function showResults(t)
            if not(debug) then return end
            for code, value in pairs(t) do
                for name, refCode in pairs(P1Reference) do 
                    if refCode == code then 
                        dz.log(name .. ' ==>> ' .. value, dz.LOG_DEBUG)
                        break
                    end
                end
            end
        end

        -- main code
        if not(item.isCustomEvent) then
            repeatScript(frequency)
        end

        local actual = readFile(smartFile)
        showResults(actual)
        updateDevices(actual)

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Thanks for your quick response I now see the three following messages:

Method updateP1 is not available for device "Power" (deviceType=General, deviceSubType=kWh). If you believe this is not correct, please report.

An error occurred when calling event handler p1-file

/domoticz/scripts/dzVents/scripts/p1-file.lua:93: attempt to call a nil value (field 'updateEnergy')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Thursday 03 September 2020 23:15 Thanks for your quick response I now see the three following messages:

Method updateP1 is not available for device "Power" (deviceType=General, deviceSubType=kWh). If you believe this is not correct, please report.

An error occurred when calling event handler p1-file

/domoticz/scripts/dzVents/scripts/p1-file.lua:93: attempt to call a nil value (field 'updateEnergy')
You used an incorrect type for the "Power" device. It must be defined as: P1 smart Meter (Electric)
Please also look in the script as what types the other devices must be defined.

The error message should not be the only logline from this script. Please share the rest of the log
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Thanks now the log window shows no errors anymore. The counters Voltage and Amperage stay at zero and the counters for Power and Usage l1 only show values for phase L1.


2020-09-03 23:51:30.377 Status: dzVents: Info: Handling Domoticz custom event for: "readTelegram"
2020-09-03 23:51:30.377 Status: dzVents: Info: readTelegram: ------ Start external script: p1-file.lua: Custom event: "readTelegram"
2020-09-03 23:51:30.395 Status: dzVents: Debug: readTelegram: Processing device-adapter for Power: P1 smart meter energy device adapter
2020-09-03 23:51:30.396 Status: dzVents: Debug: readTelegram: Processing device-adapter for Gas: Gas device adapter
2020-09-03 23:51:30.397 Status: dzVents: Debug: readTelegram: Processing device-adapter for Usage l1: Electric usage device adapter
2020-09-03 23:51:30.397 Status: dzVents: Debug: readTelegram: Processing device-adapter for Voltage: Voltage device adapter
2020-09-03 23:51:30.397 Status: dzVents: Debug: readTelegram: Processing device-adapter for Amperage: 3-phase Ampere device adapter
2020-09-03 23:51:30.398 Status: dzVents: > 24.2.1: 5611154
2020-09-03 23:51:30.398 Status: dzVents: > 96.1.0: 4.7303033333539e+36
2020-09-03 23:51:30.398 Status: dzVents: > 31.7.0: 3000
2020-09-03 23:51:30.398 Status: dzVents: > 21.7.0: 397
2020-09-03 23:51:30.398 Status: dzVents: > 96.7.21: 2000
2020-09-03 23:51:30.398 Status: dzVents: > 0.2.8: 42000
2020-09-03 23:51:30.398 Status: dzVents: > 62.7.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 42.7.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 2.8.1: 0
2020-09-03 23:51:30.398 Status: dzVents: > 32.36.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 52.36.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 96.13.1: -1000
2020-09-03 23:51:30.398 Status: dzVents: > 72.36.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 96.1.1: 4.530303330303e+36
2020-09-03 23:51:30.398 Status: dzVents: > 72.32.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 22.7.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 32.32.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 52.32.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 1.8.1: 14442050
2020-09-03 23:51:30.398 Status: dzVents: > 51.7.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 1.0.0: 200903235048000
2020-09-03 23:51:30.398 Status: dzVents: > 71.7.0: 1000
2020-09-03 23:51:30.398 Status: dzVents: > 1.7.0: 663
2020-09-03 23:51:30.398 Status: dzVents: > 96.13.0: -1000
2020-09-03 23:51:30.398 Status: dzVents: > 2.8.2: 0
2020-09-03 23:51:30.398 Status: dzVents: > 1.8.2: 11133555
2020-09-03 23:51:30.398 Status: dzVents: > 99.97.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 96.14.0: 1000
2020-09-03 23:51:30.398 Status: dzVents: > 61.7.0: 197
2020-09-03 23:51:30.398 Status: dzVents: > 2.7.0: 0
2020-09-03 23:51:30.398 Status: dzVents: > 24.1.0: 3000
2020-09-03 23:51:30.398 Status: dzVents: > 96.7.9: 0
2020-09-03 23:51:30.398 Status: dzVents: > 41.7.0: 68
2020-09-03 23:51:30.398 Status: dzVents: Debug: readTelegram: gas ==>> 5611154
2020-09-03 23:51:30.398 Status: dzVents: Debug: readTelegram: powerUsage ==>> 397
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: returned1 ==>> 0
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: l1Power ==>> 0
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: usage1 ==>> 14442050
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: powerDelivery ==>> 663
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: returned2 ==>> 0
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: usage2 ==>> 11133555
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: tariff ==>> 1000
2020-09-03 23:51:30.399 Status: dzVents: Debug: readTelegram: powerReceived ==>> 0
2020-09-03 23:51:30.399 Status: dzVents: Info: readTelegram: ------ Finished p1-file.lua
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Friday 04 September 2020 0:01 Thanks now the log window shows no errors anymore. The counters Voltage and Amperage stay at zero and the counters for Power and Usage l1 only show values for phase L1.
Thx.
Can you please provide the information on what the codes represent?
With that information I can complete the script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Thanks,

Power kW usage L1: 1-0:21.7.0
Power kW usage L2: 1-0:41.7.0
Power kW usage L3: 1-0:61.7.0

Ampères L1: 1-0:31.7.0
Ampères L2: 1-0:51.7.0
Ampères L3: 1-0:71.7.0

it seems that my meter does not report the Voltages L1, L2 and L3
Voltage L1: 1-0:32.7.0
Voltage L2: 1-0:52.7.0
Voltage L3: 1-0:72.7.0
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Friday 04 September 2020 0:37 Power kW usage L1: 1-0:21.7.0 ....
Can you please also send an example of your telegram text file? That will make it easier to see if I am on the right track.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Hi below the output. Thanks

Code: Select all

1-3:0.2.8(42)
0-0:1.0.0(200904204639S)
0-0:96.1.1(4530303330303033323930323935373136)
1-0:1.8.1(014446.348*kWh)
1-0:1.8.2(011145.374*kWh)
1-0:2.8.1(000000.000*kWh)
1-0:2.8.2(000000.000*kWh)
0-0:96.14.0(0002)
1-0:1.7.0(00.728*kW)
1-0:2.7.0(00.000*kW)
0-0:96.7.21(00002)
0-0:96.7.9(00000)
1-0:99.97.0(0)(0-0:96.7.19)
1-0:32.32.0(00000)
1-0:52.32.0(00000)
1-0:72.32.0(00000)
1-0:32.36.0(00000)
1-0:52.36.0(00000)
1-0:72.36.0(00000)
0-0:96.13.1()
0-0:96.13.0()
1-0:31.7.0(003*A)
1-0:51.7.0(000*A)
1-0:71.7.0(002*A)
1-0:21.7.0(00.418*kW)
1-0:41.7.0(00.004*kW)
1-0:61.7.0(00.304*kW)
1-0:22.7.0(00.000*kW)
1-0:42.7.0(00.000*kW)
1-0:62.7.0(00.000*kW)
0-1:24.1.0(003)
0-1:96.1.0(4730303333353931323633313235353136)
0-1:24.2.1(200904200000S)(05611.715*m3)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Friday 04 September 2020 20:51 Hi below the output. Thanks
The reported amperage in this file does not seem very accurate. In fact if you could get your voltage from another source, the script could made to display this and a much more accurate amperage.

Can you try this and report back with your findings?

Code: Select all

local scriptVar = 'readTelegram'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        devices =
        {
            -- scriptVar, -- Only used during test
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local power = dz.devices('Power') -- change to name of your virtual smart meter (P1 smart Meter (Electric))
        local gas = dz.devices('Gas') -- change to name of your virtual smart meter (Gas)
        
        local l1 = dz.devices('Usage l1')  -- change to name of your virtual Usage L1 (Usage Electric)
        local l2 = dz.devices('Usage l2')  -- change to name of your virtual Usage L1 (Usage Electric)
        local l3 = dz.devices('Usage l3')  -- change to name of your virtual Usage L1 (Usage Electric)
        
        -- local volt = dz.devices('Voltage') -- change to name of your virtual voltage device
        
        local ampere1Phase = dz.devices('amperageTotal') -- change to name of your virtual amperage device (1 phase)
        local ampere3Phases = dz.devices('amperageCombined') -- change to name of your virtual amperage device (3 phases)

        local smartFile = '/p1mon/mnt/ramdisk/p1msg.txt' --  full qualified file name
       --  local smartFile = '/tmp/telegram'

        local frequency = 6 -- every 60 / frequency seconds
        local P1Reference =
        {
             ['l1Power'] = '22.7.0',
             ['tariff'] = '96.14.0',
             ['usage2'] = '1.8.2',
             ['usage1'] = '1.8.1',
             ['returned1'] = '2.8.1',
             ['returned2'] = '2.8.2',
             ['powerReceived'] = '2.7.0',
             ['gas'] = '24.2.1',
             ['powerUsageL1'] = '21.7.0',
             ['powerUsageL2'] = '41.7.0',
             ['powerUsageL3'] = '61.7.0',
             ['powerDelivery'] = '1.7.0',
             ['powerUsageL2'] = '41.7.0',
             ['powerUsageL3'] = '61.7.0',
             ['amperageL1'] = '31.7.0',
             ['amperageL2'] = '51.7.0',
             ['amperageL3'] = '71.7.0',
             ['voltageL1'] = '32.7.0',
             ['voltageL2'] = '52.7.0',
             ['voltageL3'] = '72.7.0',
        }

        local debug = _G.logLevel == dz.LOG_DEBUG

        local function repeatScript(frequency)
            local interval = math.floor(60/frequency)
            for secondsDelay = interval, (60 - interval), interval do
                dz.emitEvent(scriptVar).afterSec(secondsDelay)
           end
        end

        function table.invert(t)
           local s={}
           for k,v in pairs(t) do
             s[v]=k
           end
           return s
        end

        local function readFile(fn, it)

            local f = io.open(fn, 'rb' )
            local completeFile = f:read('*a')
            f:close()

            local lines = {}
            local rawLines = dz.utils.stringSplit(completeFile,'\n\r')
            for _, line in ipairs(rawLines) do
                if line:find(':') then
                    key = it[line:match(':(%d+.%d+.%d+)')] 
                    if key then 
                        lines[key] = math.floor((line:match('%b()%s*%c*$'):match('%d+%.*%d*') or -1) * 1000)
                    end
                end
            end
            dz.utils.dumpTable(lines)
            return lines

        end

        local function updateDevices(t)
            power.updateP1  (
                                t.usage1 or 0,
                                t.usage2  or 0,
                                t.returned1  or 0,
                                t.returned2  or 0,
                                t.powerUsageL1 + t.powerUsageL2 + t.powerUsageL3,
                                t.powerDelivery  or 0
                            )

            if t.gas then gas.updateGas(t.gas) end
            
            if t.powerUsageL1 then l1.updateEnergy(t.powerUsageL1) end
            if t.powerUsageL2 then l2.updateEnergy(t.powerUsageL2) end
            if t.powerUsageL3 then l3.updateEnergy(t.powerUsageL3 ) end
            
            if t.amperageL1 then ampere1Phase.updateCurrent(( t.amperageL1 + t.amperageL2 + t.amperageL3 ) / 1000 ) end
            if t.amperageL1 then ampere3Phases.updateCurrent( t.amperageL1/1000, t.amperageL2 /1000,  t.amperageL3 / 1000 ) end

        end

        local function showResults(t, it)
            if not(debug) then return end
            for code, value in pairs(t) do
                if it[code] then 
                    dz.log(it[code] .. ' ==>> ' .. value, dz.LOG_DEBUG)
                end
            end
        end

        -- main code
        if not(item.isCustomEvent) then
            repeatScript(frequency)
        end

        local invertedTable = table.invert(P1Reference) 

        
        local actual = readFile(smartFile, invertedTable)
        showResults(actual, invertedTable)
        updateDevices(actual, invertedTable)

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

The script is working thanks for that, but now I need to find out how to get the voltage from another source and put that source in the script.
urool
Posts: 8
Joined: Thursday 03 September 2020 22:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by urool »

Hi, I now have a device in Domoticz that shows the Input Voltage, the device name is "Input Voltage", how can I use the device "Input Voltage" to let the script to calculate the the amperage. I know how to calculate the amperage by devide the Power/volt.

Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Domoticz + P1 monitor share device

Post by waaren »

urool wrote: Monday 07 September 2020 23:52 Hi, I now have a device in Domoticz that shows the Input Voltage, the device name is "Input Voltage", how can I use the device "Input Voltage" to let the script to calculate the the amperage. I know how to calculate the amperage by devide the Power/volt.

Thanks
Can you try with this ?

Code: Select all

local scriptVar = 'readTelegram'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        devices =
        {
            -- scriptVar, -- Only used during test
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local power = dz.devices('Power') -- change to name of your virtual smart meter (P1 smart Meter (Electric))
        local gas = dz.devices('Gas') -- change to name of your virtual smart meter (Gas)
        local volt = dz.devices('Input Voltage') -- change to name of your virtual voltage device

        local l1 = dz.devices('Usage l1')  -- change to name of your virtual Usage L1 (Usage Electric)
        local l2 = dz.devices('Usage l2')  -- change to name of your virtual Usage L1 (Usage Electric)
        local l3 = dz.devices('Usage l3')  -- change to name of your virtual Usage L1 (Usage Electric)


        local ampere1Phase = dz.devices('amperageTotal') -- change to name of your virtual amperage device (1 phase)
        local ampere3Phases = dz.devices('amperageCombined') -- change to name of your virtual amperage device (3 phases)
        local amperage1, amperage2, amperage3 = 0, 0, 0

        local smartFile = '/p1mon/mnt/ramdisk/p1msg.txt' --  full qualified file name
       --  local smartFile = '/tmp/telegram'

        local frequency = 6 -- every 60 / frequency seconds
        local P1Reference =
        {
             ['l1Power'] = '22.7.0',
             ['tariff'] = '96.14.0',
             ['usage2'] = '1.8.2',
             ['usage1'] = '1.8.1',
             ['returned1'] = '2.8.1',
             ['returned2'] = '2.8.2',
             ['powerReceived'] = '2.7.0',
             ['gas'] = '24.2.1',
             ['powerUsageL1'] = '21.7.0',
             ['powerUsageL2'] = '41.7.0',
             ['powerUsageL3'] = '61.7.0',
             ['powerDelivery'] = '1.7.0',
             ['powerUsageL2'] = '41.7.0',
             ['powerUsageL3'] = '61.7.0',
             ['amperageL1'] = '31.7.0',
             ['amperageL2'] = '51.7.0',
             ['amperageL3'] = '71.7.0',
             ['voltageL1'] = '32.7.0',
             ['voltageL2'] = '52.7.0',
             ['voltageL3'] = '72.7.0',
        }

        local debug = _G.logLevel == dz.LOG_DEBUG

        local function repeatScript(frequency)
            local interval = math.floor(60/frequency)
            for secondsDelay = interval, (60 - interval), interval do
                dz.emitEvent(scriptVar).afterSec(secondsDelay)
           end
        end

        function table.invert(t)
           local s={}
           for k,v in pairs(t) do
             s[v]=k
           end
           return s
        end

        local function readFile(fn, it)

            local f = io.open(fn, 'rb' )
            local completeFile = f:read('*a')
            f:close()

            local lines = {}
            local rawLines = dz.utils.stringSplit(completeFile,'\n\r')
            for _, line in ipairs(rawLines) do
                if line:find(':') then
                    key = it[line:match(':(%d+.%d+.%d+)')]
                    if key then
                        lines[key] = math.floor((line:match('%b()%s*%c*$'):match('%d+%.*%d*') or -1) * 1000)
                    end
                end
            end
            dz.utils.dumpTable(lines)
            return lines

        end

        local function updateDevices(t)
            power.updateP1  (
                                t.usage1 or 0,
                                t.usage2  or 0,
                                t.returned1  or 0,
                                t.returned2  or 0,
                                t.powerUsageL1 + t.powerUsageL2 + t.powerUsageL3,
                                t.powerDelivery  or 0
                            )

            if t.gas then gas.updateGas(t.gas) end

            if t.powerUsageL1 then
                l1.updateEnergy(t.powerUsageL1)
                amperage1 = dz.utils.round( t.powerUsageL1 / volt.voltage , 2)
            end

            if t.powerUsageL2 then
                l2.updateEnergy(t.powerUsageL2)
                amperage2 = dz.utils.round( t.powerUsageL2 / volt.voltage , 2)
            end

            if t.powerUsageL3 then
                l3.updateEnergy(t.powerUsageL3 )
                amperage3 = dz.utils.round( t.powerUsageL3 / volt.voltage , 2)
            end

            ampere1Phase.updateCurrent( dz.utils.round( amperage1 + amperage2 + amperage3 , 2 ) )
            ampere3Phases.updateCurrent( amperage1, amperage2, amperage3 )

        end

        local function showResults(t, it)
            if not(debug) then return end
            for code, value in pairs(t) do
                if it[code] then
                    dz.log(it[code] .. ' ==>> ' .. value, dz.LOG_DEBUG)
                end
            end
        end

        -- main code
        if not(item.isCustomEvent) then
            repeatScript(frequency)
        end

        local invertedTable = table.invert(P1Reference)


        local actual = readFile(smartFile, invertedTable)
        showResults(actual, invertedTable)
        updateDevices(actual, invertedTable)

    end
}
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