Multiply value

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

Moderator: leecollings

Post Reply
Lillios
Posts: 8
Joined: Sunday 04 December 2016 22:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Multiply value

Post 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
Attachments
domoticz.png
domoticz.png (7.61 KiB) Viewed 1535 times
User avatar
waltervl
Posts: 5853
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Multiply value

Post 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.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Lillios
Posts: 8
Joined: Sunday 04 December 2016 22:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Multiply value

Post 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)
User avatar
RonkA
Posts: 115
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: Multiply value

Post 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
    }
}
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Multiply value

Post by boum »

RonkA wrote: Tuesday 10 October 2023 23:58 Not tested but is this something?
This script has zero chance to work.
Lillios
Posts: 8
Joined: Sunday 04 December 2016 22:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Multiply value

Post 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
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Multiply value

Post 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
}
Lillios
Posts: 8
Joined: Sunday 04 December 2016 22:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Multiply value

Post 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:
Attachments
Untitled11.png
Untitled11.png (6.52 KiB) Viewed 1414 times
User avatar
RonkA
Posts: 115
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: Multiply value

Post 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)
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Multiply value

Post 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?
Lillios
Posts: 8
Joined: Sunday 04 December 2016 22:07
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Multiply value

Post 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!
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Multiply value

Post by boum »

👍
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest