Logic with user variables  [Solved]

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

Moderator: leecollings

Post Reply
User avatar
Solderbro
Posts: 80
Joined: Tuesday 18 September 2018 15:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Hamburg, Germany
Contact:

Logic with user variables

Post 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
}
Raspi 3B+RTC, SSD 128GB, Aeotec Gen5, Eurotronic SpiritZ, Fibaro FRGBW, Zipato PIR, Everspring AN180, Neo Coolcam Plug, Fibaro FGMS, Neo Coolcam Doorsensor, Popp Z-Weather
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Logic with user variables

Post by freijn »

Do a print motion and print dark so you can see the status of both...
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Logic with user variables

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Logic with user variables

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
Solderbro
Posts: 80
Joined: Tuesday 18 September 2018 15:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Hamburg, Germany
Contact:

Re: Logic with user variables  [Solved]

Post 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
Raspi 3B+RTC, SSD 128GB, Aeotec Gen5, Eurotronic SpiritZ, Fibaro FRGBW, Zipato PIR, Everspring AN180, Neo Coolcam Plug, Fibaro FGMS, Neo Coolcam Doorsensor, Popp Z-Weather
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest