Page 1 of 1

first steps

Posted: Saturday 05 August 2017 15:52
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
}

Re: first steps

Posted: Saturday 05 August 2017 16:30
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.

Re: first steps

Posted: Saturday 05 August 2017 18:41
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!

Re: first steps

Posted: Sunday 06 August 2017 10:24
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

Re: first steps

Posted: Sunday 06 August 2017 10:30
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
}

Re: first steps

Posted: Sunday 06 August 2017 13:02
by dannybloe
The documentation should give you examples in how to use the forEach() function. Isn't that helping you?

Re: first steps

Posted: Sunday 06 August 2017 13:21
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

Re: first steps

Posted: Sunday 06 August 2017 14:08
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.

Re: first steps

Posted: Sunday 06 August 2017 14:37
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!

Re: first steps

Posted: Sunday 06 August 2017 14:48
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.

Re: first steps

Posted: Sunday 06 August 2017 15:07
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
}

Re: first steps

Posted: Sunday 06 August 2017 15:28
by dannybloe
Ah okay I get it.

Re: first steps

Posted: Sunday 06 August 2017 15:56
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!"

Re: first steps

Posted: Sunday 06 August 2017 16:04
by dannybloe
Please start a seperate topic for this.