Logic Machine Forum
Shelly http command - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Shelly http command (/showthread.php?tid=4422)



Shelly http command - victor.back - 03.12.2022

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



RE: Shelly http command - admin - 05.12.2022

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


RE: Shelly http command - victor.back - 05.12.2022

(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





RE: Shelly http command - admin - 05.12.2022

It should be event.getvalue() not grp.getvalue().


RE: Shelly http command - victor.back - 05.12.2022

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

Aah nice Smile  now it works, Thanks!


RE: Shelly http command - ivob - 27.11.2023

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}}


RE: Shelly http command - admin - 28.11.2023

Use this:
Code:
data = require('json').pdecode(data)

log(data['switch:0'].voltage)
log(data['switch:1'].current)
log(data['switch:2'].apower)



RE: Shelly http command - ivob - 28.11.2023

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.


RE: Shelly http command - Daniel - 28.11.2023

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.


RE: Shelly http command - ivob - 28.11.2023

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


RE: Shelly http command - ivob - 30.11.2023

@Admin still one question: the "aenergy":{"total":0.000 (accumulated energy in Kwh), how do I get this values written to an KNX object?


RE: Shelly http command - admin - 30.11.2023

Use data['switch:2'].aenergy.total