Page 1 of 1

Multiply value

Posted: Tuesday 10 October 2023 22:41
by Lillios
Hey,
I have an old LUA scripts that was used to multiply a value from a sensor with 50.
The actual sensor is an Co2 sensor, but Telldus reads the value as a temperature.

I have recently setup my old installation, and i can not remember how i got this working, but now i get error in Domoticz log:
Error: EventSystem: in Script #1: [string "--Domoticz LUA Script (put text in file scrip..."]:12: attempt to index a nil value (global 'devicechanged')

Any ideas?
Or how to make this in DzVents?

Code: Select all


--Name of the real CO2 meter
aqMeterName = 'WMS-Co2'

--ID of the created dummy CO2 meter
dummyAqMeterId = 41

aqVal = otherdevices_svalues[aqMeterName]

commandArray = {}
if devicechanged[aqMeterName] then
   --calculate actual value
   actual = 50 * tonumber(aqVal)
   
   --update dummy meter
   commandArray['UpdateDevice'] = dummyAqMeterId .. "|" .. actual .. "|0" 
end

return commandArray

Re: Multiply value

Posted: Tuesday 10 October 2023 23:12
by waltervl
I think you mixed something up in your event script.
What is script named script#1 ? Is that the script you want to run? Or is it some empty script you need to delete? The error is coming from this script.

Re: Multiply value

Posted: Tuesday 10 October 2023 23:16
by Lillios
waltervl wrote: Tuesday 10 October 2023 23:12 I think you mixed something up in your event script.
What is script named script#1 ? Is that the script you want to run? Or is it some empty script you need to delete? The error is coming from this script.
It is the script i want to run, (renamed it to Co2)

Re: Multiply value

Posted: Tuesday 10 October 2023 23:58
by RonkA
Not tested but is this something?

Code: Select all

return {
    logging = {
        level = domoticz.LOG_INFO, -- Adjust the log level as needed
        marker = 'CO2 Script'
    },
    
    on = {
        devices = { 'WMS-Co2' } 
    },

    execute = function(dz)
        local aqMeterName = 'WMS-Co2'  -- Name of the real CO2 meter that is a temperature device
        
        local dummyAqMeterId = 41 -- ID of the dummy that is a Air quality device
        
        local aqVal = dz.devices(aqMeterName).temperature  -- read the value from sensor

        local commandArray = {}
        local actual = 0

        if dz.changedDevices[aqMeterName] then
            -- Calculate actual value
            actual = 50 * tonumber(aqVal)

            -- Log the calculated value
            dz.log("Actual CO2 value: " .. actual, domoticz.LOG_INFO)

            -- Update dummy meter
        dz.devices(41).updateAirQuality(actual)
        end

        return commandArray
    }
}

Re: Multiply value

Posted: Wednesday 11 October 2023 0:08
by boum
RonkA wrote: Tuesday 10 October 2023 23:58 Not tested but is this something?
This script has zero chance to work.

Re: Multiply value

Posted: Wednesday 11 October 2023 16:22
by Lillios
boum wrote: Wednesday 11 October 2023 0:08
RonkA wrote: Tuesday 10 October 2023 23:58 Not tested but is this something?
This script has zero chance to work.
What needs to be changed?
I dont have any knowledge reguarding scripting. Trying to learn, but its frustrating when its not working :o

Re: Multiply value

Posted: Thursday 12 October 2023 11:27
by boum
Your issue with devicechanged being nil is probably because you set your script to be triggered by "time" or "all" (basically every choice in the trigger drop-down except "device")
When the script is triggered every minute, there is no changed device at that point.

In my opinion, one advantage of dzVents is that the triggers are explicit in the script (in the "on" section)
Also do not use chatgpt to generate scripts (not targetting anyone — maybe —), the result might work but it's more like a lottery.

When learning, I understand the frustration. Come with your issues of the forum, but if you want good honest help, document what you've tried. Also, learn to use debug logging.

I have not tested the following dzVent script, but at least it's not complete bullshit:

Code: Select all

local aqMeterName = 'WMS-Co2'  -- Name of the real CO2 meter that is a temperature device
return {
    logging = {
        level = domoticz.LOG_INFO, -- Adjust the log level as needed
        marker = 'CO2 Script'
    },
    
    on = {
        devices = { aqMeterName } 
    },

    execute = function(dz, item)
        local actual = 50 * item.temperature
        dz.log("Actual CO2 value: " .. actual)

        local dummyAqMeterId = 41 -- ID of the dummy that is a Air quality device
        dz.devices(dummyAqMeterId).updateAirQuality(actual)
    end
}

Re: Multiply value

Posted: Thursday 12 October 2023 22:19
by Lillios
Thanks alot!
Unfortunately i cant get it to work.
Have set logging to Debug for Dzvent. And also changed your script to:
level = domoticz.LOG_DEBUG

But the log does not show anything. No errors, or no info.

I also tried to move "Line 1", as i thought it should be placed after "return {", and then i get an error in the Domoticz Log.
So the scripts seems to be running.


2023-10-12 22:17:56.398 Status: EventSystem: reset all events...
2023-10-12 22:17:56.399 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/CO2 Script.lua
2023-10-12 22:17:56.442 Status: dzVents: Debug: Dumping domoticz data to /home/pi/domoticz/scripts/dzVents/domoticzData.lua
2023-10-12 22:17:56.452 Status: dzVents: Debug: dzVents version: 3.1.8
2023-10-12 22:17:56.452 Status: dzVents: Debug: Event triggers:

Re: Multiply value

Posted: Friday 13 October 2023 0:25
by RonkA
Searching (Ctrl+F) on 'wiki/DzVents:_next_generation_Lua_scripting' for 'item.' i read:
  • Since you can define multiple on-triggers in your script, it is not always clear what the type is of this second parameter. In your code you need to know this in order to properly respond to the different events. To help you inspect the object you can use these attributes like if (item.isDevice) then ... end:
So... is it maybe:

Code: Select all

        local actual = 50 * (item.temperature)

Re: Multiply value

Posted: Friday 13 October 2023 15:45
by boum
Lillios wrote: Thursday 12 October 2023 22:19 Have set logging to Debug for Dzvent. And also changed your script to:
level = domoticz.LOG_DEBUG

But the log does not show anything. No errors, or no info.
Once you've done that, you've got to pepper the script with dz.log calls to check how things are going. If you don't get any output, then the script is compiled, but never called. Does the "temperature" device really change? (or is it modified with the JSON API with parsetrigger?)

Code: Select all

    execute = function(dz, item)
        dz.log("Entering CO2 script update, from device changed: " .. item.name, dz.LOG_DEBUG)
        dz.log("CO2 meter temperature: " .. item.temperature, dz.LOG_DEBUG)

        local actual = 50 * item.temperature
        dz.log("Actual CO2 value: " .. actual, dz.LOG_INFO)

        local aqMeter = dz.devices(41) -- ID of the dummy that is a Air quality device
        dz.log("Air quality before update: " .. aqMeter.co2, dz.LOG_DEBUG)
        aqMeter.updateAirQuality(actual)
    end
do you see?

Re: Multiply value

Posted: Friday 13 October 2023 20:29
by Lillios
boum wrote: Friday 13 October 2023 15:45
Lillios wrote: Thursday 12 October 2023 22:19 Have set logging to Debug for Dzvent. And also changed your script to:
level = domoticz.LOG_DEBUG

But the log does not show anything. No errors, or no info.
Once you've done that, you've got to pepper the script with dz.log calls to check how things are going. If you don't get any output, then the script is compiled, but never called. Does the "temperature" device really change? (or is it modified with the JSON API with parsetrigger?)

Code: Select all

    execute = function(dz, item)
        dz.log("Entering CO2 script update, from device changed: " .. item.name, dz.LOG_DEBUG)
        dz.log("CO2 meter temperature: " .. item.temperature, dz.LOG_DEBUG)

        local actual = 50 * item.temperature
        dz.log("Actual CO2 value: " .. actual, dz.LOG_INFO)

        local aqMeter = dz.devices(41) -- ID of the dummy that is a Air quality device
        dz.log("Air quality before update: " .. aqMeter.co2, dz.LOG_DEBUG)
        aqMeter.updateAirQuality(actual)
    end
do you see?
Awesome, its working...!
At first it did not work, even if i breed on the Co2 sensor to get it to change the value of "VMS-Co2".
But then i unplugged the sensor, removed it from Domoticz, added it again and added it to "Temperature" page... and Baaaam its working :lol:


Big, big thank your for your help. I have put so many hours trying to get this to work!
Thanks again!

Re: Multiply value

Posted: Saturday 14 October 2023 14:45
by boum
👍