Multiple conditions with if  [Solved]

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

Moderator: leecollings

Post Reply
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Multiple conditions with if

Post by RezaRose »

I am searching for a day now, i cant fix it. I am a beginner in programming.


I tried to make a script when sensor 'Buitensensor' has updated x minutes ago, and the group 'Tuinverlichting' is on, then turn off 'Tuinverlichting'.

I tried to debug my script with the print() so i know what outcome i get. And it seems that if i combine the to conditions with and it will return false. But if i use them separately they work.

I tried

Code: Select all

if (domoticz.devices('Buitensensor').lastUpdate.minutesAgo >= 1  and domoticz.groups('Tuinverlichting') == 'On')  then
and:

Code: Select all

if ((domoticz.devices('Buitensensor').lastUpdate.minutesAgo >= 1)  and (domoticz.groups('Tuinverlichting') == 'On'))  then
This is the whole script.

Code: Select all

return {
       on = {
          timer = {
             'every 1 minutes'
          }
       },
      	execute = function(domoticz, timer)
		if ((domoticz.devices('Buitensensor').lastUpdate.minutesAgo >= 1)  and (domoticz.groups('Tuinverlichting') == 'On'))  then
    domoticz.groups('Tuinverlichting').switchOff()
    print('Tuinverlichting is ' .. (domoticz.groups('Tuinverlichting').lastUpdate.minutesAgo) .. ' minuten geleden ge-update')
    print('Tuinverlichting staat op ' .. (domoticz.groups('Tuinverlichting').state))
else
    print('Tuinverlichting is ' .. (domoticz.groups('Tuinverlichting').lastUpdate.minutesAgo) .. ' minuten geleden ge-update')
    print('Buitensensor is ' .. (domoticz.devices('Buitensensor').lastUpdate.minutesAgo) .. ' minuten geleden ge-update')
	end
       end
    }
output:

Code: Select all

2021-02-01 16:42:00.641 Status: dzVents: Info: ------ Start internal script: Buiten verlichting uit bij geen beweging:, trigger: "every 1 minutes"
2021-02-01 16:42:00.660 Status: dzVents: Tuinverlichting is 68 minuten geleden ge-update
2021-02-01 16:42:00.660 Status: dzVents: Buitensensor is 4 minuten geleden ge-update
2021-02-01 16:42:00.661 Status: dzVents: Info: ------ Finished Buiten verlichting uit bij geen beweging
It think i forget a small thing, but i can't find it. Anyone want to help me? :)
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Multiple conditions with if

Post by waaren »

RezaRose wrote: Monday 01 February 2021 16:43 I am searching for a day now, i cant fix it. I am a beginner in programming.

Code: Select all

if ((domoticz.devices('Buitensensor').lastUpdate.minutesAgo >= 1)  and (domoticz.groups('Tuinverlichting') == 'On'))  then
You forgot the word state in your condition.

Code: Select all

return
{
    on =
    {
        timer =
        {
             'every 1 minutes'
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'combined conditions',
    },

      execute = function(dz)

        dz.log('Tuinverlichting is ' .. dz.groups('Tuinverlichting').lastUpdate.minutesAgo .. ' minuten geleden ge-update', dz.LOG_DEBUG)
        dz.log('Tuinverlichting staat op ' .. dz.groups('Tuinverlichting').state, dz.LOG_DEBUG )

        if dz.devices('Buitensensor').lastUpdate.minutesAgo >= 1 and dz.groups('Tuinverlichting').state == 'On' then
            dz.groups('Tuinverlichting').switchOff()
       end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Multiple conditions with if

Post by RezaRose »

Oh i feel so stupid! Thank you for your answer i will look in to the debug!
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Multiple conditions with if

Post by RezaRose »

In your script i get a error over the DZ, should i change it to domoticz? Mayby because i do not have that variable?

Code: Select all

2021-02-01 17:43:43.784 Error: dzVents: Error: (3.1.4) ...domoticz/scripts/dzVents/generated_scripts/Script #4.lua:13: attempt to index a nil value (global 'dz')
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Multiple conditions with if

Post by waaren »

RezaRose wrote: Monday 01 February 2021 17:44 In your script i get a error over the DZ, should i change it to domoticz? Mayby because i do not have that variable?
Yes change it to domoticz but only in that line.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
RezaRose
Posts: 65
Joined: Sunday 09 February 2020 11:34
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Holland
Contact:

Re: Multiple conditions with if  [Solved]

Post by RezaRose »

Thank u, i understand dzvents better now!
Raspberry Pi 4 - Raspbian bullseye - Python 3.9 - stable
Dashticz on apache2 - Mqqt - node red
Zigate - USB Tll - Philips hue - Ikea - Innr - Xiaomi - TPlink - Nest -v3
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest