dzVents calculate average of 60 readings

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

dzVents calculate average of 60 readings

Post by Copitano »

I have been using a SEM228T pyranometer for several months now. This is read into domoticz every 10 seconds with a Modbus plugin. I can't adjust those 10 seconds. The value that comes in is given in W/m² and I convert it to J/cm² with a dzVents script. The script calculates with the number of milliseconds since the last update (Joules = Watt.sec).
Now I have found out that the outcome of the script over a whole day is about 6% too high. This is probably due to some delay between the measurement and the calculation. Many small deviations then add up to a substantial deviation. If I perform the same calculation for the 10-minute average data that Buienradar sends to domoticz, the daily total almost corresponds with the figures published by the KNMI the following day (deviation < 0.3%).

To (almost) eliminate the error in the daily total for my own pyranometer, I now also want to calculate this per 10 minutes with a dzVents script. So I want to calculate the arithmetic mean of 60 values ​​and send it to a domoticz sensor which I then use as input for the calculation to J/cm².

I made the following script for that.

Code: Select all

return
{
    
	on = {
        devices = {'Sun Power SEM228T'}
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'SEM228T',
    },

    data =
    {
        SEM228THistory = { history = true, maxMinutes = 10 },
    },

    execute = function(domoticz, sensor)

         -- add new data
        domoticz.data.SEM228THistory.add(sensor.Watts)

        -- average
        local average = domoticz.data.SEM228THistory.avg()
       
        -- maximum value in the past 10 minutes:
        local max = domoticz.data.temperatures.maxSince('00:10:00')

        -- maximum value in the past 10 minutes:
        local min = domoticz.data.temperatures.minSince('00:10:00')

    end
}
However, in the log I get the following error.

Code: Select all

2022-03-30 15:25:22.639 Error: dzVents: Error: (3.1.7) SEM228T: Item data is not a number type. Type is nil
2022-03-30 15:25:22.639 Error: dzVents: Error: (3.1.7) SEM228T: An error occurred when calling event handler SEM228T10M
2022-03-30 15:25:22.639 Error: dzVents: Error: (3.1.7) SEM228T: /home/pi/domoticz/dzVents/runtime/HistoricalStorage.lua:289: attempt to perform arithmetic on a nil value (local 'sum')
The SEM gives a string value that apparently first has to be converted to a number. But I have no idea how and where I do that. I did find https://www.domoticz.com/wiki/DzVents:_ ... %20require, but I don't quite understand it. Who can help me on the right track?
User avatar
waltervl
Posts: 5990
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by waltervl »

What kind of device is 'Sun Power SEM228T' ?
If it is a custom sensor you have to use .add(sensor.sensorValue) instead of .add(sensor.Watts)

Edit: if it is a Solar Radiation device use .add(sensor.radiation) instead of .add(sensor.Watts)
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
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

To be quite honest, I don't know exactly.
I assume solar radiation because I had to enter it in the plugin that retrieves the data via Modbus. I have given both of your suggestions a try. No luck so far.
Sensor looks like this and it's in the weather section. No further indication.
Attachments
Schermafbeelding 2022-03-30 175156.jpg
Schermafbeelding 2022-03-30 175156.jpg (16.54 KiB) Viewed 1268 times
Last edited by Copitano on Wednesday 30 March 2022 19:30, edited 1 time in total.
User avatar
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

Code: Select all

2022-03-30 17:19:22.465 Status: dzVents: Info: ------ Start external script: Sun Power SEM228T.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-03-30 17:19:22.467 Status: dzVents: device> signalLevel: 12
2022-03-30 17:19:22.467 Status: dzVents: device> state: 130
2022-03-30 17:19:22.467 Status: dzVents: device> isScene: false
2022-03-30 17:19:22.467 Status: dzVents: device> hardwareName: Test Total solar radiation
2022-03-30 17:19:22.467 Status: dzVents: device> isTimer: false
2022-03-30 17:19:22.467 Status: dzVents: device> radiation: 130
2022-03-30 17:19:22.467 Status: dzVents: device> _nValue: 1
2022-03-30 17:19:22.467 Status: dzVents: device> isDevice: true
2022-03-30 17:19:22.467 Status: dzVents: device> _adapters:
2022-03-30 17:19:22.467 Status: dzVents: device> 1: Solar radiation device adapter
2022-03-30 17:19:22.467 Status: dzVents: device> switchTypeValue: 0
2022-03-30 17:19:22.467 Status: dzVents: device> deviceSubType: Solar Radiation
2022-03-30 17:19:22.467 Status: dzVents: device> isSecurity: false
2022-03-30 17:19:22.467 Status: dzVents: device> sValue: 130
2022-03-30 17:19:22.467 Status: dzVents: device> hardwareType: Modbus RTU / ASCII / TCP/IP - READ v2021.7
2022-03-30 17:19:22.467 Status: dzVents: device> changed: true
2022-03-30 17:19:22.467 Status: dzVents: device> description:
2022-03-30 17:19:22.467 Status: dzVents: device> hardwareTypeValue: 94
2022-03-30 17:19:22.467 Status: dzVents: device> protected: false
2022-03-30 17:19:22.467 Status: dzVents: device> _state: 130
2022-03-30 17:19:22.467 Status: dzVents: device> customImage: 0
2022-03-30 17:19:22.467 Status: dzVents: device> idx: 610
2022-03-30 17:19:22.467 Status: dzVents: device> hardwareID: 43
2022-03-30 17:19:22.467 Status: dzVents: device> isVariable: false
2022-03-30 17:19:22.467 Status: dzVents: device> id: 610
2022-03-30 17:19:22.467 Status: dzVents: device> deviceType: General
2022-03-30 17:19:22.468 Status: dzVents: device> lastUpdate: 2022-03-30 17:19:12
2022-03-30 17:19:22.468 Status: dzVents: device> active: false
2022-03-30 17:19:22.468 Status: dzVents: device> baseType: device
2022-03-30 17:19:22.468 Status: dzVents: device> rawData:
2022-03-30 17:19:22.468 Status: dzVents: device> 1: 130
2022-03-30 17:19:22.468 Status: dzVents: device> unit: 1
2022-03-30 17:19:22.468 Status: dzVents: device> deviceId: 002B0001
2022-03-30 17:19:22.468 Status: dzVents: device> nValue: 1
2022-03-30 17:19:22.468 Status: dzVents: device> isHardware: false
2022-03-30 17:19:22.468 Status: dzVents: device> switchType: On/Off
2022-03-30 17:19:22.468 Status: dzVents: device> _data:
2022-03-30 17:19:22.468 Status: dzVents: device> deviceType: General
2022-03-30 17:19:22.468 Status: dzVents: device> signalLevel: 12
2022-03-30 17:19:22.468 Status: dzVents: device> image:
2022-03-30 17:19:22.468 Status: dzVents: device> switchTypeValue: 0
2022-03-30 17:19:22.468 Status: dzVents: device> batteryLevel: 255
2022-03-30 17:19:22.468 Status: dzVents: device> lastUpdate: 2022-03-30 17:19:12
2022-03-30 17:19:22.468 Status: dzVents: device> lastLevel: 0
2022-03-30 17:19:22.468 Status: dzVents: device> description:
2022-03-30 17:19:22.468 Status: dzVents: device> subType: Solar Radiation
2022-03-30 17:19:22.468 Status: dzVents: device> name: Test Total solar radiation
2022-03-30 17:19:22.468 Status: dzVents: device> baseType: device
2022-03-30 17:19:22.468 Status: dzVents: device> protected: false
2022-03-30 17:19:22.468 Status: dzVents: device> data:
2022-03-30 17:19:22.468 Status: dzVents: device> hardwareType: Modbus RTU / ASCII / TCP/IP - READ v2021.7
2022-03-30 17:19:22.468 Status: dzVents: device> protected: false
2022-03-30 17:19:22.468 Status: dzVents: device> hardwareName: Test Total solar radiation
2022-03-30 17:19:22.468 Status: dzVents: device> hardwareID: 43
2022-03-30 17:19:22.468 Status: dzVents: device> icon: radiation
2022-03-30 17:19:22.468 Status: dzVents: device> _state: 130
2022-03-30 17:19:22.468 Status: dzVents: device> unit: 1
2022-03-30 17:19:22.468 Status: dzVents: device> customImage: 0
2022-03-30 17:19:22.468 Status: dzVents: device> radiation: 130.0
2022-03-30 17:19:22.468 Status: dzVents: device> _nValue: 1
2022-03-30 17:19:22.468 Status: dzVents: device> hardwareTypeValue: 94
2022-03-30 17:19:22.468 Status: dzVents: device> deviceID: 002B0001
2022-03-30 17:19:22.468 Status: dzVents: device> switchType: On/Off
2022-03-30 17:19:22.468 Status: dzVents: device> rawData:
2022-03-30 17:19:22.468 Status: dzVents: device> 1: 130
2022-03-30 17:19:22.468 Status: dzVents: device> iconNumber: 0
2022-03-30 17:19:22.468 Status: dzVents: device> timedOut: false
2022-03-30 17:19:22.469 Status: dzVents: device> changed: true
2022-03-30 17:19:22.469 Status: dzVents: device> id: 610
2022-03-30 17:19:22.469 Status: dzVents: device> inActive: true
2022-03-30 17:19:22.469 Status: dzVents: device> icon: radiation
2022-03-30 17:19:22.469 Status: dzVents: device> isHTTPResponse: false
2022-03-30 17:19:22.469 Status: dzVents: device> isGroup: false
2022-03-30 17:19:22.469 Status: dzVents: device> hardwareId: 43
2022-03-30 17:19:22.469 Status: dzVents: device> name: Test Total solar radiation
2022-03-30 17:19:22.469 Status: dzVents: device> timedOut: false
2022-03-30 17:19:22.469 Status: dzVents: device> bState: false
2022-03-30 17:19:22.469 Status: dzVents: Info: ------ Finished Sun Power SEM228T.lua
Looks like it should be radiation, but it doesn't work. So i think somehow sValue should first be tranformd to number. But how do I do this best in this script?
User avatar
waltervl
Posts: 5990
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2025.1
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by waltervl »

I did some testing and when I use a temperature device it is working for me.
So please try this script.

Code: Select all

return
{
    
	on = {
        devices = {'Sun Power SEM228T'}
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'SEM228T',
    },

    data =
    {
        SEM228THistory = { history = true, maxMinutes = 10 },
    },

    execute = function(domoticz, sensor)

         -- add new data
        domoticz.data.SEM228THistory.add(sensor.radiation)

        -- average
        local RadAverage = domoticz.data.SEM228THistory.avg()
        domoticz.log('average radiation ' .. RadAverage, domoticz.LOG_INFO)
       
    end
}
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
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

Thanks, I will test tomorrow when the Sun is shinning. :roll: :lol:
User avatar
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

@waltervl: Tested your script today. Unfortunatly.............

Code: Select all

2022-03-31 11:49:06.785 Error: dzVents: Error: (3.1.7) SEM228T: Item data is not a number type. Type is nil
2022-03-31 11:49:06.785 Error: dzVents: Error: (3.1.7) SEM228T: An error occurred when calling event handler SEM228T10M
2022-03-31 11:49:06.785 Error: dzVents: Error: (3.1.7) SEM228T: /home/pi/domoticz/dzVents/runtime/HistoricalStorage.lua:289: attempt to perform arithmetic on a nil value (local 'sum')
User avatar
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

i also tried this:

Code: Select all

return
{
    
	on = {
        devices = {'Test Total solar radiation'}
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'SEM228T',
    },

    data =
    {
        SEM228THistory = { history = true, maxMinutes = 10,getValue = function(item) return item.data.radiation end },
    },

    execute = function(domoticz, sensor)

         -- add new data
        domoticz.data.SEM228THistory.add(sensor.radiation)

        -- average
        local RadAverage = domoticz.data.SEM228THistory.avg()
        domoticz.log('average radiation ' .. RadAverage, domoticz.LOG_INFO)
       
    end
}
where 'Test Total solar radiation' is the original sensor from the Modbus plugin.
Then I get this error:

Code: Select all

2022-03-31 13:46:46.350 Error: dzVents: Error: (3.1.7) SEM228T: getData function returned an error. /home/pi/domoticz/scripts/dzVents/scripts/SEM228T10M.lua:16: attempt to index a number value (field 'data')
2022-03-31 13:46:46.350 Error: dzVents: Error: (3.1.7) SEM228T: An error occurred when calling event handler SEM228T10M
2022-03-31 13:46:46.350 Error: dzVents: Error: (3.1.7) SEM228T: /home/pi/domoticz/dzVents/runtime/HistoricalStorage.lua:289: attempt to perform arithmetic on a nil value (local 'sum')
So still nil value.
Because I couldn't send the value that came out of that to influxdb, I created a second general costum sensor where I read the value with the script below.

Code: Select all

return
{
   active = true,
   on = {
      	    devices =
	    { 
	    'Test Total solar radiation',
                --{'between 30 minutes before sunrise and 35 minutes after sunset'},
	    },
	},
   execute = function(domoticz, Sun_Power_SEM228T)
         local SunPowerWatts = domoticz.devices('Test Total solar radiation').sValue
      
	domoticz.devices('Sun Power SEM228T').updateCustomSensor(SunPowerWatts)
	--domoticz.devices('Test Total solar radiation').dump()
   end

}
That goes with the sValue of 'Test Total solar radiation'. That is a string and not a number. That's why I think it works with temperature (number) and not (yet) with my sensor (string). I just don't know how to turn that string into a value that the Statistical function can use (number).
User avatar
Copitano
Posts: 50
Joined: Friday 28 June 2019 1:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: NL
Contact:

Re: dzVents calculate average of 60 readings

Post by Copitano »

SOLVED!!!

Code: Select all

return
{
    
	on = {
        devices = {'Test Total solar radiation'}
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'SEM228T',
    },

    data =
    {
        SEM228THistory = { history = true, maxMinutes = 10},
    },

    execute = function(domoticz, sensor)

        -- add new data
        domoticz.data.SEM228THistory.add(sensor.radiation)

        -- average
        local RadAverage = domoticz.data.SEM228THistory.avg()
        domoticz.log('average radiation ' .. RadAverage, domoticz.LOG_INFO)
       
    end
}

Code: Select all

2022-03-31 14:23:56.303 Status: dzVents: Info: SEM228T: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-03-31 14:23:56.319 Status: dzVents: Info: SEM228T: average radiation 137.90196078431
2022-03-31 14:23:56.323 Status: dzVents: Info: SEM228T: ------ Finished SEM228T10M.lua
@waltervl: thanks for thinking along. Your version was pretty close ;) .
Now part two: ensuring that only 1 x every 10 minutes the average value is sent to a domoticz sensor.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest