updating user variable does not work

Easy to use, 100% Lua-based event scripting framework.
Post Reply
EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

updating user variable does not work

Post by EricT » Wednesday 02 May 2018 16:56

Hello all,
I hope you can help me.
I want to update a user variable with this script:

Code: Select all

commandArray = {}

local switchName = "On signal motion sensor"
local USERVARIABLENAME = "counter1 var"
local USERVARIABLETYPE = "0"
local USERVARIABLEVALUE = "11"

if devicechanged[switchName] == 'On' then

   http://192.168.2.10:8080/json.htm?type=command&param=updateuservariable&vname=USERVARIABLENAME&vtype=USERVARIABLETYPE&vvalue=USERVARIABLEVALUE

end

return commandArray

However, it does not work and I get the following error: Error: EventSystem: in update user value: [string " ..."]:11: expected near '/'

I have followed these instructions from the Domoticz wiki: http://www.domoticz.com/wiki/Domoticz_A ... g_variable:
Update an existing variable
/json.htm?type=command&param=updateuservariable&vname=USERVARIABLENAME&vtype=USERVARIABLETYPE
&vvalue=USERVARIABLEVALUE

Remember to change:
USERVARIABLENAME with the name of your variable
USERVARIABLETYPE according to the table from "Store a new variable"
USERVARIABLEVALUE with the new value

Can someone please tell me what I am doing wrong?
Thanks!
if it was easy everybody would do it

User avatar
McMelloW
Posts: 193
Joined: Monday 20 November 2017 18:01
Target OS: Raspberry Pi
Domoticz version: V3.beta
Location: Harderwijk, NL
Contact:

Re: updating user variable does not work

Post by McMelloW » Wednesday 02 May 2018 17:16

Perhaps think about switching to dzVents. Far more easier then lua. As a fact dzVents is lua

Updating a variable in dzVents is

Code: Select all

domoticz.variables(VarName).set(new_value)
That's all !
Greetings McMelloW

User avatar
jvdz
Posts: 1368
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: updating user variable does not work

Post by jvdz » Wednesday 02 May 2018 17:26

I am wondering why you think you can use an HTTP:// link just like that in LUA and expect it will be understood. :-)
You need to use the standard available event option in LUA for opening URLs:

Code: Select all

  commandArray['OpenURL']= 'http://192.168.2.10:8080/json.htm?type=command&param=updateuservariable&vname='..USERVARIABLENAME..'&vtype='..USERVARIABLETYPE..'&vvalue='..USERVARIABLEVALUE
but why not simply use :

Code: Select all

	uservariables[USERVARIABLENAME] = USERVARIABLEVALUE
This is all described in this wiki page: https://www.domoticz.com/wiki/Events#Lu ... _Interface

Jos :-)

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Thursday 03 May 2018 11:24

Thank you both for your help, much appericiated!

I want to help my mother who is 84 and still lives on her own 60 km away from me. Therefore I want to help her and keep an eye on her, however I am totally new to Domoticz, Lua, dzVents and Blockly. I am trying to learn from what I can find on the Internet and I know some of the Python3 basics.

I have tried to create a Blockly but I can not get it not work: http://www.domoticz.com/forum/viewtopic ... 62&t=23272. I think the Blockly script does not see the counter value as a number but as a string. Therefore I want to use an integer user variable as a workaround.

I have tried

Code: Select all

commandArray['OpenURL']= 'http://192.168.2.10:8080/json.htm?type=command&param=updateuservariable&vname='..USERVARIABLENAME..'&vtype='..USERVARIABLETYPE..'&vvalue='..USERVARIABLEVALUE
But I get the error: Error opening url. I thought it was caused by the "¶" in command¶m=updateuservariable&vname=counter1 var&vtype=0&vvalue=11

I have replaced &param by &dparam according to this post viewtopic.php?t=19311#p149864 but I still get the error message with command&dparam

I have also tried to update the user variable by using:

Code: Select all

uservariables[USERVARIABLENAME] = USERVARIABLEVALUE
Unfortunately I can not get it to work either.

I finally found this to get the user variable update to work:

Code: Select all

commandArray['Variable:'..USERVARIABLENAME] = USERVARIABLEVALUE
I prefer not to use a workaround for the Blockly using the counter value. Is it possible for you to point me in the right direction or share some advice?
if it was easy everybody would do it

User avatar
jvdz
Posts: 1368
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: updating user variable does not work

Post by jvdz » Thursday 03 May 2018 12:18

EricT wrote:
Thursday 03 May 2018 11:24
I want to help my mother who is 84 and still lives on her own 60 km away from me. Therefore I want to help her and keep an eye on her, however I am totally new to Domoticz, Lua, dzVents and Blockly. I am trying to learn from what I can find on the Internet and I know some of the Python3 basics.
Understand the feeling and no problem to help make that happen. :-)
EricT wrote:
Thursday 03 May 2018 11:24
I finally found this to get the user variable update to work:

Code: Select all

commandArray['Variable:'..USERVARIABLENAME] = USERVARIABLEVALUE
Sorry about that, that was my mistake. This is indeed the correct syntax.
EricT wrote:
Thursday 03 May 2018 11:24
I prefer not to use a workaround for the Blockly using the counter value. Is it possible for you to point me in the right direction or share some advice?
I would go for a single LUA script ( or DzEvents script) for this purpose as that should do it.
I gather you like to send an Email when the counter is higher than 1?
What is the exact logic you would like to apply here?
This is the script from the other topic with a few modifications but untested, and all that is needed is to add to logic to send that email?

Code: Select all

commandArray = {}

local virtualcounter_idx = "30"
local virtualcounter_name = "counter1"
local switchName = "On signal motion sensor"

if devicechanged[switchName] == 'On' then

-- Get current value
   count_value = tonumber(otherdevices_svalues[virtualcounter_name])
   print("old value counter1= " .. count_value)

-- Calculate new value
   count_value = count_value + 1
   print("new value counter1= " .. count_value)

-- Update virtual counter device
   ts = virtualcounter_idx .. "|0|" .. tostring(count_value)
   commandArray['UpdateDevice'] = ts
   print ("ts counter1= " ..ts)

end

return commandArray
Jos

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Thursday 03 May 2018 14:57

Thank you Jos, your help and understanding is much appericiated!

What I try to accomplish is to send a message (email and/or other) to me when something is wrong. For instance when there is no activity detected by the motion sensor by the time she normally is out of bed. I want to be able to set a threshold of a minimum amount of activities before that message is sent, therefore I want to use a counter to be able to set that threshold and monitor the amount of times the motion sensor was triggered within a certain timeframe.

This is what I have sofar, it updates the counter and the user variable:

Code: Select all

commandArray = {}

local virtualcounter_idx = "30"
local virtualcounter_name = "counter1"
local switchName = "On signal motion sensor"
local USERVARIABLENAME = "counter1 var"

if devicechanged[switchName] == 'On' then

-- Get current value
   count_value = tostring(otherdevices_svalues[virtualcounter_name])
   print("old value counter1= " .. count_value)

-- Calculate new value
   count_value = count_value + 1
   print("new value counter1= " .. count_value)
   
-- Update virtual counter device
   ts = virtualcounter_idx .. "|0|" .. tostring(count_value)
   commandArray['UpdateDevice'] = ts
   print ("ts counter1= " ..ts)

-- Get old counter vanlue and add 1 to make it eaqual to the new value of the counter
   x = tostring(otherdevices_svalues[virtualcounter_name])
   y = x + 1
   USERVARIABLEVALUE = tostring(y)
   
-- Update user variable "counter1 var"   
   commandArray['Variable:'..USERVARIABLENAME] = USERVARIABLEVALUE

end

return commandArray
One of the things I do not understand is that it looks like the user variable requires a string to be updated but the type is an integer.

Once I have the counter working I have to figure out how to use the with value threshold and timeframes .
if it was easy everybody would do it

User avatar
jvdz
Posts: 1368
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: updating user variable does not work

Post by jvdz » Thursday 03 May 2018 15:27

EricT wrote:
Thursday 03 May 2018 14:57
What I try to accomplish is to send a message (email and/or other) to me when something is wrong. For instance when there is no activity detected by the motion sensor by the time she normally is out of bed. I want to be able to set a threshold of a minimum amount of activities before that message is sent, therefore I want to use a counter to be able to set that threshold and monitor the amount of times the motion sensor was triggered within a certain timeframe.
These sentences are a little contradictory. :)
The first part I do understand, about detecting no activity in the morning and could be pretty strait forward.
This is an example how that could be done with the devices you already have.
This script will send an notification when there was no activity in the last 4 hours in the morning at 10:00 : (untested)

Code: Select all

-- this needs to be a TIME script ran each minute so name it e.g.: script_time_morningcheck.lua

commandArray = {}

local check_hh = 10
local check_mm = 00
local Max_NoAct = 4 * 60 *60   -- 4 hours = 4*60*60 seconds
local switchName = "On signal motion sensor"

----------------------------------------------------------------------------------------
local time = os.date("*t")

-- Calculate the time difference in seconds between Now and supplied time. in YY/MM/DD HH:MM:SS
function datetimedifferencenow(s)
	s = s .. ":00"              -- add seconds in case only hh:mm is supplied
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	t1 = os.time()
	t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
	difference = os.difftime(t1, t2)
	return difference
end

-- only perfom the check at defined time
if time.hour == check_hh and time.min == check_mm then
	-- check for last detect activity and when longer send notification
	if datetimedifferencenow(otherdevices_lastupdate[switchName]) >= Max_NoAct then
		commandArray['SendEmail'] = 'Waarschuwing geen beweging#Geen beweging gezien sinds '..otherdevices_lastupdate['switchName']..'#user@domain.com'
	end
end

return commandArray
I am not sure what you are looking for with the counter when activity is detected.

Jos

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Thursday 03 May 2018 16:32

Awesome! I will give it a try.

With the counter I want to be able to see what has happened when I get the message and to know what her daily routine is so I can tune the script to be more accurate.

something like this:
Image
Attachments
monitor example.png
monitor example.png (114.01 KiB) Viewed 702 times
if it was easy everybody would do it

User avatar
jvdz
Posts: 1368
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: updating user variable does not work

Post by jvdz » Thursday 03 May 2018 16:43

Ah ... ok understand that part. So it is not the case you want to send a notification when a certain number of detections happened as I initially understood from your post.
So the counter is working properly, which should indeed give that basic information about movement where you can apply logic on.

Jos

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Thursday 03 May 2018 16:53

Correct, I want to know what not happened when I get the notification (sorry for the confusion). I also wanted to set a minimum, e.g.
If
not triggered with a minimum of 3 between 0800 and 0900
do
send notification "activity too low between 0800 and 0900, check log and call 112"
if it was easy everybody would do it

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Friday 04 May 2018 9:16

Hi Jos,

I had three time event running during the night to test it, two of them set in a timeframe that would send the email and one not to . This morning I saw two error messages in the log:
Error: EventSystem: in night activity check: [string "-- this needs to be a TIME script ran each mi..."]:32: attempt to concatenate field 'switchName' (a nil value)

I figured it was a sytax error thus I replaced otherdevices_lastupdate['switchName'] by otherdevices_lastupdate[switchName] and now it works!

Awesome! I clearly understand you are the Yoda and the force is with you, can I be a pedawan and learn the secrets of the force?

I will do some more testing today and let you know how that worked out.

Eric
if it was easy everybody would do it

User avatar
jvdz
Posts: 1368
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: updating user variable does not work

Post by jvdz » Friday 04 May 2018 9:38

Much to learn you still have…my old padawan. … This is just the beginning!
Glad it works for you now as that is the fun part of it! :)
Jos

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Friday 04 May 2018 13:32

Much to learn you still have…my old padawan. … This is just the beginning!
You are absolutely right, therefore I thought I might as well start here http://tylerneylon.com/a/learn-lua/

It says "Learn Lua in 15 minutes". Let's see how that works out..... ;)
if it was easy everybody would do it

EricT
Posts: 35
Joined: Tuesday 01 May 2018 8:09
Target OS: Raspberry Pi
Domoticz version: 3.9440
Location: Rijen, The Netherlands
Contact:

Re: updating user variable does not work

Post by EricT » Friday 04 May 2018 14:38

I have tested it today and it worked like a charm. I have added a door switch to the script because there is also a door to "escape" from the bedroom. So the script now uses a motion sensor and a door switch, if both do not have any activity within the set timeframe an email is sent.

Here is the script:

Code: Select all

-- this needs to be a TIME script ran each minute so name it e.g.: script_time_morningcheck.lua

commandArray = {}

local check_hh = 10 -- 10 = 10 AM
local check_mm = 00
local Max_NoAct = 4 * 60 *60   -- 4 hours = 4*60*60 seconds
local switchName1 = "On signal motion sensor"
local switchName2 = "door switch"

----------------------------------------------------------------------------------------
local time = os.date("*t")

-- Calculate the time difference in seconds between Now and supplied time. in YY/MM/DD HH:MM:SS
function datetimedifferencenow(s)
	s = s .. ":00"              -- add seconds in case only hh:mm is supplied
	year = string.sub(s, 1, 4)
	month = string.sub(s, 6, 7)
	day = string.sub(s, 9, 10)
	hour = string.sub(s, 12, 13)
	minutes = string.sub(s, 15, 16)
	seconds = string.sub(s, 18, 19)
	t1 = os.time()
	t2 = os.time{year = year, month = month, day = day, hour = hour, min = minutes, sec = seconds}
	difference = os.difftime(t1, t2)
	return difference
end

-- only perfom the check at defined time
if time.hour == check_hh and time.min == check_mm then
	-- check for last detect activity and when longer send notification
	if datetimedifferencenow(otherdevices_lastupdate[switchName1]) >= Max_NoAct and datetimedifferencenow(otherdevices_lastupdate[switchName2])  >= Max_NoAct then
		commandArray['SendEmail'] = 'Waarschuwing geen beweging#Geen beweging gezien sinds '..otherdevices_lastupdate['switchName']..'#user@domain.com'
	end
end

return commandArray
I will do some further testing and fine tuning before I install it at my mother's home.

Thank you so much master Yoda.
if it was easy everybody would do it

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests