Page 1 of 1

DzVents if then else returns an error or value 0

Posted: Tuesday 08 March 2022 17:16
by Copitano
Who helps? I am out of options :roll:
I bought a pyranometer (measures global radiation from the sun).
https://tweakers.net/i/Ut0QbXAs-PH2fCc1 ... lbum_large
The data is retrieved in domoticz via Modbus RTU in Watt/m². With a dzVents script I convert that to Joules/cm². That gives the global radiation per day. I can then test this against an official KNMI station 5 km away as the crow flies.
After a few months of testing, my pyranometer on average seems to indicate about 20% more than the official KNMI station. That is also the case when I compare it with 5 other KNMI stations within a radius of approximately 30 km. The thing comes from China and is therefore a bit expensive to send it back to have it recalibrated. So I want to do that with software. I have already worked out the formula for this.
I'm trying to do that in domoticz with this script:

Code: Select all

return

   active = true,
   on = {
            devices =
      
        [ 'Sun Power SEM228T' ] =
                {'between 30 minutes before sunrise and 35 minutes after sunset'},
        
  
   execute = function(domoticz, Sun_Power_SEM228T)

    if domoticz.devices('Sun Power SEM228T').sValue >= 38 then
       local SunPowerWatts50 = domoticz.devices('Sun Power SEM228T').sValue - 38
    else
       local SunPowerWatts50 = domoticz.devices('Sun Power SEM228T').sValue
        end
       domoticz.devices('Sun Power SEM228T 50').updateCustomSensor(SunPowerWatts50)
   end

What the script has to do is check whether the value of Sun Power SEM228T is above or equal to 38 Watt/m² and then subtract 38 Watt/m² form Sun Power SEM228T. If the value of 38 Watt/m² is not exceeded, the value of Sun Power SEM228T is adopted. The result then appears in Sun Power SEM228T 50.

The problem is that I keep getting this error message:

Code: Select all

2022-03-07 16:28:07.307 Error: dzVents: Error: (3.1.7) An error occurred when calling event handler Sun Power SEM228T 50
2022-03-07 16:28:07.307 Error: dzVents: Error: (3.1.7) ...omoticz/scripts/dzVents/scripts/Sun Power SEM228T 50.lua:13: attempt to compare number with string
Or if I use the line:

Code: Select all

domoticz.devices('Sun Power SEM228T').sValue >= 38
between brackets and/or multiply the 38 by 1, I no longer get an error message, but the value 0 while it should be (much) higher.
As soon as I comment out the if then else end with -- the line works

Code: Select all

       local SunPowerWatts50 = domoticz.devices('Sun Power SEM228T').sValue - 38
fine. sValue is then apparently recognized as a number, but then I no longer have a test whether the original value is higher than 38 Watt/m²

What I've already tried is to replace sValue with: state; _state; raw; rawData and sensorValue. But even then I keep getting the error that an attempt is being made to compare a string with a number.

Also tried this:

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 SunPower50 = tonumber(domoticz.devices('Test Total solar radiation').sValue)
    if SunPower50 >= 38.0 then
       local SunPowerWatts50 = SunPower50 - 38
    else
       local SunPowerWatts50 = SunPower50
    end
       domoticz.devices('Sun Power SEM228T 50').updateCustomSensor(SunPowerWatts50)
   end
}
Test Total solar radiation is the original device in which the value is stored directly from MODBUS RTU. In order to be able to continue to influxdb, this had to be done via a costum sensor Sun Power SEM228T that is in the script for this.
It does not give an error message anymore, but a value of 0

What am I doing wrong here? And how do I solve it?

Re: DzVents if then else returns an error or value 0

Posted: Tuesday 08 March 2022 17:49
by boum
Local variable SunPowerWatts50 is not visible outside the then (and else) block, you must move the declaration outside

Code: Select all

    local SunPowerWatts50
    if SunPower50 >= 38.0 then
       SunPowerWatts50 = SunPower50 - 38
    else
       SunPowerWatts50 = SunPower50
    end

Re: DzVents if then else returns an error or value 0

Posted: Tuesday 08 March 2022 21:19
by Copitano
@boum
Thanks! Looks like this is working :D . it's always the stupid little things :lol: .

Code: Select all

return
{
   active = true,
   on = {
            devices =
        { 
        [ 'Test Total solar radiation' ] =
                {'between 30 minutes before sunrise and 235 minutes after sunset'},
        },
    },
   execute = function(domoticz, Sun_Power_SEM228T)
        local SunPower50 = tonumber(domoticz.devices('Test Total solar radiation').sValue)
	local SunPowerWatts50
    if SunPower50 >= 170.0 then
        SunPowerWatts50 = SunPower50 - 44
    else
        SunPowerWatts50 = SunPower50
    end
       domoticz.devices('Sun Power SEM228T 50').updateCustomSensor(SunPowerWatts50)
    --domoticz.devices('Sun Power SEM228T 50').dump()
   end
}