Lua: Trigger a virtual text sensor

Moderator: leecollings

Post Reply
Fanfan
Posts: 2
Joined: Tuesday 21 July 2015 20:53
Target OS: NAS (Synology & others)
Domoticz version: 2.2619
Location: France
Contact:

Lua: Trigger a virtual text sensor

Post 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
mrf68

Re: Lua: Trigger a virtual text sensor

Post 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&param=udevice&idx=IDX&nvalue=0&svalue=TEXT"
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Lua: Trigger a virtual text sensor

Post by jannl »

Looks lik ethe commandarray line is wrong
User avatar
bizziebis
Posts: 182
Joined: Saturday 19 October 2013 14:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8805
Location: The Netherlands
Contact:

Re: Lua: Trigger a virtual text sensor

Post by bizziebis »

Is Home_Context a variable in your script which refers to a actual sensor?

Otherwise it should be devicechanged['Home_Context']
Fanfan
Posts: 2
Joined: Tuesday 21 July 2015 20:53
Target OS: NAS (Synology & others)
Domoticz version: 2.2619
Location: France
Contact:

Re: Lua: Trigger a virtual text sensor

Post 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.
jannl
Posts: 673
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Lua: Trigger a virtual text sensor

Post 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.
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua: Trigger a virtual text sensor

Post 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?
Scones
Posts: 47
Joined: Wednesday 10 December 2014 16:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua: Trigger a virtual text sensor

Post 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&param=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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest