How to update a virtual 'Instant+counter' sensor with Lua

Moderator: leecollings

nood
Posts: 22
Joined: Sunday 26 July 2015 13:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by nood »

I just figured out how to scrape the local url too! I shared my python script here
Hieron wrote: Sunday 01 April 2018 13:20 Only thing I'm still trying to figure out is how to properly show the value for todays output. The correct value from the inverter is 0.440 kWh. So what is the value "Today: 0.060 kWh"?
I think you only have to find the total value, Domoticz will automaticaly calculate the difference with yesterday's total.
Hieron
Posts: 2
Joined: Saturday 31 March 2018 20:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by Hieron »

yea it works well, after 1 day the totals and all show up fine.
annegerben
Posts: 26
Joined: Wednesday 03 August 2016 12:34
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by annegerben »

gsmink wrote: Sunday 30 July 2017 11:04 Hi Annegerben,

Can you share the code with the ping option
annegerben wrote: Thanks for your code!
I've been using it quite a while, but recently I've added a ping check so the script doesn't have to run completely when the inverter is offline.
reply is a bit late, sorry.
But I'm using the builtin ping and change the code a bit :

if statement on the switch in the beginning and closing it at the end.
Spoiler: show

Code: Select all

commandArray = {}
lineNo = 0
now    = 0
temp   = 0
today  = 0
total  = 0
v1     = 0
v2     = 0
i1     = 0
i2     = 0
ip     = 'ip'
i = 1

-- Only run during day or saj can be pinged
-- if ( uservariables["dark"] == "False" ) then
if (otherdevices['saj'] == "On") then
--if (uservariables["saj_online"] == "on") then
    function UpdateDevice(device, data)
        idx = otherdevices_idx[device]
        if (idx == nil) then
            print('** Unknown device '..device)
        else
            commandArray[i] = {['UpdateDevice'] = idx..'|0|'..data}
            i = i+1
        end
    end

	--Get the XML data from the inverter
    local url = 'curl http://'..ip..'/real_time_data.xml'
    local data = assert(io.popen(url))

    for line in data:lines() do

        if (lineNo == 0 and string.find(line,'Normal') ~= nil) then
            lineNo = 1
        elseif (lineNo > 0) then
            if     (lineNo == 4) then now   = string.match(line, "%d+")
            elseif (lineNo == 5) then temp  = string.match(line, "%d+%.%d+")
            elseif (lineNo == 6) then today = string.match(line, "%d+%.%d+")
            elseif (lineNo == 8) then total = string.match(line, "%d+%.%d+")
            end
            lineNo = lineNo + 1
        end
    end

    data:close()

    if (lineNo >= 14) then
        -- Update the Domoticz GUI
        ep = v1*i1
        wp = v2*i2
        UpdateDevice('Solar', now..";".. total*1000)
        UpdateDevice('Invtemp', temp)

		-- Upload the data to PVoutput every 10 mins
		-- date = os.date("*t")
		-- if (date.min % 10 == 0) then
		--     baseURL="http://pvoutput.org/service/r2/addstatus.jsp?"
		--     API="yourAPIkey"
		--     PVO_URL= baseURL .. "sid=36263&key=" .. API .. "&d=" .. os.date("%Y%m%d") .. "&t=" .. os.date("%H:%M")
		--     PVO_URL = PVO_URL .. "&v1=" .. (today*1000) .. "&v2=" .. now
		--     commandArray['OpenURL'] = PVO_URL
		-- end
   end
end
return commandArray
jluc2808
Posts: 49
Joined: Thursday 18 January 2018 6:30
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by jluc2808 »

Hieron wrote: Sunday 01 April 2018 13:20 In my case I've added 2 dummy sensors:

Code: Select all

- 'Solar Output' Sensor type 'Electric (Instant+Counter)
- 'Inverter temp' Sensor type 'Temperature'
hi @hieron , i just wonder to know how you had selected the Sensor: type 'Electric (Instant+Counter) to get inside the display now and today
when i select the same sensor (as a virtual device) , i display NOW is the upper right part and just Today inside the text part.
Your attachment picture show :
NOW (instant) in the Upper right part,
Total Now and Today in the text part

this is exactly what i would like to do but never find the parameter i could display the 3 info
(problem isn't the update device code , but the display)
JeeGee
Posts: 22
Joined: Sunday 25 August 2019 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by JeeGee »

Hi, Found this post as I want to display my SAJ info in Domoticz too, but I need a some help please.

So far:
Took the script from the 3th post in this thread and added the IP address of my SAJ.
placed the script in "/home/pi/domoticz/scripts/lua" annd named is: "Solar.lua"
Created virtual hardware and 2 virtual devices. And didn't do anything with properties of them.
Rebooted my Pi. But sofar... nothing happens. Last update time in the devices is from the time I installed them.
So no values are read.

Do I need to do something with IDX?

In the logs, I don't find anything, or maybe thats because my Pi smartmeter is generating a lot of logs.
I see this: PluginSystem: Failed dynamic library load, install the latest libpython3.x library that is available for your platform.
But maybe that is not the issue.

Anyone a hint?

Edit, could someone post the script with the ping option?
Attachments
solar.JPG
solar.JPG (22.3 KiB) Viewed 1813 times
realtime.JPG
realtime.JPG (36.12 KiB) Viewed 1813 times
JeeGee
Posts: 22
Joined: Sunday 25 August 2019 13:10
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by JeeGee »

Ok, took some reading and now the info is read every minute with LUA from my invertor.
Filename had to be script_time_solar.lua RTFM.....

But it seems the values are not displayed correctly. Today (vandaag) shows the valuw for Total.
So I miss the values like in your examples, today and total displayed together.
Attachments
solar2.JPG
solar2.JPG (19.9 KiB) Viewed 1811 times
mancide
Posts: 29
Joined: Wednesday 17 May 2017 0:18
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.6
Location: Belgium - Ghent
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by mancide »

I had to adjust the script for my SAJ invertor (Sununo plus 4K-M-RD).

This is my script:

Code: Select all

commandArray = {}
local now    = 0
local bNowUpdated = false
local temp   = 0
local bTempUpdated = false
local today  = 0
local bTodayUpdated = false
local total  = 0
local bTotalUpdated = false
--not used for now
--local v1     = 0
--local v2     = 0
--local i1     = 0
--local i2     = 0
local ip     = '192.168.123.100'    --customize this to your invertor IP
local i = 1

    function UpdateDevice(device, data)
        idx = otherdevices_idx[device]
        if (idx == nil) then
            print('** Unknown device '..device)
        else
            commandArray[i] = {['UpdateDevice'] = idx..'|0|'..data}
            i = i+1
        end
    end

	--Get the XML data from the inverter
    local url = 'curl http://'..ip..'/real_time_data.xml'
    local data = assert(io.popen(url))
    
    --print(string.format("http request from SAJ: %s", data))

    local LineNr = 0
    for line in data:lines() do
        LineNr = LineNr + 1

        if( string.find(line, 'temp') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            temp  = string.match(line, "%d+%.%d+")
            bTempUpdated = true

        elseif( string.find(line, 'e%-total') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            total  = string.match(line, "%d+%.%d+")
            bTotalUpdated = true

        elseif( string.find(line, 'e%-today') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            today  = string.match(line, "%d+%.%d+")
            bTodayUpdated = true

        elseif( string.find(line, 'p%-ac') ~= nil)
        then
            --print(string.format("line %d: %s", LineNr, line))
            now  = string.match(line, "%d+")
            bNowUpdated = true

        else
            --print(string.format("line %d: %s", LineNr, line))
        end

    end

    data:close()

    -- Update the Domoticz GUI
    if( bTempUpdated==true)
    then
        --print(string.format("updating SAJ temp:%4.1f",temp))
        UpdateDevice('SAJ_Temp', temp)                          --customize the temperature sensor name
    end
    
    if( bNowUpdated==true and bTotalUpdated==true)
    then
        --print(string.format("updating SAJ energy:%d %d",now, total*1000))
        UpdateDevice('SAJ_Energy', now..";".. total*1000)       --customize the energy sensor name
    end
    

--        local ep = v1*i1
--        local wp = v2*i2
		-- Upload the data to PVoutput every 10 mins
		-- date = os.date("*t")
		-- if (date.min % 10 == 0) then
		--     baseURL="http://pvoutput.org/service/r2/addstatus.jsp?"
		--     API="yourAPIkey"
		--     PVO_URL= baseURL .. "sid=36263&key=" .. API .. "&d=" .. os.date("%Y%m%d") .. "&t=" .. os.date("%H:%M")
		--     PVO_URL = PVO_URL .. "&v1=" .. (today*1000) .. "&v2=" .. now
		--     commandArray['OpenURL'] = PVO_URL
		-- end
        
return commandArray

Raspberry Pi 3B v1.3 - 16GB SDcard - Raspbian Bullseye
Aeotec Z-Stick Gen5 (ZW090) -> Z-Wave JS UI -> mosquitto
RFXCOM RFXtrx433E
Unifi controller -> Domoticz-Unifi-Presence plugin
Nest thermostat
SAJ solar convertor
Smart Gateway Gas/water meter
BarryT
Posts: 367
Joined: Tuesday 31 March 2015 22:06
Target OS: Linux
Domoticz version: 2024.3
Location: east netherlands
Contact:

Re: How to update a virtual 'Instant+counter' sensor with Lua

Post by BarryT »

I used this script to get data from my saj solar, but the script isnt executed and no data is updating in the 2 dummy's :(
what is wrong?

Code: Select all

commandArray = {}

        lineNo = 0
        now    = 0
        temp   = 0
        today  = 0
        total  = 0
        v1     = 0
        v2     = 0
        i1     = 0
        i2     = 0

    i = 1

function UpdateDevice(device, data)
    idx = otherdevices_idx[device]
    if (idx == nil) then
        print('** Unknown device '..device)
    else
        commandArray[i] = {['UpdateDevice'] = idx..'|0|'..data}
        i = i+1
    end
end

        --Get the XML data from the inverter
        local url = 'curl http://192.168.1.18/real_time_data.xml'
        local data = assert(io.popen(url))

        for line in data:lines() do

            if (lineNo == 0 and string.find(line,'Normal') ~= nil) then
                lineNo = 1
            elseif (lineNo > 0) then
                if     (lineNo == 4) then now   = string.match(line, "%d+")
                elseif (lineNo == 5) then temp  = string.match(line, "%d+%.%d+")
                elseif (lineNo == 6) then today = string.match(line, "%d+%.%d+")
                elseif (lineNo == 8) then total = string.match(line, "%d+%.%d+")
                end
                lineNo = lineNo + 1
            end
        end

        data:close()

        if (lineNo >= 14) then
            -- Update the Domoticz GUI
            ep = v1*i1
            wp = v2*i2
            UpdateDevice('PV SAJ', now..";".. total*1000)
            UpdateDevice('Temperatuur SAJ', temp)
        end
end
return commandArray

saj.jpg
saj.jpg (222.46 KiB) Viewed 1246 times
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest