write the program to the changing varants of a variable?
Moderator: leecollings
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
write the program to the changing varants of a variable?
Hi, I have a problem writing the program.thank you for your help,
I want to write a program that runs when a variable changes.
It is important to me that the program be run only when it changes.
Can I do this through command 'uservariablechanged' ?
How to use the a command ' uservariablechanged' ??
Thank for your guidance
I want to write a program that runs when a variable changes.
It is important to me that the program be run only when it changes.
Can I do this through command 'uservariablechanged' ?
How to use the a command ' uservariablechanged' ??
Thank for your guidance
- psubiaco
- Posts: 222
- Joined: Monday 20 August 2018 9:38
- Target OS: Raspberry Pi / ODroid
- Domoticz version: Beta
- Location: Italy
- Contact:
Re: write the program to the changing varants of a variable?
Hi,
Setup -> More options -> Events
click on + button
select LUA -> User variables
and you'll get a clean script template already made that will be called everytime a variable changes.
Regards.
Paolo
Setup -> More options -> Events
click on + button
select LUA -> User variables
and you'll get a clean script template already made that will be called everytime a variable changes.
Regards.
Paolo
Paolo
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
--
I use DomBus modules to charge EV car, get a full alarm system, control heat pump, fire alarm detection, lights and much more. Video
Facebook page - Youtube channel
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
That is not completely true. This will be called every time a variable is updated, regardless if the value changed or not.
If you need that functionality you will have to compare the new value against the last value (you will have to store that somewhere e.g. dzVenst persistent storage or another variable)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Hello, thank you for your guidance..
Is it possible to write a program for me as a small example?
I want a program that turns on the lights when a variable called Smart changes.
Example:
please edit lines 4 and 7 for me.
I do not know how to use the a 'uservariablechanged' command.
thank you for your help.
Is it possible to write a program for me as a small example?
I want a program that turns on the lights when a variable called Smart changes.
Example:
Code: Select all
commandArray ={}
smart=tonumber(uservariables['smart'])
if smart ==1 then
commandArray['light']='On'
else
if smart==0 then
commandArray['light']='Off'
end
end
return commandArray
please edit lines 4 and 7 for me.
I do not know how to use the a 'uservariablechanged' command.
thank you for your help.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
This example will do something every time the uservariable is updated.abdolhamednik wrote: ↑Saturday 15 May 2021 13:05 Is it possible to write a program for me as a small example? I want a program that turns on the lights when a variable called Smart changes.
Code: Select all
commandArray ={}
if uservariablechanged['smart'] then
smart = tonumber(uservariablechanged['smart']) or 99
if smart == 1 then
commandArray['light'] = 'On'
elseif smart == 0 then
commandArray['light'] = 'Off'
else
print ('smart is neither 0 nor 1, but ' .. uservariablechanged['smart'] )
end
end
return commandArray
Code: Select all
commandArray ={}
if uservariablechanged['smart'] and ( uservariables['smart_lastValue'] ~= uservariablechanged['smart']) then
commandArray['Variable:smart_lastValue'] = uservariablechanged['smart']
smart = tonumber(uservariablechanged['smart']) or 99
if smart == 1 then
commandArray['light'] = 'On'
elseif smart == 0 then
commandArray['light'] = 'Off'
else
print ('smart is neither 0 nor 1, but ' .. uservariablechanged['smart'] )
end
end
return commandArray
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Hi thank you very much for your help.
I copied both programs you wrote but is gives the error :
2021-05-16 08:31:34.738 Error: EventSystem: in NEW1: [string "--[[commandArray = {}..."]:37: attempt to index a nil value (global 'uservariablechanged')
smart is also available in uservariables, but I don't know where problem is.
thank you for helping me.
I copied both programs you wrote but is gives the error :
2021-05-16 08:31:34.738 Error: EventSystem: in NEW1: [string "--[[commandArray = {}..."]:37: attempt to index a nil value (global 'uservariablechanged')
smart is also available in uservariables, but I don't know where problem is.
thank you for helping me.
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Both programs are adjunct when they're on the all option.
But when they are on option ,uservariable, they don,t habe rrror, but in this case it doesn't run, that means the light don't go off or on whit smart changes.
But when they are on option ,uservariable, they don,t habe rrror, but in this case it doesn't run, that means the light don't go off or on whit smart changes.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
You should save it with trigger User variable and you should have added a uservar with name smart_lastValue and same type as uservar smart.abdolhamednik wrote: ↑Sunday 16 May 2021 6:14 2021-05-16 08:31:34.738 Error: EventSystem: in NEW1: [string "--[[commandArray = {}..."]:37: attempt to index a nil value (global 'uservariablechanged')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
I'll put the smart program and the light on here.
I'd appreciate it if you told me exactly the flaw in the program or edited the program for me.
smart program(Device):
program light(uservariable):
variable smart also exists in uservariables.
But the light doesn't go off or on white smart changes.
I'd appreciate it if you told me exactly the flaw in the program or edited the program for me.
smart program(Device):
Code: Select all
commandArray ={}
smart = tonumber(uservariables['smart'])
if devicechanged['auto/man']=='Open' then
commandArray['Variable:smart']='1'
else
if devicechanged['auto/man']=='Closed' then
commandArray['Variable:smart']='0'
end
end
return commandArray
Code: Select all
commandArray ={}
if uservariablechanged['smart'] then
smart = tonumber(uservariablechanged['smart']) or 99
if smart == 1 then
commandArray['light'] = 'On'
elseif smart == 0 then
commandArray['light'] = 'Off'
else
print ('smart is neither 0 nor 1, but ' .. uservariablechanged['smart'] )
end
end
return commandArray
But the light doesn't go off or on white smart changes.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
Have you added the domotizc uservariable smart_lastValue with the same type as smart? If not please do now.abdolhamednik wrote: ↑Sunday 16 May 2021 9:03 I'll put the smart program and the light on here.
I'd appreciate it if you told me exactly the flaw in the program or edited the program for me.
Please show what you see in the log with below version saved as triggered User variable and after updating the uservariable smart with 0 or 1.
Code: Select all
-- Added some extra checks and debug printlines.
commandArray ={}
if uservariablechanged then
if uservariables['smart_lastValue'] then
if uservariablechanged['smart'] and ( uservariables['smart_lastValue'] ~= uservariablechanged['smart']) then
print ('DEBUG - Uservar: ' .. 'smart value changed' )
commandArray['Variable:smart_lastValue'] = uservariablechanged['smart']
smart = tonumber(uservariablechanged['smart']) or 99
if smart == 1 then
print ('DEBUG - Uservar: ' .. 'Switching light On' )
commandArray['light'] = 'On'
elseif smart == 0 then
print ('DEBUG - Uservar: ' .. 'Switching light Off' )
commandArray['light'] = 'Off'
else
print ('DEBUG - Uservar: ' .. 'smart is neither 0 nor 1, but ' .. uservariablechanged['smart'] )
end
elseif uservariablechanged['smart'] then
print ('DEBUG - Uservar: ' .. 'smart updated but value did not change' )
else
print ('DEBUG - Uservar: ' .. 'smart not updated' )
end
else
print ('DEBUG - Uservar: ' .. 'smart_lastValue does not exist.' )
end
else
print ('DEBUG - Uservar: ' .. 'Not triggered by a uservariable' )
end
return commandArray
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Hi there,your friend
Thank you greatly for your help and guidance.
I edited the program you sent yesterday a little bit and it was kind of right.
I'll out the program here and continue the description.
program:
The smart variable in the above program changes between a zero number or one in another program under certain conditions.
I'll put the program that changes 'smart' :
(Devaice)
Now the program you wrote is wrong when it changes that second smart program the program you wrote will not be executed.
But when I manually change the smart variable in the 'uservariables' the program works properly and the lights turn on or off.
Now I have a few questions:
1-what is reason for this problem?
2-what's the reason you used variable 'smart_value' ?
I apologize for taking your time..
If you edite the program for me and send it to me,I'll be very grateful.
Thanks Faravan.
Thank you greatly for your help and guidance.
I edited the program you sent yesterday a little bit and it was kind of right.
I'll out the program here and continue the description.
program:
Code: Select all
commandArray ={}
print('hello....')
if uservariablechanged then
if uservariables['smart_lastValue'] then
if uservariablechanged['smart'] and ( uservariables['smart_lastValue'] ~= uservariablechanged['smart']) then
print ('DEBUG!!!!! - Uservar: ' .. 'smart value changed' )
commandArray['Variable:smart_lastValue'] = uservariablechanged['smart']
smart = tonumber(uservariablechanged['smart']) or 99
if smart == 1 then
print ('DEBUG!!!!! - Uservar: ' .. 'Switching light On' )
commandArray['light'] = 'On'
elseif smart == 0 then
print ('DEBUG!!!!! - Uservar: ' .. 'Switching light Off' )
commandArray['light'] = 'Off'
else
print ('DEBUG!!!!! - Uservar: ' .. 'smart is neither 0 nor 1, but ' .. uservariablechanged['smart'] )
end
elseif uservariablechanged['smart'] then
print ('DEBUG!!!!! - Uservar: ' .. 'smart updated but value did not change' )
else
print ('DEBUG!!!!! - Uservar: ' .. 'smart not updated' )
end
else
print ('DEBUG!!!!!! - Uservar: ' .. 'smart_lastValue does not exist.' )
end
else
print ('DEBUG!!!!! - Uservar: ' .. 'Not triggered by a uservariable' )
end
return commandArray
I'll put the program that changes 'smart' :
(Devaice)
Code: Select all
smart = tonumber(uservariables['smart'])
if devicechanged['auto/man']=='Open' then
commandArray['Variable:smart']='On'
else
if devicechanged['auto/man']=='Closed' then
commandArray['Variable:smart']='Off'
end
end
return commandArray
But when I manually change the smart variable in the 'uservariables' the program works properly and the lights turn on or off.
Now I have a few questions:
1-what is reason for this problem?
2-what's the reason you used variable 'smart_value' ?
I apologize for taking your time..
If you edite the program for me and send it to me,I'll be very grateful.
Thanks Faravan.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
Probably a language thing but I tested the script and it behaves as expected, so definitely not wrong.abdolhamednik wrote: ↑Monday 17 May 2021 6:59 Now the program you wrote is wrong when it changes that second smart program the program you wrote will not be executed.
But when I manually change the smart variable in the 'uservariables' the program works properly and the lights turn on or off.
Reasons;Now I have a few questions:
1-what is reason for this problem?
- When you set a domoticz uservariable from a classic Lua script or Blockly it will not trigger an event.
You will have to change the uservar manually, or via a JSON call or use a dzVents script to do that. - You set the value of the smart uservarable to 'On' or 'Off' but test if it's 0 or 1
I used the uservariable smart_lastValue to keep track of the previous value of smart to ensure that the script only switches the light when the value of smart changed and not when smart is updated but the value is kept unchanged.2-what's the reason you used variable 'smart_value' ?
I also have some questions for you.
- I asked a couple of times for the output of the log to help me understanding what happens but you ignore this question. Why?
- Your profile states that you use V4.9700 is that stil accurate?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Hello, thank you for your help.
I will test the program again and send you the log report
System log for when I manually change Smart:
when smart=1 :
2021-05-17 15:49:50.156 (GSB) Light/Switch (light)
2021-05-17 15:49:50.088 Status: LUA: hello....
2021-05-17 15:49:50.104 Status: LUA: DEBUG!!!!! - Uservar: smart value changed
2021-05-17 15:49:50.105 Status: LUA: DEBUG!!!!! - Uservar: Switching light On
2021-05-17 15:49:50.117 Status: EventSystem: Script event triggered: Script #2
when smart=0 :
2021-05-17 15:51:44.132 (GSB) Light/Switch (light)
2021-05-17 15:51:44.013 Status: LUA: hello....
2021-05-17 15:51:44.027 Status: LUA: DEBUG!!!!! - Uservar: smart value changed
2021-05-17 15:51:44.030 Status: LUA: DEBUG!!!!! - Uservar: Switching light Off
2021-05-17 15:51:44.037 Status: EventSystem: Script event triggered: Script #2
smart and smart_value Available in uservariables.
I will test the program again and send you the log report
System log for when I manually change Smart:
when smart=1 :
2021-05-17 15:49:50.156 (GSB) Light/Switch (light)
2021-05-17 15:49:50.088 Status: LUA: hello....
2021-05-17 15:49:50.104 Status: LUA: DEBUG!!!!! - Uservar: smart value changed
2021-05-17 15:49:50.105 Status: LUA: DEBUG!!!!! - Uservar: Switching light On
2021-05-17 15:49:50.117 Status: EventSystem: Script event triggered: Script #2
when smart=0 :
2021-05-17 15:51:44.132 (GSB) Light/Switch (light)
2021-05-17 15:51:44.013 Status: LUA: hello....
2021-05-17 15:51:44.027 Status: LUA: DEBUG!!!!! - Uservar: smart value changed
2021-05-17 15:51:44.030 Status: LUA: DEBUG!!!!! - Uservar: Switching light Off
2021-05-17 15:51:44.037 Status: EventSystem: Script event triggered: Script #2
smart and smart_value Available in uservariables.
- abdolhamednik
- Posts: 39
- Joined: Sunday 27 September 2020 7:33
- Target OS: Raspberry Pi / ODroid
- Domoticz version: 4.9700
- Contact:
Re: write the program to the changing varants of a variable?
Log for when Smart is changed by Event:
when smart=1 :
2021-05-17 15:57:11.870 (GSB) Light/Switch (R9(1.44)<3-3>[66]**)
2021-05-17 15:57:11.831 Status: User: unity-tech (IP: 192.168.1.80) initiated a switch command (66/R9(1.44)<3-3>[66]**/On)
2021-05-17 15:57:12.565 Status: LUA: smart=1 chon kelid tablo hooshmand shod_sistem hooshmand amade be kar hast
2021-05-17 15:57:12.569 Status: EventSystem: Script event triggered: smart_hadi_new
when smart=0
2021-05-17 15:57:58.342 Status: User: unity-tech (IP: 192.168.1.80) initiated a switch command (66/R9(1.44)<3-3>[66]**/Off)
2021-05-17 15:57:58.784 Status: LUA: smart=0 chon kelid tablo dasti shod
2021-05-17 15:57:58.797 Status: EventSystem: Script event triggered: smart_hadi_new
Thank you very much for your kindness.
when smart=1 :
2021-05-17 15:57:11.870 (GSB) Light/Switch (R9(1.44)<3-3>[66]**)
2021-05-17 15:57:11.831 Status: User: unity-tech (IP: 192.168.1.80) initiated a switch command (66/R9(1.44)<3-3>[66]**/On)
2021-05-17 15:57:12.565 Status: LUA: smart=1 chon kelid tablo hooshmand shod_sistem hooshmand amade be kar hast
2021-05-17 15:57:12.569 Status: EventSystem: Script event triggered: smart_hadi_new
when smart=0
2021-05-17 15:57:58.342 Status: User: unity-tech (IP: 192.168.1.80) initiated a switch command (66/R9(1.44)<3-3>[66]**/Off)
2021-05-17 15:57:58.784 Status: LUA: smart=0 chon kelid tablo dasti shod
2021-05-17 15:57:58.797 Status: EventSystem: Script event triggered: smart_hadi_new
Thank you very much for your kindness.
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
Do you mean smart_lastValue?
but despite that; script works as expected
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
- waaren
- Posts: 6028
- Joined: Tuesday 03 January 2017 14:18
- Target OS: Linux
- Domoticz version: Beta
- Location: Netherlands
- Contact:
Re: write the program to the changing varants of a variable?
Again: uservariables updated by a Lua script or Blockly do not trigger an event.
Do not enter your personal details here. They can be misused and I do not give other support then via the public part of this forum with an exception if any personal data need to be shared.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Who is online
Users browsing this forum: No registered users and 1 guest