Page 1 of 1
Lua: Trigger a virtual text sensor
Posted: Tuesday 21 July 2015 22:09
by Fanfan
Hi everybody,
I have a virtual text sensor (Home_Context ) that displays the status of the house ( Vacant or Present ), and try to trigger a script on his change of value.
With an API call, the trigger works, but with a change initiate in a lua script with the command " commandArray['UpdateDevice'] = 'idx|0|Present''" it is not working.
As a virtual text sensor has no state, just a value, my script looks like that:
Code: Select all
if (devicechanged[Home_Context]) then
if (otherdevices_svalues[Home_Context] == 'Present') then
elseif (otherdevices_svalues[v_Trigger] == 'Vacant') then
end
end
Sure I can use the API command, but it's just to know if it is possible with commandArray command.
Thank you in advance
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 22 July 2015 9:41
by mrf68
Hi Fanfan,
So you are not looking for a solution like this?
Code: Select all
commandArray['OpenURL'] = "http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEXT"
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 22 July 2015 11:14
by jannl
Looks lik ethe commandarray line is wrong
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 22 July 2015 14:34
by bizziebis
Is Home_Context a variable in your script which refers to a actual sensor?
Otherwise it should be devicechanged['Home_Context']
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 22 July 2015 15:56
by Fanfan
Hi
Thanks, for your feedback.
@mrf68: This is the last solution tested and she works fine. Thank
@jan_nl: My commandarray type " commandArray['UpdateDevice'] = 'idx|0|Present''" for changing the value of the virtual text sensor is good, when a script who use this command is running, the value change. But no event is triggered.
@bizziebis: Yes in my exemple code, Home_Context is a local variable.
My question was more to understand the behavior of virtual text sensor trigger.
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 22 July 2015 16:05
by jannl
you could add some print statements and check the contents of the virtual sensor and check if the conditions of the if-statement is met.
Re: Lua: Trigger a virtual text sensor
Posted: Wednesday 13 April 2016 15:42
by Scones
I've just tested this and it is still the case.
If I update my text virtual sensor using JSON it triggers a LUA event.
If I update my text virtual sensor using LUA it doesn't trigger a LUA event.
Is there any chance of fixing this?
Re: Lua: Trigger a virtual text sensor
Posted: Friday 13 January 2017 15:14
by Scones
This still appears to not work. Is there something I can do about this?
I have the following LUA
Code: Select all
-- Notification data push string
-- print('notification data push string')
function urlencode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
commandArray = {}
if (devicechanged['Notification_String']) then
print("Notification String changed to - " .. devicechanged['Notification_String'])
commandArray['OpenURL'] = "http://{myURL}?value="..urlencode(devicechanged['Notification_String'])
end
return commandArray
If I update the virtual sensor using
Code: Select all
curl -s -i -H Accept: application/json 'http://127.0.0.1:8080/json.htm?type=command¶m=udevice&idx=413&nvalue=0&svalue=This+is+test+8'
the LUA gets triggered and all is great. However if I updating it using
Code: Select all
commandArray[#commandArray+1]={['UpdateDevice'] = '413|0|This is test 9' }
The text sensor still gets updated, but the devicechanged LUA doesn't get triggered.
The whole point of this is to just be able to update the text sensor and automatically get it pushed to the notification server. I don't want to have to do the openURL / urlencode every time.