What am I doing wrong here?

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

Moderator: leecollings

Post Reply
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

What am I doing wrong here?

Post by Melotron »

What am I doing wrong here ?
Im trying to get a night light script going.
We have a switch that we toggle when we go to sleep. Virt_SLEEP
So when this switch are on and there are motion in the kithchen or hallway so should the light go on and 35sec after no motion so should they go off.
But when Virt_SLEEP are off so will the script go on any way.

Code: Select all

----------------------------------------
-- Night Light in Hallway and Kitchen --
----------------------------------------

return {
    on = { devices = {'PIR*','Virt_SLEEP'} },
            
    execute = function(domoticz)
    
        if  domoticz.devices('Virt_SLEEP').state        == "On"      and 
            domoticz.devices('PIR_Kök').state           == "Off"     and
            domoticz.devices('PIR_Hall').state          == "Off"     and
            domoticz.devices('Presence detection - Anyone').state    == "On"	  then
            domoticz.devices('Kök: Led MI').switchOff().afterSec(35)
	    domoticz.devices('Hall: Led MI').switchOff().afterSec(35)
	    print("Night Light Off")		
        end

        if  domoticz.devices('Virt_SLEEP').state        == "On"     and 
            domoticz.devices('PIR_Kök').state           == "On"     or
            domoticz.devices('PIR_Hall').state          == "On"     and
            domoticz.devices('Presence detection - Anyone').state   == "On"	    then	
            domoticz.devices('Kök: Led MI').dimTo(40)
	    domoticz.devices('Hall: Led MI').dimTo(40)
	    print("Night Light 40%")		
        end
        
    end
}
Is it the or command when the turns on or what is it that I am doing wrong here ??

Regards Magnus Svensson
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: What am I doing wrong here?

Post by waaren »

Melotron wrote: Thursday 16 August 2018 18:19 What am I doing wrong here ?
Im trying to get a night light script going.
We have a switch that we toggle when we go to sleep. Virt_SLEEP
So when this switch are on and there are motion in the kithchen or hallway so should the light go on and 35sec after no motion so should they go off.
But when Virt_SLEEP are off so will the script go on any way.
With the or command, the second if is very hard to understand.
To make it easier to understand my advice is to break it down in a couple of nested if's and add some extra print- or log statements while testing.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: What am I doing wrong here?

Post by Melotron »

waaren wrote: Thursday 16 August 2018 19:13
Melotron wrote: Thursday 16 August 2018 18:19 What am I doing wrong here ?
Im trying to get a night light script going.
We have a switch that we toggle when we go to sleep. Virt_SLEEP
So when this switch are on and there are motion in the kithchen or hallway so should the light go on and 35sec after no motion so should they go off.
But when Virt_SLEEP are off so will the script go on any way.
With the or command, the second if is very hard to understand.
To make it easier to understand my advice is to break it down in a couple of nested if's and add some extra print- or log statements while testing.
That's what I've been doing.
The script are really simple now.

Code: Select all

return {
    on = { devices = { 'PIR*','23' } },
            
    execute = function(domoticz)

        if  domoticz.devices('23').state        == "On"     and
            domoticz.devices("PIR_Kök").state     == "On"     or
            domoticz.devices("PIR_Hall").state     == "On"     then
            print("TESTY")

        end
    end
}
I've changed the name to ID to make it simpler. But no luck.

Code: Select all

Status: dzVents: Info: Handling events for: "PIR_Hall", value: "On"
Status: dzVents: Info: ------ Start internal script: dz testy: Device: "PIR_Hall (RFXCOM)", Index: 49
Status: dzVents: Error (2.4.7): There is no device with that name or id: 23
Status: dzVents: Error (2.4.7): An error occured when calling event handler dz testy
Status: dzVents: Error (2.4.7): .../domoticz/scripts/dzVents/generated_scripts/dz testy.lua:11: attempt to index a nil value
Status: dzVents: Info: ------ Finished dz testy
I'm lost at why this isn't working.
Attachments
idx_23.jpg
idx_23.jpg (13.99 KiB) Viewed 967 times
SweetPants

Re: What am I doing wrong here?

Post by SweetPants »

IDX is a number, not a string. Try:

on = { devices = { 'PIR*',23 } },
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: What am I doing wrong here?

Post by waaren »

Melotron wrote: Thursday 16 August 2018 19:36
waaren wrote: Thursday 16 August 2018 19:13
Melotron wrote: Thursday 16 August 2018 18:19 What am I doing wrong here ?
Im trying to get a night light script going.
We have a switch that we toggle when we go to sleep. Virt_SLEEP
So when this switch are on and there are motion in the kithchen or hallway so should the light go on and 35sec after no motion so should they go off.
But when Virt_SLEEP are off so will the script go on any way.
With the or command, the second if is very hard to understand.
To make it easier to understand my advice is to break it down in a couple of nested if's and add some extra print- or log statements while testing.
That's what I've been doing.
The script are really simple now.

I've changed the name to ID to make it simpler. But no luck.

Code: Select all

Status: dzVents: Info: Handling events for: "PIR_Hall", value: "On"
Status: dzVents: Info: ------ Start internal script: dz testy: Device: "PIR_Hall (RFXCOM)", Index: 49
Status: dzVents: Error (2.4.7): There is no device with that name or id: 23
Status: dzVents: Error (2.4.7): An error occured when calling event handler dz testy
Status: dzVents: Error (2.4.7): .../domoticz/scripts/dzVents/generated_scripts/dz testy.lua:11: attempt to index a nil value
Status: dzVents: Info: ------ Finished dz testy
I'm lost at why this isn't working.
I was more thinking to debug / find the error in the logic like

Code: Select all

return {
    on = { devices = { 'PIR*',23 } },
            
    execute = function(domoticz)

        if  domoticz.devices(23).state        == "On" then
            print(domoticz.devices(23).name .. " state is: On") 
            if domoticz.devices("PIR_Kök").state     == "On" then
                print("PIR_Kökstate is: On") 
                if domoticz.devices("PIR_Hall").state     == "On" then
                    print("PIR_Hall is: On")
                end
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: What am I doing wrong here?

Post by Melotron »

waaren wrote: Thursday 16 August 2018 20:17 I was more thinking to debug / find the error in the logic like

Code: Select all

return {
    on = { devices = { 'PIR*',23 } },
            
    execute = function(domoticz)

        if  domoticz.devices(23).state        == "On" then
            print(domoticz.devices(23).name .. " state is: On") 
            if domoticz.devices("PIR_Kök").state     == "On" then
                print("PIR_Kökstate is: On") 
                if domoticz.devices("PIR_Hall").state     == "On" then
                    print("PIR_Hall is: On")
                end
            end
        end
    end
}
Didn't think of that :oops:

With that it is working as it should.
With 23 off :

Code: Select all

Status: dzVents: Info: Handling events for: "PIR_Kök", value: "On"
Status: dzVents: Info: ------ Start internal script: dz testy: Device: "PIR_Kök (RFXCOM)", Index: 112
Status: dzVents: Info: ------ Finished dz testy
With 23 on:

Code: Select all

Status: dzVents: Info: Handling events for: "PIR_Kök", value: "On"
Status: dzVents: Info: ------ Start internal script: dz testy: Device: "PIR_Kök (RFXCOM)", Index: 112
Status: dzVents: Virt_SLEEP state is: On
Status: dzVents: PIR_Kök state is: On
Status: dzVents: PIR_Hall state is: On
Status: dzVents: Info: ------ Finished dz testy
The blocky version are working and its basically the same.
Attachments
NightLight.jpg
NightLight.jpg (45.65 KiB) Viewed 951 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: What am I doing wrong here?

Post by waaren »

Melotron wrote: Thursday 16 August 2018 20:51 The blocky version are working and its basically the same.
Not quite... I see two or's in the blockly and only one in your first dzVents script
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: What am I doing wrong here?

Post by Melotron »

waaren wrote: Thursday 16 August 2018 21:17
Melotron wrote: Thursday 16 August 2018 20:51 The blocky version are working and its basically the same.
Not quite... I see two or's in the blockly and only one in your first dzVents script
Yes, the last PIR are removed from the dzvent script as I haven't set it up after we repainted the wall.
Going to do that this weekend.
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: What am I doing wrong here?

Post by Melotron »

It looks as its working. So this looks to be solved :)

No error logs and and the light turns on and off as I want to.
But are this the right way to do it, or are there any better way to handle this ??
It took a few days, but its working =)

Also are there any pros on building says 2 or 3 huge scripts ? Or should I keep them separated.
It feels little easier to keep them in one scrips when you are trying to get an overview, but also easier that everything stops working with one small error.
What are the recommendation on this as I am really green on programming lol

Code: Select all

    -----------------------------------
    -- Run enviromental scene LED -----
    -----------------------------------
    -- Night Light for LED stripe -----
    -----------------------------------
    
	--------------------------------------------------
	-- Switch for when we are going to sleep.---------
	--  23 = Virt_SLEEP                             --
	--  78 = Virt_Morning                           --
	-- 645 = Presence detection - Anyone            --
	--------------------------------------------------
	-- The id of the PIR and name of them.          --
	--  49 = Pir_Hallway                            --
	--  97 = Pir_Livingroom                         --
	-- 112 = Pir_Kitchen                            --
	--------------------------------------------------
	-- The id of the Lamps and name of them. ---------
	-- 635 = Kitchen: Led MI                        --
	-- 636 = Hallway Led MI                         --
	--------------------------------------------------
	-- Updated 2018-08-20                           --
	--------------------------------------------------
	
return {
	on = { devices = { 49, 97, 112, 23 } },

    execute = function(domoticz)
	
	     if domoticz.devices(645).state         == "On"     and
            domoticz.devices(23).state    == "On"    and 
            domoticz.devices(78).state    == "Off"    then

            if    domoticz.devices(49).state           == "On"     or
                  domoticz.devices(97).state          == "On"     or
                  domoticz.devices(112).state     == "On" then
				  domoticz.devices(635).dimTo(50)
			          domoticz.devices(636).dimTo(50)
			          print("Motion is: On Turning on Night Light")
            end
        end
        	
         if domoticz.devices(645).state         == "On"     and
            domoticz.devices(23).state    == "On"    and 
            domoticz.devices(78).state    == "Off"    then

            if    domoticz.devices(49).state           == "Off"     and
                  domoticz.devices(97).state          == "Off"     and
                  domoticz.devices(112).state     == "Off" then
		              domoticz.devices(635).switchOff().afterSec(35)
			      domoticz.devices(636).switchOff().afterSec(35)
			      print("Motion is: Off: Turning off Night Light")
            end
        end	

    end
}
//Magnus Svensson
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: What am I doing wrong here?

Post by felix63 »

Hi Magnus,

Definitely small separate scripts. Much easier to maintain and as you said one error doesn't mess up too much. I currently have 30 -40 scripts active. I do try to maintain a clear naming convention (Status xxx, Switch xxx, Movement xxx etc) and use some system in assigning different log-labels to each script. This facilitates debugging. And I add a lot of comments. Because some times I write very clever stuff only to find that 6 months down the line I can't work out anymore how it's supposed to work...
Melotron
Posts: 62
Joined: Tuesday 22 November 2016 21:04
Target OS: Linux
Domoticz version: 2020.2
Location: Gothenburg
Contact:

Re: What am I doing wrong here?

Post by Melotron »

Thanks.
Ive started to split up the scripts in rooms and then what they should do. Only keeping one for the day, dusk and dark environment and night light.
But the rest are getting separated.
Its more note keeping to have a couple of scripts then just a few long.
I had to add notes on one of my longer script on where I was in the script and what the parts abow should do.
But I guess it's a good learning experience.
The script are still in Domoticz but once they are in a almost a final version so will I start to move them in to the system.

//Magnus Svensson
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest