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

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

Moderator: leecollings

Post Reply
domogijs
Posts: 99
Joined: Monday 17 August 2015 23:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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
Raspberry Pi3, RFlink, PiZigate, Yeelight, klikaanklikuit, Kodi, harmony hub, Zwave, ThermoSmart, XiaomiGateway (Aqara), Google home hub, roomba,
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
domogijs
Posts: 99
Joined: Monday 17 August 2015 23:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by domogijs »

Thanks, i get a few errors, but i will try to solve it :)
Raspberry Pi3, RFlink, PiZigate, Yeelight, klikaanklikuit, Kodi, harmony hub, Zwave, ThermoSmart, XiaomiGateway (Aqara), Google home hub, roomba,
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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 :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
domogijs
Posts: 99
Joined: Monday 17 August 2015 23:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post by domogijs »

i dont understand this one; myLight.setRGB(255,0,0)

should i make a local variable for this one ore something?
Raspberry Pi3, RFlink, PiZigate, Yeelight, klikaanklikuit, Kodi, harmony hub, Zwave, ThermoSmart, XiaomiGateway (Aqara), Google home hub, roomba,
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
domogijs
Posts: 99
Joined: Monday 17 August 2015 23:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

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

Post 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)
Raspberry Pi3, RFlink, PiZigate, Yeelight, klikaanklikuit, Kodi, harmony hub, Zwave, ThermoSmart, XiaomiGateway (Aqara), Google home hub, roomba,
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

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

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest