Item.state problem  [Solved]

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

Moderator: leecollings

Post Reply
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Item.state problem

Post by Hydci »

Hello, I come to you for help I have a script that works perfectly but I have a problem with item.state I have two devices that can turn on to turn off

The devices are 'Jardin' and 'vacances scolaires'


I tried to do this but it does not work item.state('jardin') == 'Off'


Here is my code if you want to take a look

Code: Select all

return {
        on = 
        {
            timer     = { '25 minutes after sunset','15 minutes after sunset','at 7:45 on mon,tue,thu,fri','at 9:30 on wed,sat,sun','at 9:30 on mon,tue,thu,fri'},
            devices = {'Jardin','Bouton volet','vacances scolaires'},
        },

    execute = function( dz, item)

        Cuisine = dz.devices('Volet cuisine')  -- racourci des devices
        Salon = dz.devices('Volet salon')
        
        if dz.time.matchesRule('15 minutes after sunset') then
        os.execute("sudo /home/pi/./alexa.sh -d Cuisine -e speak:'Fermeture des volets dans dix minutes'")
        end
        if item.state == "1 Click" and dz.devices('Volet cuisine').state == 'Closed' and dz.devices('Volet salon').state == 'Closed' then
            Cuisine.switchOn().silent()
            Salon.switchOn().silent()
	    elseif item.state == "1 Click" and dz.devices('Volet cuisine').state == 'Open' and dz.devices('Volet salon').state == 'Open' then
	        Cuisine.switchOff().silent()
            Salon.switchOff().silent()
        end
	    if item.state == "2 Clicks" and dz.devices('Volet cuisine').state == 'Closed' then
            Cuisine.switchOn().silent()
	    elseif item.state == "2 Clicks" and dz.devices('Volet cuisine').state == 'Open' then
	        Cuisine.switchOff().silent()
        end
	    if item.state == "3 Clicks" and dz.devices('Volet salon').state == 'Closed' then
            Salon.switchOn().silent()
	    elseif item.state == "3 Clicks" and dz.devices('Volet salon').state == 'Open' then
            Salon.switchOff().silent()
        end
    
        if item.isDevice and item.state == 'Off' and dz.time.matchesRule('at nighttime') then  -- ferme les volets si le jardin pass en off
        if dz.devices('Volet cuisine').state == 'Open' then
			Cuisine.switchOff().silent()
		    end
        if dz.devices('Volet salon').state == 'Open' then
			Salon.switchOff().silent()
            end
		end
		if dz.time.matchesRule('25 minutes after sunset') and dz.devices('Jardin').state == 'Off' then --coucher du soleil protection lumière on attend le off pour fermer les volets
        if dz.devices('Volet cuisine').state == 'Open' then
		  Cuisine.switchOff().silent()
            end
        if dz.devices('Volet salon').state == 'Open' then
			Salon.switchOff().silent()
            end
		end

		if dz.time.matchesRule('at 7:45 on mon,tue,thu,fri') and dz.devices('vacances scolaires').state == 'Off' then -- lever du soleil
        if dz.devices('Volet cuisine').state == 'Closed' then 
		    Cuisine.switchOn().silent()
            end
        if dz.devices('Volet salon').state == 'Closed' then
			Salon.switchOn().silent()
		    end
        end
    
        if dz.time.matchesRule('at 9:30 on mon,tue,thu,fri') and dz.devices('vacances scolaires').state == 'On' then -- lever du soleil
        if dz.devices('Volet cuisine').state == 'Closed' then 
		    Cuisine.switchOn().silent()
            end
        if dz.devices('Volet salon').state == 'Closed' then
			Salon.switchOn().silent()
		    end
	    end
    

		if dz.time.matchesRule('at 9:30 on wed,sat,sun') then -- lever du soleil
        if dz.devices('Volet cuisine').state == 'Closed' then    
			Cuisine.switchOn().silent()
            end
        if dz.devices('Volet salon').state == 'Closed' then
			Salon.switchOn().silent()
            end
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Item.state problem

Post by waaren »

Hydci wrote: Sunday 27 October 2019 18:57 Hello, I come to you for help I have a script that works perfectly but I have a problem with item.state I have two devices that can turn on to turn off
The devices are 'Jardin' and 'vacances scolaires'
I tried to do this but it does not work item.state('jardin') == 'Off'
Sorry but I don't understand what you mean and what does not work as intended.
I added some log statements so you can see what happens when the script executes and grouped the various parts.
If you cannot find why it is not working as designed the please explain in detail what you want and include the relevant loglines.

Code: Select all

return 
{
    on = 
    {
        timer     = { '25 minutes after sunset','15 minutes after sunset','at 7:45 on mon,tue,thu,fri','at 9:30 on wed,sat,sun','at 9:30 on mon,tue,thu,fri'},
        devices = {'Jardin','Bouton volet','vacances scolaires'},
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },

    execute = function( dz, item)
        
        _G.logMarker = _G.moduleLabel -- Set marker to scriptname
        
        Cuisine = dz.devices('Volet cuisine')  -- racourci des devices
        Salon = dz.devices('Volet salon')
        Jardin = dz.devices('Jardin')
        Vacances = dz.devices('vacances scolaires')
        
        dz.log('State of Cuisine: ' .. Cuisine.state,dz.LOG_DEBUG)
        dz.log('State of Salon: ' .. Salon.state,dz.LOG_DEBUG)
        dz.log('State of Jardin: ' .. Jardin.state,dz.LOG_DEBUG)
        dz.log('State of Vacances: ' .. Vacances.state,dz.LOG_DEBUG)
        
        if item.isTimer then 
            dz.log('Time trigger is: ' .. item.trigger,dz.LOG_DEBUG )
            if dz.time.matchesRule('15 minutes after sunset') then
                os.execute("sudo /home/pi/./alexa.sh -d Cuisine -e speak:'Fermeture des volets dans dix minutes'")
            elseif dz.time.matchesRule('25 minutes after sunset') and Jardin.state == 'Off' then --coucher du soleil protection lumière on attend le off pour fermer les volets
                if Cuisine.state == 'Open' then
                  Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
                end
            elseif dz.time.matchesRule('at 9:30 on wed,sat,sun') then -- lever du soleil
                if Cuisine.state == 'Closed' then    
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            elseif dz.time.matchesRule('at 7:45 on mon,tue,thu,fri') and Vacances.state == 'Off' then -- lever du soleil
                if Cuisine.state == 'Closed' then 
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            elseif dz.time.matchesRule('at 9:30 on mon,tue,thu,fri') and Vacances.state == 'On' then -- lever du soleil
                if Cuisine.state == 'Closed' then 
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            end
       elseif item.isDevice then
            dz.log(item.name .. ' ; state is ' .. item.state,dz.LOG_DEBUG)
            if item.state == "1 Click" and Cuisine.state == 'Closed' and Salon.state == 'Closed' then
                Cuisine.switchOn().silent()
                Salon.switchOn().silent()
            elseif item.state == "1 Click" and Cuisine.state == 'Open' and Salon.state == 'Open' then
                Cuisine.switchOff().silent()
                Salon.switchOff().silent()
            elseif item.state == "2 Clicks" and Cuisine.state == 'Closed' then
                Cuisine.switchOn().silent()
            elseif item.state == "2 Clicks" and Cuisine.state == 'Open' then
                Cuisine.switchOff().silent()
            elseif item.state == "3 Clicks" and Salon.state == 'Closed' then
                Salon.switchOn().silent()
            elseif item.state == "3 Clicks" and Salon.state == 'Open' then
                Salon.switchOff().silent()
            elseif item.state == 'Off' and dz.time.matchesRule('at nighttime') then  -- ferme les volets si le jardin pass en off
                if Cuisine.state == 'Open' then
                    Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
                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
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Item.state problem

Post by Hydci »

Hello Waaren,
Thank you for your answer and for your optimization of my script.

My problem is at the code level, I have two devices 'Jardin' and 'vacances scolaire' who can go from that can switch on to turn off what triggers this code I would only like this code to be operated only by the garden device

Code: Select all

          elseif item.state == 'Off' and dz.time.matchesRule('at nighttime') then  -- ferme les volets si le jardin pass en off
                if Cuisine.state == 'Open' then
                    Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
I try this but it does not work item.state('Jardin')

Code: Select all

          elseif item.state('Jardin') == 'Off' and dz.time.matchesRule('at nighttime') then  -- ferme les volets si le jardin pass en off
                if Cuisine.state == 'Open' then
                    Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Item.state problem

Post by Hydci »

I think I found a beginning answer is it corect if I write it like that? I'm not sure that parentheses is useful

Code: Select all

if (item.isDevice and item.name == 'Jardin' and item.state == 'Off') and dz.time.matchesRule('at nighttime') then
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Item.state problem

Post by waaren »

Hydci wrote: Monday 28 October 2019 8:11 I think I found a beginning answer is it corect if I write it like that? I'm not sure that parentheses is useful

Code: Select all

if (item.isDevice and item.name == 'Jardin' and item.state == 'Off') and dz.time.matchesRule('at nighttime') then
It's not incorrect but why not using ?

Code: Select all

elseif Jardin.state == 'Off' and dz.time.matchesRule('at nighttime') then
compete code
Spoiler: show

Code: Select all

return 
{
    on = 
    {
        timer     = { '25 minutes after sunset','15 minutes after sunset','at 7:45 on mon,tue,thu,fri','at 9:30 on wed,sat,sun','at 9:30 on mon,tue,thu,fri'},
        devices = {'Jardin','Bouton volet','vacances scolaires'},
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },

    execute = function( dz, item)
        
        _G.logMarker = _G.moduleLabel -- Set marker to scriptname
        
        Cuisine = dz.devices('Volet cuisine')  -- racourci des devices
        Salon = dz.devices('Volet salon')
        Jardin = dz.devices('Jardin')
        Vacances = dz.devices('vacances scolaires')
        
        dz.log('State of Cuisine: ' .. Cuisine.state,dz.LOG_DEBUG)
        dz.log('State of Salon: ' .. Salon.state,dz.LOG_DEBUG)
        dz.log('State of Jardin: ' .. Jardin.state,dz.LOG_DEBUG)
        dz.log('State of Vacances: ' .. Vacances.state,dz.LOG_DEBUG)
        
        if item.isTimer then 
            dz.log('Time trigger is: ' .. item.trigger,dz.LOG_DEBUG )
            if dz.time.matchesRule('15 minutes after sunset') then
                os.execute("sudo /home/pi/./alexa.sh -d Cuisine -e speak:'Fermeture des volets dans dix minutes'")
            elseif dz.time.matchesRule('25 minutes after sunset') and Jardin.state == 'Off' then --coucher du soleil protection lumière on attend le off pour fermer les volets
                if Cuisine.state == 'Open' then
                  Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
                end
            elseif dz.time.matchesRule('at 9:30 on wed,sat,sun') then -- lever du soleil
                if Cuisine.state == 'Closed' then    
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            elseif dz.time.matchesRule('at 7:45 on mon,tue,thu,fri') and Vacances.state == 'Off' then -- lever du soleil
                if Cuisine.state == 'Closed' then 
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            elseif dz.time.matchesRule('at 9:30 on mon,tue,thu,fri') and Vacances.state == 'On' then -- lever du soleil
                if Cuisine.state == 'Closed' then 
                    Cuisine.switchOn().silent()
                end
                if Salon.state == 'Closed' then
                    Salon.switchOn().silent()
                end
            end
       elseif item.isDevice then
            dz.log(item.name .. ' ; state is ' .. item.state,dz.LOG_DEBUG)
            if item.state == "1 Click" and Cuisine.state == 'Closed' and Salon.state == 'Closed' then
                Cuisine.switchOn().silent()
                Salon.switchOn().silent()
            elseif item.state == "1 Click" and Cuisine.state == 'Open' and Salon.state == 'Open' then
                Cuisine.switchOff().silent()
                Salon.switchOff().silent()
            elseif item.state == "2 Clicks" and Cuisine.state == 'Closed' then
                Cuisine.switchOn().silent()
            elseif item.state == "2 Clicks" and Cuisine.state == 'Open' then
                Cuisine.switchOff().silent()
            elseif item.state == "3 Clicks" and Salon.state == 'Closed' then
                Salon.switchOn().silent()
            elseif item.state == "3 Clicks" and Salon.state == 'Open' then
                Salon.switchOff().silent()
            elseif Jardin.state == 'Off' and dz.time.matchesRule('at nighttime') then  -- ferme les volets si le jardin pass en off
                if Cuisine.state == 'Open' then
                    Cuisine.switchOff().silent()
                end
                if Salon.state == 'Open' then
                    Salon.switchOff().silent()
                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
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Item.state problem

Post by Hydci »

I had not thought of this solution small question it will remain an actuator ie if the light goes out the code will be executed?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Item.state problem  [Solved]

Post by waaren »

Hydci wrote: Monday 28 October 2019 9:17 I had not thought of this solution small question it will remain an actuator ie if the light goes out the code will be executed?
Yes, the script will be executed on every update to the devices mentioned in the devices = section and at the times mentioned in the timer = section
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Hydci
Posts: 35
Joined: Tuesday 04 June 2019 21:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Item.state problem

Post by Hydci »

I have one last question here in this code that the state of the light activates the code

Code: Select all

elseif Jardin.state == 'Off' and dz.time.matchesRule('at nighttime') then
And it checks the state it's not an actuator

Code: Select all

elseif dz.time.matchesRule('at 9:30 on mon,tue,thu,fri') and Vacances.state == 'On' then
so device.state little serve for both at once? or to check the condition I have to use something else?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Item.state problem

Post by waaren »

Hydci wrote: Monday 28 October 2019 11:44

Code: Select all

elseif Jardin.state == 'Off' and dz.time.matchesRule('at nighttime') then
In this code this if statement only evaluates to true if the scriptTigger was a device and when the state of device Jardin is ' Off'' and the time is between sunset and sunrise.

Code: Select all

elseif dz.time.matchesRule('at 9:30 on mon,tue,thu,fri') and Vacances.state == 'On' then
In this code this if statement will only evaluate to true if the scriptTrigger was a timer and the time is 9:30 and it is monday, tuesday, thursday or friday and the state of device vacances scolaires is ' Off''.
so device.state little serve for both at once? or to check the condition I have to use something else?
I don't understand this question

The construct of the script and the nature of the 2nd level if / elseif rules is that only one of the if / elseif are true at the same time.
Hope this will answer your question. If not please clarify and include loglines. To point out what you intended and what dd not work as expected.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest