simple multiplication  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

simple multiplication

Post by EdddieN »

Hello,

I think is one of those days that I'm missing something very obvious... what I'm doing wrong?

Code: Select all

--[[
Update OIL
]]

return {
    on = {

        -- timer riggers
        timer = {
            'every 1 minutes'
        }
    },

    -- custom logging level for this script
    logging = {
       level = domoticz.LOG_DEBUG,
       marker = "Update OIL"
   },
   
   execute = function(domoticz)
   -- domoticz.devices
       domoticz.log('Running OIL updater...', domoticz.LOG_INFO)

            local oilCounter = domoticz.devices('Oil tank')

            local oil = domoticz.devices('AIN1')   -- oil sensor (machinon analogue signal)
            local oilconversion = tonumber(oil) * 14  -- convert to Litres
           
            if oilconversion ~= oilCounter.value then                     
                dz.log("OilCounter updated to " .. oilconversion ,dz.LOG_DEBUG)
                oilCounter.updateCustomSensor(oilconversion)
            else
                dz.log("No update of oilCounter necessary. It is still " .. oil ,dz.LOG_DEBUG)
            end
        
        end
    
}
This results on
019-08-10 20:28:00.180 Status: dzVents: Error (2.4.10): Update OIL: ...omoticz/scripts/dzVents/generated_scripts/Oil sensor.lua:27: attempt to perform arithmetic on a nil value
11101101 - www.machinon.com
Thorgal789
Posts: 850
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: simple multiplication

Post by Thorgal789 »

line 27 > local oilconversion = tonumber(oil) * 14 -- convert to Litres
print the oil value to debug, I think the value is "nil"
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: simple multiplication

Post by hoeby »

What gives 'AIN1' for value?
The script says it goes wrong in line 27. But line 27 starts at line 26 with making the local oil. When 'AIN1' is nil, than line 27 will also be nill

Also change dz.log into domoticz.log

Or change everything to dz instead of domoticz.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

I'm very very rusty... got lazy and most of my scripts are in Blockly.

Ok, this is what I have now:

Code: Select all

--[[
Update OIL
]]

return {
    on = {

        -- timer riggers
        timer = {
            'every 1 minutes'
        }
    },

    -- custom logging level for this script
    logging = {
       level = domoticz.LOG_DEBUG,
       marker = "Update OIL"
   },
   
   execute = function(domoticz, devices)
   -- domoticz.devices
       domoticz.log('Running OIL updater...', domoticz.LOG_INFO)

            local oilCounter = domoticz.devices('Oil tank')

            local oil = domoticz.devices('AIN1')   -- oil sensor (machinon analogue signal)
                domoticz.log("SENSOR value is " .. oil)
            local oilconversion = tonumber(oil) * 14  -- convert to Litres
           
            if oilconversion ~= oilCounter.value then                     
                domoticz.log("OilCounter updated to " .. oilconversion)
                oilCounter.updateCustomSensor(oilconversion)
            else
                domoticz.log("No update of oilCounter necessary. It is still " .. oil)
            end
        
        end
    
}
and still gives me
2019-08-10 21:02:00.252 Status: dzVents: Info: Update OIL: ------ Start internal script: Oil sensor:, trigger: every 1 minutes
2019-08-10 21:02:00.252 Status: dzVents: Info: Update OIL: Running OIL updater...
2019-08-10 21:02:00.270 Status: dzVents: Debug: Update OIL: Processing device-adapter for Oil tank: Custom sensor device adapter
2019-08-10 21:02:00.271 Status: dzVents: Debug: Update OIL: Processing device-adapter for AIN1: Voltage device adapter
2019-08-10 21:02:00.271 Status: dzVents: Error (2.4.10): Update OIL: An error occured when calling event handler Oil sensor
2019-08-10 21:02:00.271 Status: dzVents: Error (2.4.10): Update OIL: ...omoticz/scripts/dzVents/generated_scripts/Oil sensor.lua:27: attempt to concatenate local 'oil' (a table value)
2019-08-10 21:02:00.271 Status: dzVents: Info: Update OIL: ------ Finished Oil sensor
cannot even see the value of AIN1, which in domoticz is 59 volts
11101101 - www.machinon.com
Thorgal789
Posts: 850
Joined: Wednesday 15 August 2018 14:38
Target OS: -
Domoticz version:
Contact:

Re: simple multiplication

Post by Thorgal789 »

local oil = domoticz.devices('AIN1') can't be a value (and a number), it's a table

Try
local oil = domoticz.devices('AIN1').temperature

With replacing "temperature" by the filed you need (Sorry I have never do dzevent, so hard for me to explain ^^)
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

Same results. If LUA is raise, happy to use lua.
11101101 - www.machinon.com
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: simple multiplication

Post by hoeby »

Try this.
I tested the part oil*14 in my domoticz and it gives a value 14x bigger than oil.
I can't run the complete script, because i don't have those devices.

Also look that i added .state in local oilCounter
And removed .value in the IF

Code: Select all

--[[
Update OIL
]]

return {
    on = {

        -- timer riggers
        timer = {
            'every 1 minutes'
        }
    },

    -- custom logging level for this script
    logging = {
       level = domoticz.LOG_DEBUG,
       marker = "Update OIL"
   },
   
   execute = function(domoticz, devices)
   -- domoticz.devices
       domoticz.log('Running OIL updater...', domoticz.LOG_INFO)

            local oilCounter = domoticz.devices('Oil tank').state

            local oil = domoticz.devices('AIN1').state   -- oil sensor (machinon analogue signal)
                domoticz.log("SENSOR value is " .. oil)
            local oilconversion = oil * 14  -- convert to Litres
           
            if oilconversion ~= oilCounter then                     
                domoticz.log("OilCounter updated to " .. oilconversion)
                oilCounter.updateCustomSensor(oilconversion)
            else
                domoticz.log("No update of oilCounter necessary. It is still " .. oil)
            end
        
        end
    
}
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

Getting somewhere now!

Code: Select all

 2019-08-11 09:10:00.296 Status: dzVents: Info: Update OIL: ------ Start internal script: Oil sensor:, trigger: every 1 minutes
2019-08-11 09:10:00.297 Status: dzVents: Info: Update OIL: Running OIL updater...
2019-08-11 09:10:00.332 Status: dzVents: Debug: Update OIL: Processing device-adapter for Oil tank: Custom sensor device adapter
2019-08-11 09:10:00.334 Status: dzVents: Debug: Update OIL: Processing device-adapter for AIN1: Voltage device adapter
2019-08-11 09:10:00.334 Status: dzVents: Info: Update OIL: SENSOR value is 59.687
2019-08-11 09:10:00.334 Status: dzVents: Info: Update OIL: OilCounter updated to 835.618
2019-08-11 09:10:00.334 Status: dzVents: Error (2.4.10): Update OIL: An error occured when calling event handler Oil sensor
2019-08-11 09:10:00.334 Status: dzVents: Error (2.4.10): Update OIL: ...omoticz/scripts/dzVents/generated_scripts/Oil sensor.lua:32: attempt to call field 'updateCustomSensor' (a nil value)
2019-08-11 09:10:00.334 Status: dzVents: Info: Update OIL: ------ Finished Oil sensor
Updating the counter with the new value is what it fails now.
11101101 - www.machinon.com
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

Ok, finally it worked! Thanks so much!!!

Here it is the code

Code: Select all

 --[[
Update OIL
]]

return {
    on = {

        -- timer riggers
        timer = {
            'every 1 minutes'
        }
    },

    -- custom logging level for this script
    logging = {
       level = domoticz.LOG_DEBUG,
       marker = "Update OIL"
   },
   
   execute = function(domoticz, devices)
   -- domoticz.devices
       domoticz.log('Running OIL updater...', domoticz.LOG_INFO)

            local oilCounter = domoticz.devices('Oil tank')

            local oil = domoticz.devices('AIN1').state   -- oil sensor (machinon analogue signal)
                domoticz.log("SENSOR value is " .. oil)
            local oilconversion = tonumber(oil) * 14  -- convert to Litres
           
            if oilconversion ~= oilCounter.value then                     
                domoticz.log("OilCounter updated to " .. oilconversion)
                oilCounter.updateCustomSensor(oilconversion)
            else
                domoticz.log("No update of oilCounter necessary. It is still " .. oil)
            end
        
        end
    
}
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: simple multiplication

Post by waaren »


EdddieN wrote:Ok, finally it worked! Thanks so much!!!

Code: Select all

                domoticz.log("No update of oilCounter necessary. It is still " .. oil)
I guess the oil in this line also needs to be changed to oilconversion?


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

Yes, and also need to find out to remove the decimals, now I get something like '834.234L' which is far too much resolution. Any suggestions?
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: simple multiplication

Post by waaren »

EdddieN wrote:Yes, and also need to find out to remove the decimals, now I get something like '834.234L' which is far too much resolution. Any suggestions?
Yes use domoticz.utils.round(your number var,1)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication

Post by EdddieN »

Like this?

local oilconversion = tonumber(oil) * 14 -- convert to Litres
oilconversion = dz.utils.round(oilconversion,1)
11101101 - www.machinon.com
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: simple multiplication

Post by waaren »

EdddieN wrote:Like this?

local oilconversion = tonumber(oil) * 14 -- convert to Litres
oilconversion = dz.utils.round(oilconversion,1)
Yes but in your script you need to change dz to domoticz because that is the name of your parm containing the domoticz object.
I use dz as the name of this object in most of my scripts (too lazy to type the full name every timeImage)




Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
EdddieN
Posts: 510
Joined: Wednesday 16 November 2016 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: Scotland
Contact:

Re: simple multiplication  [Solved]

Post by EdddieN »

Ah! I was wondering what dz, domoticz difference it was... thanks. That did the trick!

Code: Select all

--[[
Update OIL
]]

return {
    on = {

        -- timer riggers
        timer = {
            'every 1 minutes'
        }
    },

    -- custom logging level for this script
    logging = {
       level = domoticz.LOG_DEBUG,
       marker = "Update OIL"
   },
   
   execute = function(domoticz, devices)
   -- domoticz.devices
       domoticz.log('Running OIL updater...', domoticz.LOG_INFO)

            local oilCounter = domoticz.devices('Oil tank')

            local oil = domoticz.devices('AIN1').state   -- oil sensor (machinon analogue signal)
                domoticz.log("SENSOR value is " .. oil)
            local oilconversion = tonumber(oil) * 14  -- convert to Litres
            oilconversion = domoticz.utils.round(oilconversion,1)
           
            if oilconversion1 ~= oilCounter.value then                     
                domoticz.log("OilCounter updated to " .. oilconversion)
                oilCounter.updateCustomSensor(oilconversion)
            else
                domoticz.log("No update of oilCounter necessary. It is still " .. oilconversion)
            end
        
        end
    
}
11101101 - www.machinon.com
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest