first steps

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

Moderator: leecollings

Post Reply
bing69
Posts: 139
Joined: Thursday 05 June 2014 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: 's-Hertogenbosch, NL
Contact:

first steps

Post by bing69 »

Just started the first steps in dzvents. Have many switches with a space in the name. Can I use that? Get an error every time.
In the example in the Wiki there is something I do not understand, i see Room switch and roomSwitch is that right?


return {
active = true,
on = {
devices = {
'Room switch'
}
},
execute = function(domoticz, roomSwitch)
if (roomSwitch.state == 'On' and domoticz.devices('Living room').temperature > 18) then
domoticz.devices('Another switch').switchOn()
domoticz.notify('This rocks!',
'Turns out that it is getting warm here',
domoticz.PRIORITY_LOW)
end
end
}
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: first steps

Post by tlpeter »

At the devices you need to enter the device name which you are going to do something with.
The function of just a name i think.
If you put in domoticz. Abc then you need to use Abc for the rest of the script like Abc.state this can be different than the switch name in domoticz.
bing69
Posts: 139
Joined: Thursday 05 June 2014 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: 's-Hertogenbosch, NL
Contact:

Re: first steps

Post by bing69 »

tlpeter wrote:At the devices you need to enter the device name which you are going to do something with.
The function of just a name i think.
If you put in domoticz. Abc then you need to use Abc for the rest of the script like Abc.state this can be different than the switch name in domoticz.
Thanks, i give 't a try tomorrow!
hekm77
Posts: 45
Joined: Thursday 09 February 2017 18:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: first steps

Post by hekm77 »

Hi. I'm newby in dzVents too.
How can I use domoticz.devices().forEach(function() .. end) instead of for n, Lamp in pairs(Lamps) do .. end in my script?

Code: Select all

return {
    active = true, 
    on = {
        devices = {
            'PIR' 
        } 
    },
    logging = {
        level = domoticz.LOG_ERROR 
    },
    execute = function(domoticz, switch)
        
         local Lamps  = {'Light-1', 'Light-2'}
         
        if (switch.state == 'On') then
               for n, Lamp in pairs(Lamps) do
                       if (domoticz.time.matchesRule('at 06:00-08:00')) then
                           domoticz.devices(Lamp).dimTo(10)
                       end
               end
        end
    end
}
Thanks
bing69
Posts: 139
Joined: Thursday 05 June 2014 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: 's-Hertogenbosch, NL
Contact:

Re: first steps

Post by bing69 »

Yes!! the first (simple) script works.

Code: Select all

 return {
    active = true,
    on = {
        devices = {
            'Deur Berging',
            'Deur berging buiten'
        }
    },
    execute = function(domoticz, DeurBerging)
            local DeurBergingBuiten = domoticz.devices('Deur berging buiten')
            local LampBerging = domoticz.devices('Lamp berging')
        if (DeurBerging.state == 'Open') or (DeurBergingBuiten.state == 'Open') then
            LampBerging.switchOn()
        else
            LampBerging.switchOff().afterSec(15)
        end   
    end
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: first steps

Post by dannybloe »

The documentation should give you examples in how to use the forEach() function. Isn't that helping you?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
hekm77
Posts: 45
Joined: Thursday 09 February 2017 18:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: first steps

Post by hekm77 »

dannybloe wrote:The documentation should give you examples in how to use the forEach() function. Isn't that helping you?
Not yet, unfortunately. Can you show in my script example?
Thanks
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: first steps

Post by dannybloe »

Well, in your case you want to loop over your array of lamp names and that is just a standard lua table/array so you loop is perfectly suited for that. dzVents forEach is if want to loop over all devices or over a set of filtered devices (or over any of the dzVents collections). But in your situstion that is not the case. So I wouldn't change your code.

What I would change is restrict your code differently to the time-frame you wish your PIR to be active. Now you check the current time inside the loop over and over again while all you need to do is:

Code: Select all

devices = { ['PIR'] = 'at 06:00-08:00' }
And remove the check in your execute function.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
hekm77
Posts: 45
Joined: Thursday 09 February 2017 18:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: first steps

Post by hekm77 »

dannybloe wrote:Well, in your case you want to loop over your array of lamp names and that is just a standard lua table/array so you loop is perfectly suited for that. dzVents forEach is if want to loop over all devices or over a set of filtered devices (or over any of the dzVents collections). But in your situstion that is not the case. So I wouldn't change your code.

What I would change is restrict your code differently to the time-frame you wish your PIR to be active. Now you check the current time inside the loop over and over again while all you need to do is:

Code: Select all

devices = { ['PIR'] = 'at 06:00-08:00' }
And remove the check in your execute function.
Means in my case all is correct.
I use "domoticz.time.matchesRule" function as a condition for turn on a group of lamps at different times with different brightness.
Special thanks for the dzVents!
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: first steps

Post by dannybloe »

I don't agree. Your if is executed for all lamps in your array. There is no need to evaluate the rule inside your loop as the result is the same for every loop iteration. That's why you can remove it from the loop and do it before the loop. If it evaluates to false there is no need for your loop in the first place. And moving it to the on-section is even better as it won't end up in your log as well as dzVents won't even call your execute function if it doesn't match the time rule.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
hekm77
Posts: 45
Joined: Thursday 09 February 2017 18:31
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Contact:

Re: first steps

Post by hekm77 »

dannybloe wrote:I don't agree. Your if is executed for all lamps in your array. There is no need to evaluate the rule inside your loop as the result is the same for every loop iteration. That's why you can remove it from the loop and do it before the loop. If it evaluates to false there is no need for your loop in the first place. And moving it to the on-section is even better as it won't end up in your log as well as dzVents won't even call your execute function if it doesn't match the time rule.
I'm sorry. That was part of the script
Spoiler: show

Code: Select all

return {
    active = true, 
    on = {
        devices = {
            'Холл-Движение' 
        } 
    },
    logging = {
        level = domoticz.LOG_ERROR 
    },
    execute = function(domoticz, switch)
        
         local DummyDevice = domoticz.devices('Холл-Dummy') 
         local WhiteLamp1  = domoticz.devices('Холл-1')
         local WhiteLamp2  = domoticz.devices('Холл-2') 
         local RGBGateway  = domoticz.devices('miШлюз RGB')
         local WhiteLamps  = {'Холл-1', 'Холл-2'}
         local url = "http://user:[email protected]:8080/json.htm?type=command&param=setcolbrightnessvalue&idx=97&hue=36&brightness=5&iswhite=false"
         
        if (switch.state == 'On') then
               if ((WhiteLamp1.state == 'Off' and WhiteLamp2.state == 'Off') and DummyDevice.state == 'On') then
                    for n, WhiteLamp in pairs(WhiteLamps) do
                       if (domoticz.time.matchesRule('at 06:00-08:00 on mon, tue, wed, thu, fri')) then
                           domoticz.devices(WhiteLamp).dimTo(10)
                       end
                       if (domoticz.time.matchesRule('at 08:00-22:00 on mon, tue, wed, thu, fri')) then
                            domoticz.devices(WhiteLamp).dimTo(100)
                       end
                       if (domoticz.time.matchesRule('at 22:00-23:30')) then
                            domoticz.devices(WhiteLamp).dimTo(10)
                       end
                       if (domoticz.time.matchesRule('at 23:30-00:00')) then
                            domoticz.devices(WhiteLamp).dimTo(1)
                       end
                       if (domoticz.time.matchesRule('at 06:00-10:00 on sat, sun')) then
                            domoticz.devices(WhiteLamp).dimTo(1)
                       end
                       if (domoticz.time.matchesRule('at 10:00-22:00 on sat, sun')) then
                            domoticz.devices(WhiteLamp).dimTo(100)
                       end
                    end
                end
                if (RGBGateway.state == 'Off') then
                       if (domoticz.time.matchesRule('at 00:00-06:00')) then
                           NightMode = 'curl -s '..'"'..url..'"'..' &'
                           os.execute(NightMode)
                       end
                end              
        end
    end
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: first steps

Post by dannybloe »

Ah okay I get it.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
bing69
Posts: 139
Joined: Thursday 05 June 2014 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: 's-Hertogenbosch, NL
Contact:

Re: first steps

Post by bing69 »

I get on my way slowly. Have already made various scripts and blocky's.
Pushover also works but I have a question about how I get telegram (bone) method working.
Does this work in the same way as in SH?


curl --data chat_id=19959367 --data-urlencode "text=Some complex text $25 78%" "https://api.telegram.org/bot_mijn code/sendMessage?chat_id=Mijn_id&text=Er is aangebeld!"
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: first steps

Post by dannybloe »

Please start a seperate topic for this.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest