Page 1 of 1

Logic with user variables

Posted: Friday 23 August 2019 19:58
by Solderbro
Third day on wondering, why does the domoticz variable toggled between 0 or 1 never match here?

Filled the script with comments to see in the log what happens, slightly 0 or 1 is never true.

Code: Select all

2019-08-23 19:53:00.332 Status: dzVents: Info: ------ Start internal script: DZ Kueche RGBW: Device: "Kue PIR Sensor (Aeotech C5)", Index: 1950
2019-08-23 19:53:00.335 Status: dzVents: Bewegung erkannt
2019-08-23 19:53:00.335 Status: dzVents: Abschaltbefehl einleiten
2019-08-23 19:53:00.336 Status: dzVents: Info: ------ Finished DZ Kueche RGBW
2019-08-23 19:53:00.337 Status: EventSystem: Script event triggered: /home/domoticz/domoticz/dzVents/runtime/dzVents.lua
The idea is to put the state from the domoticz variable into the local and react from it.

Code: Select all

    execute = function(domoticz, item)
        local LOG_LEVEL = domoticz.LOG_DEBUG -- Script default log level.
        local hell = 15 -- lux level for darkness
        local motion = item.active
        local dark = domoticz.devices('Kue PIR Lux').lux <= hell
        local light = domoticz.devices('Kue LED RGB')
        local pir = domoticz.devices('Kue PIR Sensor')
        local color = domoticz.devices('Kue RGBW Color')
        local nachtlicht = domoticz.variables("Nachtlicht").state
        local log = domoticz.log
        
        if motion and dark then
            print('Bewegung erkannt')
            
                if nachtlicht == "0" then
                print('Taglicht einschalten')
                light.dimTo(90)
                color.setHex(0xF5,0xFF,0xFA)
                
            elseif nachtlicht == "1" then
                print('Nachtlicht einschalten')
                light.dimTo(50)
                color.setHex(0xFF,0xDE,0xAD)
                
            end
            print('Abschaltbefehl einleiten')
            light.switchOff().afterSec(300)
        end
    end
}

Re: Logic with user variables

Posted: Friday 23 August 2019 21:30
by freijn
Do a print motion and print dark so you can see the status of both...

Re: Logic with user variables

Posted: Friday 23 August 2019 23:03
by boum
I would rather print nachtlicht . There is no state attribute.
(https://www.domoticz.com/wiki/DzVents:_ ... riables.29)

You should use

Code: Select all

local nachtlicht = domoticz.variables("Nachtlicht").value
Not sure if you'll get an integer or a string. You could print(type(nachtlicht)) to adjust your tests.

Re: Logic with user variables

Posted: Friday 23 August 2019 23:10
by waaren
Solderbro wrote: Friday 23 August 2019 19:58 Third day on wondering, why does the domoticz variable toggled between 0 or 1 never match here?
[EDIT as already answered by @boum :) ]
Please have a look at the dzVents wiki You will read there that state is not an attribute of the uservariable object. If you need to work with the value of a uservariable use the attribute value and please be aware you use the right type (string, integer, float, date or time ) when asking the state "1" is not equal to 1.

Re: Logic with user variables  [Solved]

Posted: Saturday 24 August 2019 13:29
by Solderbro
Yes that shed light to the problem.

An integer need .value and a direct compare of the number without any quotes.

2019-08-24 13:24:16.238 Status: dzVents: Bewegung erkannt
2019-08-24 13:24:16.238 Status: dzVents: Taglicht einschalten
2019-08-24 13:24:16.238 Status: dzVents: Abschaltbefehl einleiten
2019-08-24 13:24:16.240 Status: EventSystem: Script event triggered: /home/domoticz/domoticz/dzVents/runtime/dzVents.lua

if nachtlicht.value == 0 then
print('Taglicht einschalten')

Thank you
Solderbro