This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Shelly http command
#1
Hi.

Can someone answer where I do wrong in this script for controling a shelly plug.
The url works as it suppose to when paste it in a web browser.
The shelly plug is just turn OFF either if I put the controling GA to "ON" or "OFF".

Code:
http = require("socket.http")
http.TIMEOUT = 5

ShellyPlugON = "http://192.168.1.51/relay/0?turn=on"
ShellyPlugOFF = "http://192.168.1.51/relay/0?turn=off"

value = grp.getvalue()

if value == true then
  http.request(ShellyPlugON)
  else
  http.request(ShellyPlugOFF)
end
Reply
#2
Try this and check that you get correct URL and response in Logs:
Code:
http = require("socket.http")
http.TIMEOUT = 5

cmd = grp.getvalue() and "on" or "off"
url = "http://192.168.1.51/relay/0?turn=" .. cmd
log(url)

res, err = http.request(url)
log(res, err)

If your device supports MQTT you can use it instead of HTTP: https://forum.logicmachine.net/showthread.php?tid=3010
Reply
#3
(05.12.2022, 12:53)admin Wrote: Try this and check that you get correct URL and response in Logs:
Code:
http = require("socket.http")
http.TIMEOUT = 5

cmd = grp.getvalue() and "on" or "off"
url = "http://192.168.1.51/relay/0?turn=" .. cmd
log(url)

res, err = http.request(url)
log(res, err)

If your device supports MQTT you can use it instead of HTTP: https://forum.logicmachine.net/showthread.php?tid=3010
Thanks I tried your script with the same result as before.
I get this in log for both ON and OFF telegram.

Shelly plug 05.12.2022 16:59:00
* string: http://192.168.1.51/relay/0?turn=off


Shelly plug 05.12.2022 16:59:00
* arg: 1
  * string: {"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"overpower":false,"source":"http"}
* arg: 2
  * number: 200


Shelly plug 05.12.2022 16:59:13
* string: http://192.168.1.51/relay/0?turn=off


Shelly plug 05.12.2022 16:59:13
* arg: 1
  * string: {"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"overpower":false,"source":"http"}
* arg: 2
  * number: 200


Reply
#4
It should be event.getvalue() not grp.getvalue().
Reply
#5
(05.12.2022, 16:02)admin Wrote: It should be event.getvalue() not grp.getvalue().

Aah nice Smile  now it works, Thanks!
Reply
#6
Anyone already tried to implement the Shelly 4PM measurment functionality? The HTTP API is very easy accessable, bit how do I get the current, apower, aenergy for each channel?
(switching the channels is no big deal)

After commanding...
require('socket.http')
socket.http.TIMEOUT = 5
data = socket.http.request('http://xxxx.xxx.xxx.xxx/rpc/Shelly.GetStatus')

if data then
log(data)

... the result is:
string: {"ble":{},"cloud":{"connected":false},"eth":{"ip":"xxx.xxx.xxx.xxx"},"input:0":{"id":0,"state":false},"input:1":{"id":1,"state":false},"input:2":{"id":2,"state":false},"input:3":{"id":3,"state":false},"mqtt":{"connected":true},"switch:0":{"id":0, "source":"init", "output":false, "apower":0.0, "voltage":225.2, "freq":50.0, "current":0.000, "pf":0.00, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490}, "ret_aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490},"temperature":{"tC":20.8, "tF":69.5}},"switch:1":{"id":1, "source":"init", "output":false, "apower":0.0, "voltage":225.2, "freq":50.0, "current":0.000, "pf":0.00, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490}, "ret_aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490},"temperature":{"tC":20.8, "tF":69.5}},"switch:2":{"id":2, "source":"init", "output":false, "apower":0.0, "voltage":225.4, "freq":50.0, "current":0.000, "pf":0.00, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490}, "ret_aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490},"temperature":{"tC":20.8, "tF":69.5}},"switch:3":{"id":3, "source":"init", "output":false, "apower":0.0, "voltage":225.4, "freq":50.0, "current":0.000, "pf":0.00, "aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490}, "ret_aenergy":{"total":0.000,"by_minute":[0.000,0.000,0.000],"minute_ts":1701113490},"temperature":{"tC":20.8, "tF":69.5}},"sys":{"mac":"xxxxxxxx","restart_required":false,"time":"20:31","unixtime":1701113491,"uptime":11903,"ram_size":241544,"ram_free":127636,"fs_size":524288,"fs_free":196608,"cfg_rev":16,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0,"available_updates":{"stable":{"version":"1.0.8"}}},"ui":{},"wifi":{"sta_ip":null,"status":"disconnected","ssid":null,"rssi":0},"ws":{"connected":false}}
Reply
#7
Use this:
Code:
data = require('json').pdecode(data)

log(data['switch:0'].voltage)
log(data['switch:1'].current)
log(data['switch:2'].apower)
Reply
#8
Thanks admin! I am already there, how to write the value to a KNX adress?
Is logging into the LM needed to operate? I prefer to switch logging of, and have te abbiliy to enable it whenever problems are there.
Reply
#9
Just use grp.checkwrite('1/1/1', value) and replace value with sentence inside log(). You can disable logs when not needed, this is used only to test script.
------------------------------
Ctrl+F5
Reply
#10
Yes it works thanks. This is what I made:
require('socket.http')
socket.http.TIMEOUT = 5
data = socket.http.request('http://xxx.xxx.xxx.xxx/rpc/Shelly.GetStatus')

if data then
--log(data)

data = require('json').pdecode(data)
grp.checkwrite('x/x/x', data['switch:0'].voltage)

end
Reply
#11
@Admin still one question: the "aenergy":{"total":0.000 (accumulated energy in Kwh), how do I get this values written to an KNX object?
Reply
#12
Use data['switch:2'].aenergy.total
Reply


Forum Jump: