User Variables in DZVents

Easy to use, 100% Lua-based event scripting framework.
Post Reply
User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

User Variables in DZVents

Post by lonebaggie » Tuesday 15 August 2017 22:02

Im having real problems understanding dzVents and the tables structures i'm trying to convert my Lua scripts and failing miserably.

what is the dzVents equivalent of :-

ur = uservariables["LW-Setting"]

i have tried

local ur = domoticz.variables('LW-Setting")

and countless other variants . I cannot find an example of manipulating user variables in dzVents

sorry for the stupid question , but i've wasted load of time on something so simple

randytsuch
Posts: 94
Joined: Sunday 20 March 2016 19:56
Target OS: Raspberry Pi
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: User Variables in DZVents

Post by randytsuch » Tuesday 15 August 2017 22:55

Code: Select all

return {
    active = true,
    on = {
        timer = {'Every 10 minutes on mon,tue,wed,thu,fri'}
    },
    execute = function(domoticz)
        -- check time of the day
        if (domoticz.time.isDayTime and domoticz.variables('myVar').value == 10) then
            domoticz.variables('anotherVar').set(15)
            --activate my scene
            domoticz.setScene('Evening lights', 'On')
            if (domoticz.devices('My PIR').lastUpdate.minutesAgo > 5) then
                domoticz.devices('Bathroom lights').switchOff()
            end
        end
    end
}
This example is from the wiki

So did you try something like ur.value ? That should read the current value.

And ur.set(new value) to set ur to a new value.

Also, what kind of variable is ur? I've used time and integers and strings

Also, if you search for "Variable object API" in the domoticz wiki, it describes all the attributes for a user variable.

Randy

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: User Variables in DZVents

Post by lonebaggie » Tuesday 15 August 2017 23:54

Thank you . Miised that example

Finally I see the light. What Im trying to do is trigger the dzVents event to run from a table which is created from the contents of a user variable. The user variable contains a list of Idx numbers for devices separated by a "|" terminator. I convert the uservariable contents into a table using the "|" as a delimiter

However I cannot call the domoticz.variables('myvar').value outside of the execute function.

what I want to end up with is

Code: Select all

on = {
        devices = IDNums
        
        
    },
Where IDNums is a table of Idx numbers . Is this possible ?

randytsuch
Posts: 94
Joined: Sunday 20 March 2016 19:56
Target OS: Raspberry Pi
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: User Variables in DZVents

Post by randytsuch » Wednesday 16 August 2017 0:37

I figured this out last night


Again, from the wiki

The on-section holds all the events/triggers that are monitored by dzVents. If any of the events or triggers matches with the current event coming from Domoticz then the execute part of the script is executed. The on-section has four kinds of subsections that can all be used simultaneously! :
devices = { ...}: this is a list of devices. Each device can be: The name of your device between string quotes. You can use the asterisk (*) wild-card here e.g. PIR_* or *_PIR. E.g.: devices = { 'myDevice', 'anotherDevice', 123, 'pir*' }
The index (idx) of your device (the name may change, the index will usually stay the same, the index can be found in the devices section in Domoticz),
The name/idx of your device followed by a time constraint (similar to what you can do): ['myDevice'] = { 'at 15:*', 'at 22:** on sat, sun' } The script will be executed if myDevice was changed and it is either between 15:00 and 16:00 or between 22:00 and 23:00 in the weekend.

timer = { ... }: a list of time triggers like every minute or at 17:*. See below.
variables = { ... }: a list of user variable-names as defined in Domoticz ( Setup > More options > User variables). If any of the variables listed here changes, the script is executed.
security = { domoticz.SECURITY_ARMEDAWAY, domoticz.SECURITY_ARMEDHOME, domoticz.SECURITY_DISARMED}: a list of one or more security states. If the security state in Domoticz changes and it matches with any of the states listed here, the script will be executed. See /path/to/domoticz/scripts/dzVents/examples/templates/security.lua for an example. See Security Panel for information about how to create a security panel device.

So if you add a new sub section to the on section, with
variables = { domoticz.variables('LW-Setting")
}

Then your script will be called whenever you change the value of LW-Setting in the user variable page.

dannybloe
Posts: 1433
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: User Variables in DZVents

Post by dannybloe » Wednesday 16 August 2017 8:23

Sorry but you don't have access to the domoticz collections when defining the on-triggers. So you cannot do something like this

Code: Select all

	on = {
		devices = { domoticz.variables('bla').value }
	}
The reason for this is that the domoticz object hasn't been initialized at that point out of performance reasons as for each and every event that happens in Domoticz, all on-sections for all scripts are evaluated so this has to be as light-weight as possible.

So, you have to do something else to make this work. There are many ways you could do this and one is probably better than another. I guess the question is more: what do you try to achieve that requires you to use uservariables for this? You can create wild-card triggers in the on-section for instance or you can use create a table of id's inside your script at the top of the script file which makes it even easier to maintain as you can already manage and write the script from inside Domoticz' GUI. No need to do nasty string splitting with separators etc. That stuff only slows down things.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
lonebaggie
Posts: 107
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: User Variables in DZVents

Post by lonebaggie » Wednesday 16 August 2017 23:47

Thanks for everyone's help. I appreciate everybody's time taken in explaining my poor grasp of DZVents

I realise now I can only use domoticz object in the execute function :)

pvklink
Posts: 104
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: User Variables in DZVents

Post by pvklink » Thursday 15 February 2018 13:19

@dannybloe
How can you set the variabele 'bla' with value 'red' ?

devices = { domoticz.variables('bla').value

dannybloe
Posts: 1433
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: User Variables in DZVents

Post by dannybloe » Thursday 15 February 2018 13:30

Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

pvklink
Posts: 104
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: User Variables in DZVents

Post by pvklink » Thursday 15 February 2018 15:43

I did, perhaps we are not all as smart as persons that programm daily...

This is my first dzevents script, my lua script works perfect.
The only thing that does not work is to read a user variabele and to change it...

1. read, like {domoticz.variables('denonstatus').value} -- denon on off
2. change it state by {domoticz.variabele('denonstatus')} = 'On'

Peter

script

Code: Select all

return {
	active = true,
	on = {
		devices = {
			'denon_volume'
		           }
	    },
execute = function(domoticz, denon_volume)
    IP = domoticz.variables('ipdenon').value                                                            -- ip adress denon receiver
    IPTV = domoticz.variables('iptv').value                                                             -- ip adress denon receiver
    TVSTATUS=os.execute('ping -c1 ' .. IPTV)
    --IP='192.168.20.26'
    --IPTV='192.168.20.24'
    MAXLEVEL=-30
    DEFLEVEL=-40                                                                                          --nog even checken of dit juist is

    if (denon_volume.state == 'On') then                                                                --denon wordt op aan gezet
        os.execute ("curl http://" .. (IP) .. "/MainZone/index.put.asp?cmd0=PutZone_OnOff%2FON" );          -- zet denon aan
        --domoticz.variabele('denonstatus') = 'On'                                                            -- zet variabele ook op aan
        if TVSTATUS then                                                                                    -- tv staat aan
            denon_source.switchSelector(20).AFTER(2)                                                             -- zet versterker op tv (switch source gaat dan ook op tv)
            denon_mixer.switchSelector(10).AFTER(2)                                                              -- set versterker op stereo
        else                                                                                                -- tv staat uit, dus radio aan
            denon_source.switchSelector(10).AFTER(2)                                                             -- set versterker op radio (switch source gaat dan ook op tv)
            denon_radio.switchSelector(10).AFTER(2)                                                              -- set radio zender sky
            denon_mixer.switchSelector(20).AFTER(2)                                                              -- set versterker op mch stereo
        end  
        os.execute ("curl http://" .. (IP) .. "/MainZone/index.put.asp?cmd0=PutMasterVolumeSet/" .. (DEFLEVEL)); --set standaard volume zetten
        domoticz.log('test2 aanzetten denon')
    elseif (denon_volume.state == 'Off') then                                                           --denon wordt uit gezet
        os.execute ("curl http://" .. (IP) .. "/MainZone/index.put.asp?cmd0=PutZone_OnOff%2FOFF" );         -- zet denon uit
        --domoticz.variabele('denonstatus') = 'Off' 
        domoticz.log('test2 uitzetten denon')
    else                                                                                                --denon geluid wordt harder of zachter gezet
        if ({domoticz.variables('denonstatus').value} == 'Off') then                                        -- als denon uitstaat terwijl volume wordt geactiveerd
            os.execute ("curl http://" .. (IP) .. "/MainZone/index.put.asp?cmd0=PutZone_OnOff%2FON" );          -- zet denon aan
            --domoticz.variabele('denonstatus') = 'On'                                                             -- zet status denon aan
            if TVSTATUS then                                                                                -- tv staat aan
                denon_source.switchSelector(20).AFTER(2)                                                         -- zet versterker op tv (switch source gaat dan ook op tv)
                denon_mixer.switchSelector(10).AFTER(2)                                                          -- set versterker op stereo
            else                                                                                            -- tv staat uit, dus radio aan
                denon_source.switchSelector(10).AFTER(2)                                                         -- set versterker op radio (switch source gaat dan ook op tv)
                denon_radio.switchSelector(10).AFTER(2)                                                          -- set radio zender sky
                denon_mixer.switchSelector(20).AFTER(2)                                                          -- set versterker op mch stereo
            end
            domoticz.log('test2 devices aanzetten nav geluidsaanpassing denon')
        end
        NewLevel = denon_volume.value - 80 ;                                                            -- zet het volume. eerst corrigeren met domoticz scaling
        --NewLevel = math.min(NewLevel, 9.5);                                                               -- Top Domoticz Scale  deze lijkt niet te werken
        if tonumber(NewLevel) > MAXLEVEL then                                                               -- Peter toegevoegd volume maxlevel
            NewLevel = MAXLEVEL                                                                               -- maximaal geluidsniveau nog checken of dit wel werkt met math.min constructie        
        end                                                                                                 -- einde toevoeging maxvolume
        os.execute ("curl http://" .. (IP) .. "/MainZone/index.put.asp?cmd0=PutMasterVolumeSet/" .. (NewLevel)); --set volume
        domoticz.log('test2 geluidsaanpassing denon')
    end
end
}
Last edited by dannybloe on Thursday 15 February 2018 17:04, edited 3 times in total.
Reason: code block

dannybloe
Posts: 1433
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: User Variables in DZVents

Post by dannybloe » Thursday 15 February 2018 15:53

well, as the documentation link I just gave you shows:
set(value): Tells Domoticz to update the variable.
That implies:

Code: Select all

domoticz.variables('denonstatus').set('On')
Thtat's all :)
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

pvklink
Posts: 104
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: User Variables in DZVents

Post by pvklink » Thursday 15 February 2018 17:00

Yes, that's great!
now to the next error in my logfile .. :-)

pvklink
Posts: 104
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: User Variables in DZVents

Post by pvklink » Thursday 15 February 2018 18:47

Do you also have a tip for the next 2 problem. This construct does not work in my scripts/dzvents.
I use this in my old lua scripts
I store ip adresses as characters in user variabels (setip/more options/ user variabeles)


local IPTV = domoticz.variables('iptv').value -- ip adress denon receiver
local TVSTATUS = os.execute('ping -c1 ' .. IPTV)

if TVSTATUS == 'True' then .....

Second problem, i have one device(selector switch) and i need to react to on, off and volume change
i used: devicechanged[RECEIVER_VOLUME] == 'On' or 'Off' or ELSE is a (volume change))

I my current DZvents i use
if (denon_volume.state == 'On') then
if (denon_volume.state == 'Off') then
else seems to be ON or OFF en no volume change
do i use the wrong variable to check the change on/off/volume_change ?
Last edited by pvklink on Thursday 15 February 2018 19:10, edited 1 time in total.

User avatar
emme
Posts: 798
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: User Variables in DZVents

Post by emme » Thursday 15 February 2018 18:58

first of all your sentence will restura True or False, not 'True' or 'False' (it's boolean, not a string ;) )
Unfortunately os.execute does NOT return true or false based on the command result, it returns true or false if the command was executed

If you want a ping a device and return its status why don't you use the System Alive Checker (Ping) hardware?
it creates a device switch that states On and off based on its presence done by a ping made from the server ;)
That's one small step for a programmer, one giant leap for me!
- - - - -
Raspberry Pi3 + AeonStick G5 + RFXCOMM433 + iRTrans Server + NodeJS
There's no stress in the mess!!

pvklink
Posts: 104
Joined: Wednesday 12 November 2014 16:01
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: User Variables in DZVents

Post by pvklink » Thursday 15 February 2018 19:11

Ok, i do have a device pinger active, so i can use that! thanks!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests