Page 1 of 1

variable not updated correctly

Posted: Monday 09 October 2017 20:20
by Dirk
Below is a script that will update the variable BurstSwitch sometimes, but not always. Who can help me figure out what is wrong?

I made this dzVent (I copied only the 'execute' part):

Code: Select all

    execute = function(domoticz, roomSwitch)
        if (roomSwitch.state == 'On') then
            domoticz.log('----------------------------------  Switch 46 has been turned on')
            domoticz.log(domoticz.variables('BurstSwitch').value)
            if (domoticz.variables('BurstSwitch').value > 0) then
                domoticz.log('----------------------------------  begin minus 1 loop')
                currentValue = domoticz.variables('BurstSwitch').value
                domoticz.log('currentValue is:')
                domoticz.log(currentValue)
                newValue = currentValue - 1
                domoticz.log('newValue is:')
                domoticz.log(newValue)
                domoticz.variables('BurstSwitch').set(newValue)
                domoticz.log(domoticz.variables('BurstSwitch').value)
                domoticz.devices(46).switchOn()
                domoticz.log('----------------------------------  end of minus 1 loop')
                end
            domoticz.devices(46).switchOff().afterSec(5)
           if (domoticz.variables('BurstSwitch').value == 0) then 
                domoticz.variables('BurstSwitch').set('5')   
                domoticz.log('----------------------------------  reset to 5')
                domoticz.log(domoticz.variables('BurstSwitch').value)
                end
        end
    end
Expected behaviour: if the script is triggered (by switching device 46 on), the variable BurstSwitch (which is initialized to 5) will be lowered by 1, and since the same loop turns 46 on again (and triggers this same script again), this loop is continued until BurstSwitch is 0. Then BurstSwitch will be reset to 5, waiting for the next time this script is triggered.
Intended behaviour: if a device is turned on, Domoticz will send 5 on-commands to ensure the signal is received (in general, my device might miss one or two on-commands, but never 5 in a row).
Observed behaviour: the variable BurstSwitch is sometimes stored correctly (by my calll domoticz.variables('BurstSwitch').set(newValue)), but not always.

This is what my log looks like:

Code: Select all

2017-10-09 20:00:04.259 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:04.277 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:04.405 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:04.405 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:04.405 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:04.406 dzVents: Info: 5
2017-10-09 20:00:04.406 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:04.406 dzVents: Info: currentValue is:
2017-10-09 20:00:04.406 dzVents: Info: 5
2017-10-09 20:00:04.406 dzVents: Info: newValue is:
2017-10-09 20:00:04.406 dzVents: Info: 4
2017-10-09 20:00:04.407 dzVents: Info: 5
2017-10-09 20:00:04.407 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:04.407 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:04.407 EventSystem: Fetching url...
2017-10-09 20:00:04.409 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:04.425 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:04.653 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:04.653 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:04.653 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:04.654 dzVents: Info: 4
2017-10-09 20:00:04.654 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:04.654 dzVents: Info: currentValue is:
2017-10-09 20:00:04.654 dzVents: Info: 4
2017-10-09 20:00:04.654 dzVents: Info: newValue is:
2017-10-09 20:00:04.654 dzVents: Info: 3
2017-10-09 20:00:04.654 dzVents: Info: 4
2017-10-09 20:00:04.654 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:04.655 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:04.655 EventSystem: Fetching url...
2017-10-09 20:00:04.656 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:04.673 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:04.900 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:04.900 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:04.900 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:04.901 dzVents: Info: 4
2017-10-09 20:00:04.901 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:04.901 dzVents: Info: currentValue is:
2017-10-09 20:00:04.901 dzVents: Info: 4
2017-10-09 20:00:04.901 dzVents: Info: newValue is:
2017-10-09 20:00:04.901 dzVents: Info: 3
2017-10-09 20:00:04.901 dzVents: Info: 4
2017-10-09 20:00:04.901 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:04.901 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:04.902 EventSystem: Fetching url...
2017-10-09 20:00:04.903 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:04.920 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:05.149 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:05.150 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:05.150 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:05.150 dzVents: Info: 3
2017-10-09 20:00:05.150 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:05.151 dzVents: Info: currentValue is:
2017-10-09 20:00:05.151 dzVents: Info: 3
2017-10-09 20:00:05.151 dzVents: Info: newValue is:
2017-10-09 20:00:05.151 dzVents: Info: 2
2017-10-09 20:00:05.151 dzVents: Info: 3
2017-10-09 20:00:05.151 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:05.151 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:05.151 EventSystem: Fetching url...
2017-10-09 20:00:05.153 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:05.171 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:05.401 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:05.402 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:05.402 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:05.402 dzVents: Info: 3
2017-10-09 20:00:05.402 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:05.402 dzVents: Info: currentValue is:
2017-10-09 20:00:05.402 dzVents: Info: 3
2017-10-09 20:00:05.403 dzVents: Info: newValue is:
2017-10-09 20:00:05.403 dzVents: Info: 2
2017-10-09 20:00:05.403 dzVents: Info: 3
2017-10-09 20:00:05.403 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:05.403 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:05.403 EventSystem: Fetching url...
2017-10-09 20:00:05.405 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:05.423 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:06.160 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:06.168 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:06.168 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:06.169 dzVents: Info: 1
2017-10-09 20:00:06.169 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:06.169 dzVents: Info: currentValue is:
2017-10-09 20:00:06.169 dzVents: Info: 1
2017-10-09 20:00:06.169 dzVents: Info: newValue is:
2017-10-09 20:00:06.169 dzVents: Info: 0
2017-10-09 20:00:06.169 dzVents: Info: 1
2017-10-09 20:00:06.169 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:06.170 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:06.170 EventSystem: Fetching url...
2017-10-09 20:00:06.172 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:06.190 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:06.420 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:06.421 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:06.421 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:06.421 dzVents: Info: 1
2017-10-09 20:00:06.421 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:06.421 dzVents: Info: currentValue is:
2017-10-09 20:00:06.421 dzVents: Info: 1
2017-10-09 20:00:06.421 dzVents: Info: newValue is:
2017-10-09 20:00:06.422 dzVents: Info: 0
2017-10-09 20:00:06.422 dzVents: Info: 1
2017-10-09 20:00:06.422 dzVents: Info: ---------------------------------- end of minus 1 loop
2017-10-09 20:00:06.422 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:06.422 EventSystem: Fetching url...
2017-10-09 20:00:06.424 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:06.442 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:06.673 dzVents: Info: Handling events for: "A7", value: "On"
2017-10-09 20:00:06.673 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:06.673 dzVents: Info: ---------------------------------- Switch 46 has been turned on
2017-10-09 20:00:06.674 dzVents: Info: 0
2017-10-09 20:00:06.674 dzVents: Info: ---------------------------------- reset to 5
2017-10-09 20:00:06.674 dzVents: Info: 0
2017-10-09 20:00:06.674 dzVents: Info: ------ Finished dztest
2017-10-09 20:00:06.675 EventSystem: Fetching url...
2017-10-09 20:00:06.676 EventSystem: Script event triggered: /home/pi/domoticz/scripts/dzVents/runtime/dzVents.lua
2017-10-09 20:00:06.693 (RFXCOM zender) Lighting 1 (A7)
2017-10-09 20:00:11.841 dzVents: Info: Handling events for: "A7", value: "Off"
2017-10-09 20:00:11.841 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:11.841 dzVents: Info: ------ Finished dztest
As you can see, the 'newValue' is always 1 lower that the 'currentValue'. As expected. But somehow this newValue is not always assigned to my variable BurstSwitch. For example here:

Code: Select all

2017-10-09 20:00:06.421 dzVents: Info: ------ Start internal script: dztest: Device: "A7 (RFXCOM zender)", Index: 46
2017-10-09 20:00:06.421 dzVents: Info: ---------------------------------- Switch 46 has been turned on   	>>>> general note that the script has started
2017-10-09 20:00:06.421 dzVents: Info: 1  												>>>> current value of BurstSwitch
2017-10-09 20:00:06.421 dzVents: Info: ---------------------------------- begin minus 1 loop
2017-10-09 20:00:06.421 dzVents: Info: currentValue is:
2017-10-09 20:00:06.421 dzVents: Info: 1												>>>> current value of BurstSwitch
2017-10-09 20:00:06.421 dzVents: Info: newValue is:
2017-10-09 20:00:06.422 dzVents: Info: 0												>>>> current value of newValue
2017-10-09 20:00:06.422 dzVents: Info: 1												>>>> current value of BurstSwitch (expected: 0)

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 7:38
by dannybloe
The variable's value is updated by Domoticz after the script is finished. Just like all commands that you issue with dzVents. If you want to have directly updated persistent values you can use the persistent data support in dzVents. Check the docs.

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 19:27
by Dirk
Thanks for pointing that out. The documentation is very extensive & I am not an experienced programmer, so I tend to read only the parts I know I need, missing the unknown unknowns...
I still don't understand how my variable can be sometimes updated (instead of never or always, but that's probably my lack of understanding. (It is always in the same loop, so it is not a matter of the script finishing or not.)

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 19:41
by Dirk
Note that the code example says:
'if (domoticz.data.counter = 5) then '
This should be
'if (domoticz.data.counter == 5) then'
right?

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 19:55
by dannybloe
yes indeed

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 20:41
by Dirk
You maintain the documentation, right? Could you update this please?

Re: variable not updated correctly

Posted: Tuesday 10 October 2017 20:43
by dannybloe
I will

Re: variable not updated correctly

Posted: Wednesday 11 October 2017 19:38
by dannybloe
Ok, I took a look at your script. First of all you could do this instead (with version 2.3):

Code: Select all

domotcz.devices(46).switchOn().afterSec(5).forMin(1).repeatAfterMin(1, 5)
Or something like that.

That said, your script does this:

Code: Select all

domoticz.variables('BurstSwitch').set(newValue)
domoticz.log(domoticz.variables('BurstSwitch').value)
Like I said earlier, the set command will only be updated AFTER the script is finished so the log will print the value as it was at the beginning of the script.
Also, in the last if-block you do this:

Code: Select all

domoticz.variables('BurstSwitch').set('5') 
There you put a string value in a variable that was intended to be an integer. Doesn't seem right to me as you cannot do arithmetic with a string (like subtracting a value).

Re: variable not updated correctly

Posted: Wednesday 11 October 2017 20:33
by Dirk
Ah, thanks for pointing out my integer-mistake, and as mentioned I'm just beginning with programming so things could probably increase too!
I was puzzeled by the fact that in some runs of then script the variable is updated, but on others not. Looking at the value at the beginning of the script-run:

2017-10-09 20:00:04.406 dzVents: Info: 5
2017-10-09 20:00:04.654 dzVents: Info: 4
2017-10-09 20:00:04.901 dzVents: Info: 4 -- this is unexpected
2017-10-09 20:00:05.150 dzVents: Info: 3
2017-10-09 20:00:05.402 dzVents: Info: 3 -- this is unexpected
2017-10-09 20:00:06.169 dzVents: Info: 1 -- this is unexpected/ 2 is missing
2017-10-09 20:00:06.421 dzVents: Info: 1
2017-10-09 20:00:06.674 dzVents: Info: 0

If that doesn't point to an underlying problem or error, then we don't have to go into this further. I'll adjust my script as pointed out, thanks again!