Lua script to execute post request on virtual switch toggle

Moderator: leecollings

Post Reply
aram
Posts: 5
Joined: Friday 11 November 2016 20:57
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6707
Contact:

Lua script to execute post request on virtual switch toggle

Post by aram »

Hello everyone,

Hope you can help me with this one. I have already spent few days researching the forum and trying different option and have a feeling that I miss something very simple.

Here is the setup:
Raspberry Pi with Domoticz V3.5879

I have got a Particle device which accepts POST requests via web. Usually I would run "curl https://api.spark.io/v1/devices/devicecode/FUNCTION -d access_token=myToken -d params=MyArguments" which perfectly works from terminal (both laptop and mac).

End result of this request is that a pump connected to a relay would either turn on or off depending on the MyArguments.

Following the forum and wiki, I have created Virtual Hardware and a Virtual Device connected to it.
Then I created Virtual switch, type:on\off.

From this step on I had few options:
a) http request in the OnAction field won't work (if I understood correctly) since its a GET request.

b) One can create lua script and put into /scripts/lua/script_device_MyVirtualSwitchName.lua with a following code:

Code: Select all

print('Pump script executed')

commandArray = {}
if (devicechanged['MyVirtualSwitchName'] == 'On')  then
	os.execute('curl -s "https://api.spark.io/v1/devices/DeviceCode/Function -d access_token=myToken -d params=on" ')
end
if (devicechanged['MyVirtualSwitchName'] == 'Off')  then
	os.execute('curl -s "https://api.spark.io/v1/devices/DeviceCode/Function -d access_token=myToken -d params=off" ')
end

return commandArray
but this will result to a loop and still won't run.

c) one can create custom lua script, MyVirtualSwitchName.lua with the same code as above and either call it from OnAction/OffAction fields or use Blockly event with Start Script.
I ended up with numerous errors: 32256, 32512, 512, 256 after going through adding #!/usr/bin/lua at the beginning of script and chmod to 777. Tried adding "> /dev/null &')" to fight 256 error without success and gave up.

d) created 2 bash scripts (one for on and one for off):

Code: Select all

#!/bin/bash
curl -s https://api.spark.io/v1/devices/DeviceCode/Function -d access_token=myToken -d params=off > /dev/null
chmod -ed and referenced them from OnAction and OffAction.
This successfully works.

However, I would love to have one script and preferably in LUA, which ideally I could later upgrade to have more functionality and maybe even parse the json answer. Could you please point to what direction to look to?

Thanks a lot.

Aram
Raspberry PI2 + Aeon Stick Gen5, Motion Sensor, Energie Meter 2e + Fibaro Switches, Motion Sensors, Wall plugs + Everspring Flood Sensors+ RFLink + RFXcom + Oregon temp\hum + Soil sensor + Global Cache + Wemo + Mysensors ESP WiFi Gateway (sensors in dev)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua script to execute post request on virtual switch toggle

Post by simonrg »

In item b try ending the curl command with & this will run the process in background and should return immediately. Google for further details
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
aram
Posts: 5
Joined: Friday 11 November 2016 20:57
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.6707
Contact:

Re: Lua script to execute post request on virtual switch toggle

Post by aram »

simonrg,
ending with & I have tried as well as with > /dev/null &
Raspberry PI2 + Aeon Stick Gen5, Motion Sensor, Energie Meter 2e + Fibaro Switches, Motion Sensors, Wall plugs + Everspring Flood Sensors+ RFLink + RFXcom + Oregon temp\hum + Soil sensor + Global Cache + Wemo + Mysensors ESP WiFi Gateway (sensors in dev)
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Lua script to execute post request on virtual switch toggle

Post by simonrg »

Aram

Looking at your script again, I am not sure that item b is not working anyway, but you just think it is in a loop, as you will see 'Pump script executed' every time any device is activated in Domoticz, not just your pump.

So if you alter it to:

Code: Select all

-- |/domoticz/scripts/lua/script_device_pump.lua
commandArray = {}

if (devicechanged['MyVirtualSwitchName'] == 'On')  then
   os.execute('curl -s "https://api.spark.io/v1/devices/DeviceCode/Function -d access_token=myToken -d params=on" ')
   print('Pump switched on')
elseif (devicechanged['MyVirtualSwitchName'] == 'Off')  then
   os.execute('curl -s "https://api.spark.io/v1/devices/DeviceCode/Function -d access_token=myToken -d params=off" ')
   print('Pump switched off')
end

return commandArray
This will only print in the log when you actually change the state of the 'MyVirtualSwitchName'.

Good luck, Simon
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest