Switch's state is not update

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

Moderator: leecollings

Post Reply
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Switch's state is not update

Post by Filnet »

Hello!
Here is my problem. In the script below, I ask to change the state of a button. This command works well (the button changes state). But if I ask to show me this state immediately, the answer is wrong.
Can anyone explain to me? And find me a solution?
Thanks

Code: Select all


return {
	on = {
	    timer = {'every 1 minutes'},
	    },
	execute = function(dz, item)

        if item.isTimer then ------------ TIMER -----------------
            print ('Etat: '..dz.devices('Bouton').sValue)
            print ('')
            
            print ('On éteint Bouton')
            dz.devices('Bouton').switchOff()
            print ('Etat: '..dz.devices('Bouton').sValue)
            print ('')
            
            print ('On allume Bouton')
            dz.devices('Bouton').switchOn()
            print ('Etat: '..dz.devices('Bouton').sValue)
            print ('')
            
            print ('On éteint Bouton')
            dz.devices('Bouton').switchOff()
            print ('Etat: '..dz.devices('Bouton').sValue)
    	end
    end
}

Code: Select all

 2024-02-02 16:33:03.564 Status: dzVents: Etat: Off
2024-02-02 16:33:03.564 Status: dzVents:
2024-02-02 16:33:03.564 Status: dzVents: On éteint Bouton
2024-02-02 16:33:03.564 Status: dzVents: Etat: Off
2024-02-02 16:33:03.565 Status: dzVents:
2024-02-02 16:33:03.565 Status: dzVents: On allume Bouton
2024-02-02 16:33:03.565 Status: dzVents: Etat: Off
2024-02-02 16:33:03.565 Status: dzVents:
2024-02-02 16:33:03.565 Status: dzVents: On éteint Bouton
2024-02-02 16:33:03.565 Status: dzVents: Etat: Off 
User avatar
waltervl
Posts: 5843
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Switch's state is not update

Post by waltervl »

Because you are using Svalue and that is not used for switches. A switch uses nvalue but better use .state (string) or .active (boolean) to get the state of a switch.
See https://www.domoticz.com/wiki/DzVents:_ ... ll_devices
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Re: Switch's state is not update

Post by Filnet »

Hello waltervl

Thank you for your message.
But .state or .active don't work. The state response stay false.
User avatar
waltervl
Posts: 5843
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Switch's state is not update

Post by waltervl »

It could be that dzvents needs another refresh action to check.
What if you switchOn() 2 times? Or use .checkFirst
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Re: Switch's state is not update

Post by Filnet »

Yes, you are right: dzvents needs to be refresh but it can't do it in the same passage. It needs to recall all the script (for instance, next minute if trigger is Timer).
User avatar
waltervl
Posts: 5843
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Switch's state is not update

Post by waltervl »

But what do you want to achieve? Why do you want to check the status of a switch multiple times in a script?

You can also trigger on device change.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
gizmocuz
Posts: 2536
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Switch's state is not update

Post by gizmocuz »

You have to work on your script logic to turn on or off the switch one time.
It makes no sense to turn on/off the switch multiple times in the same script
So make a Boolean, and in the end, if this is true, turn on the switch, and if not, turn it off
Quality outlives Quantity!
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Re: Switch's state is not update

Post by Filnet »

Hello gizmocuz
I don't want to change state multiple times. The script I wrote is there to highlight the poor real-time button state display. It is only done once the script is finished and not in real time.
Try this script:

Code: Select all

return {
	on = {
	    timer = {'every 1 minutes'},
	    devices = {'Bouton'},
	    },
	execute = function(dz, item)
        if item.isTimer then ------------ TIMER -----------------
            print ('State before: '..tostring(dz.devices('Bouton').active))
            print ('-> Command Bouton')
            dz.devices('Bouton').toggleSwitch()
            print ('State after: '..tostring(dz.devices('Bouton').active))
    	end
    end
}
Result is (You could see that ' State after' is wrong):

Code: Select all

 2024-02-03 08:40:00.305 test: Light/Switch (Bouton)
2024-02-03 08:40:00.265 Status: dzVents: State before: false
2024-02-03 08:40:00.267 Status: dzVents: -> Command Bouton
2024-02-03 08:40:00.267 Status: dzVents: State after: false
2024-02-03 08:41:00.315 test: Light/Switch (Bouton)
2024-02-03 08:41:00.273 Status: dzVents: State before: true
2024-02-03 08:41:00.273 Status: dzVents: -> Command Bouton
2024-02-03 08:41:00.273 Status: dzVents: State after: true 
User avatar
gizmocuz
Posts: 2536
Joined: Thursday 11 July 2013 18:59
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Top of the world
Contact:

Re: Switch's state is not update

Post by gizmocuz »

It's by design, as you can see in the script, you 'return' an object
What do you expect? That during a line executed from dzVents the state is updated?
Nope...
So, what you want is not working and should also not be necessary or done.
Quality outlives Quantity!
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Re: Switch's state is not update

Post by Filnet »

I understand the design well. It is the same as for LUA (of which dzvents is an application). I'm troubled by the fact that the button's state can change several times in the script but not its representation. Currently I transpose all my lua scripts (>40) into dzvents and I don't always see the benefit of doing so. That said to solve this problem in dzvents, I use the Device trigger.
Thank you for your reply.
Filnet
Posts: 23
Joined: Tuesday 06 March 2018 13:55
Target OS: -
Domoticz version:
Contact:

Re: Switch's state is not update

Post by Filnet »

I solved my problem with the use of persistant datas.
Thanks to waltervl and gizmocuz.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest