Page 1 of 1

Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 10:59
by domogijs
Hi

I need some help with my script.

I have a co2 meter and if the value is to high i want a lamp to turn red for 2 seconds. This lamp can be already on (white) or off. After the 2 sec i want the lamp back on his original state.

I tried this with this script:

Code: Select all

return {
    on = { devices = { "CO2" }},
    
    execute = function(domoticz, device)
        Ikea = domoticz.devices(792)
        
        if  (domoticz.devices('CO2').co2 > 1250) then
            
            Ikea.setColorBrightness(255, 0, 0).forSec(2)
        
        end
    end
}

but i get this error

Code: Select all

2019-11-03 10:53:31.897 Status: setcolbrightnessvalue: ID: 318, bri: 100, color: '{m: 3, RGB: ff0000, CWWW: 0000, CT: 0}'
2019-11-03 10:53:31.880 Error: dzVents: Error: (2.4.29) An error occurred when calling event handler Co2 test
2019-11-03 10:53:31.880 Error: dzVents: Error: (2.4.29) .../domoticz/scripts/dzVents/generated_scripts/Co2 test.lua:10: attempt to call field 'forSec' (a nil value)
any ideas how to fix this?

Greets Gijs

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 11:32
by waaren
domogijs wrote: Sunday 03 November 2019 10:59 Hi

I need some help with my script.

I have a co2 meter and if the value is to high i want a lamp to turn red for 2 seconds. This lamp can be already on (white) or off. After the 2 sec i want the lamp back on his original state.
Below script could do the job. Not tested with Ikea bulbs but works with hue and other RGBW device types.

Code: Select all

return {
    on = { devices = { "CO2" }},
    
    execute = function(domoticz, device)
        Ikea = domoticz.devices(792)
         local myDelay = 2 -- seconds delay before restoring old color
           
        if  (domoticz.devices('CO2').co2 > 1250) then

           
            local function restoreColor(device, delay)
                local url = dz.settings['Domoticz url'] ..
                            '/json.htm?type=command&param=setcolbrightnessvalue' ..
                            '&idx=' .. device.id   .. '&brightness=' .. device.level .. '&color=' .. device.color

                dz.openURL(url).afterSec(delay)
                device.switchOn().afterSec(delay + 0.3 --Force store of device.color in domoticz database
                dz.log('Color will be restored to ' .. device.color .. '( brightness: ' ..device.level ..') after ' .. delay  .. ' seconds.',dz.LOG_DEBUG )
            end

            restoreColor(Ikea, myDelay)
            myLight.setRGB(255,0,0)
        
        end
    end
}


Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 13:18
by domogijs
Thanks, i get a few errors, but i will try to solve it :)

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 13:34
by waaren
domogijs wrote: Sunday 03 November 2019 13:18 Thanks, i get a few errors, but i will try to solve it :)
Majority of the errors will go away if you add

Code: Select all

local dz = domoticz
directly after the execute = line

I am too lazy to type domoticz that often :)

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 14:09
by domogijs
i dont understand this one; myLight.setRGB(255,0,0)

should i make a local variable for this one ore something?

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Sunday 03 November 2019 14:16
by waaren
domogijs wrote: Sunday 03 November 2019 14:09 i dont understand this one; myLight.setRGB(255,0,0)

should i make a local variable for this one ore something?
This was a left behind from my example.

Please try this one.

Code: Select all

return {
    on = { devices = { "CO2" }},
    
    execute = function(dz, item)
        local Ikea = dz.devices(792)
        local myDelay = 2 -- seconds delay before restoring old color
           
        if  item.co2 > 1250 then

            local function restoreColor(device, delay)
                local url = dz.settings['Domoticz url'] ..
                            '/json.htm?type=command&param=setcolbrightnessvalue' ..
                            '&idx=' .. device.id   .. '&brightness=' .. device.level .. '&color=' .. device.color

                dz.openURL(url).afterSec(delay)
                device.switchOn().afterSec(delay + 0.3 --Force store of device.color in domoticz database
                dz.log('Color will be restored to ' .. device.color .. '( brightness: ' ..device.level ..') after ' .. delay  .. ' seconds.',dz.LOG_DEBUG )
            end

            restoreColor(Ikea, myDelay)
            Ikea.setRGB(255,0,0)

        end
    end
}

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Monday 04 November 2019 23:20
by domogijs
Thanks, tried to make it work but bumbing from error to error (sorry; noob)

This is the status

Code: Select all

return {
    on = { devices = { "CO2" }},
    
     execute = function(domoticz, device)
        local dz = domoticz
        Ikea = domoticz.devices(792)
         local delay = 2 -- seconds delay before restoring old color
           
        if  (domoticz.devices('CO2').co2 > 500) then

            local function restoreColor(device, delay)
                local url = dz.settings['Domoticz url'] ..
                            '/json.htm?type=command&param=setcolbrightnessvalue' ..
                            '&idx=' .. device.id   .. '&brightness=' .. device.level .. '&color=' .. device.color

                dz.openURL(url).afterSec(delay)
                device.switchOn().afterSec(delay + 0.3) --Force store of device.color in domoticz database
                dz.log('Color will be restored to ' .. device.color .. '( brightness: ' ..device.level ..') after ' .. delay  .. ' seconds.',dz.LOG_DEBUG )
            end

            restoreColor(Ikea, delay)
            Ikea.setRGB(255,0,0)

        end
    end
}
gives error: Error: (2.4.29) ...e/pi/domoticz/scripts/dzVents/generated_scripts/CO22.lua:14: attempt to concatenate field 'level' (a nil value)

Re: Script help; rgb 255,0,0 on for 2 sec

Posted: Monday 04 November 2019 23:40
by waaren
domogijs wrote: Monday 04 November 2019 23:20 Thanks, tried to make it work but bumbing from error to error
Error: (2.4.29) ...e/pi/domoticz/scripts/dzVents/generated_scripts/CO22.lua:14: attempt to concatenate field 'level' (a nil value)
Suggest you start with this one and after every change check if it still works.

Code: Select all

return {
    on = { devices = { "CO2" }},
    
     execute = function(dz, item)
        Ikea = dz.devices(792)
        local delay = 2 -- seconds delay before restoring old color
           
        if item.co2 > 500 then

            local function restoreColor(device, delay)
                local url = dz.settings['Domoticz url'] ..
                            '/json.htm?type=command&param=setcolbrightnessvalue' ..
                            '&idx=' .. device.id   .. '&brightness=' .. device.level .. '&color=' .. device.color

                dz.openURL(url).afterSec(delay)
                device.switchOn().afterSec(delay + 0.3) --Force store of device.color in domoticz database
                dz.log('Color will be restored to ' .. device.color .. '( brightness: ' ..device.level ..') after ' .. delay  .. ' seconds.',dz.LOG_FORCE )
            end

            restoreColor(Ikea, delay)
            Ikea.setRGB(255,0,0)

        end
    end
}