Shelly cloud api sensor

hi… I try create sensor for reading api from Shelly cloud…

This is my call

> curl -X POST https://shelly-58-eu.shelly.cloud/device/status -d "id=48sdsdsdea8&auth_key=sdsdsdsd7B7F425ECFAD4CE7144Dsdsdds3E2D280E43EA9063588sdsd315ABD32D39D"

and this is the answer

> {"isok":true,"data":{"online":true,"device_status":{"_updated":"2022-12-15 18:33:42","sys":{"available_updates":[],"mac":"4855asda9CEA8","restart_required":false,"time":"18:42","unixtime":1671126145,"uptime":4,"ram_size":234832,"ram_free":160108,"fs_size":458752,"fs_free":110592,"cfg_rev":17,"kvs_rev":0,"schedule_rev":0,"webhook_rev":0},"ws":{"connected":false},"input:0":{"id":0,"state":false},"ble":[],"v_eve:0":{"ev":"","ttl":-1,"id":0},"mqtt":{"connected":false},"wifi":{"sta_ip":"192.168.5.88","status":"got ip","ssid":"sdadsad","rssi":-53},"id":"48551999cea8","cloud":{"connected":true},"code":"asdasdaP16EU","switch:0":{"id":0,"aenergy":{"by_minute":[30.183,30.655,29.712],"minute_ts":1671129178,"total":38.43},"source":"SHC","output":true,"apower":2,"voltage":227.8,"current":0.017,"temperature":{"tC":46.84,"tF":116.31}},"serial":16yxfc76}}}pi@raspberrypi:~ $

I need sensors:
“apower”

I try with rest sensors. Sensors is created but value not show…

> -platform: rest
> name: “Shelly 1PM power”
> scan_interval: 5
> resource: https://shelly-58-eu.shelly.cloud/device/status -d "id=48asdadasea8&auth_key=asdaU3dWlk2DB6asdaB7F425ECFAD4CE714asda55F3E2D280E43EA906358886CA315asda"
> value_template: ‘{{ value_json.apower}}’
> unit_of_measurement: 'W'

What am I doing wrong?
Thanks for the help.

You need to paste it in code blocks, we can’t use this.
It has incorrect characters and indentations are wrong

I hope it’s okay now

Try;

value_template: "{{ value_json.data.device_status["switch:0"]apower}}"

But I highly doubt it will work since the JSON isn’t valid.

HI ! I try but i get error in HA…

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/configuration.yaml", line 749, column 5
expected <block end>, but found '<scalar>'
in "/config/configuration.yaml", line 754, column 56

I got such instructions on the official site of shelly…
You can look at the composition…

Communication | Shelly Technical Documentation

I can’t believe no one is using a shelly device in another location and monitoring the values via API ???

As I said, if the JSON isn’t valid then there is not much anyone can do.
You can probably parse it using Node red since it has ways to do string manipulation if the JSON isn’t valid

Why you say is JSON not valid ??

This is back info when i call api …

{
    "isok": true,
    "data": {
        "online": true,
        "device_status": {
            "_updated": "2022-12-16 19:43:40",
            "sys": {
                "available_updates": [],
                "mac": "yxc",
                "restart_required": false,
                "time": "18:42",
                "unixtime": 1671126145,
                "uptime": 4,
                "ram_size": 234832,
                "ram_free": 160108,
                "fs_size": 458752,
                "fs_free": 110592,
                "cfg_rev": 17,
                "kvs_rev": 0,
                "schedule_rev": 0,
                "webhook_rev": 0
            },
            "ws": {
                "connected": false
            },
            "input:0": {
                "id": 0,
                "state": false
            },
            "ble": [],
            "v_eve:0": {
                "ev": "",
                "ttl": -1,
                "id": 0
            },
            "mqtt": {
                "connected": false
            },
            "wifi": {
                "sta_ip": "yxc",
                "status": "got ip",
                "ssid": "xy",
                "rssi": -53
            },
            "id": "yxc",
            "cloud": {
                "connected": true
            },
            "code": "sdfg",
            "switch:0": {
                "id": 0,
                "aenergy": {
                    "by_minute": [44.331, 44.803, 45.275],
                    "minute_ts": 1671219778,
                    "total": 783.702
                },
                "source": "SHC",
                "output": true,
                "apower": 2.6,
                "voltage": 226.8,
                "current": 0.016,
                "temperature": {
                    "tC": 51.96,
                    "tF": 125.53
                }
            },
            "serial": 16xy19780.39
        }
    }
}

Maybe help you my code in python, work OK…

Here is python code

import requests
from requests.structures import CaseInsensitiveDict
import json
import threading


url = "https://shelly-58-eu.shelly.cloud/device/status"

headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/x-www-form-urlencoded"

data = "id=xy&auth_key=MTyxycC335F67B7F425ECFyxcyc5F3E2D280E43EA90yxcy315ABD32D39D"

response = requests.post(url, headers=headers, data=data)

data_1 = response.text
parsed_1 = json.loads(data_1)
apower = (parsed_1["data"]["device_status"]["switch:0"]["apower"])
voltage = (parsed_1["data"]["device_status"]["switch:0"]["voltage"])
current = (parsed_1["data"]["device_status"]["switch:0"]["current"])
total = (parsed_1["data"]["device_status"]["switch:0"]["aenergy"]["total"])

print (apower)
print (voltage)
print (current)
print (total)

Can you help me ?

Previously you posted there was something with raspberry after the JSON end.

But even now that it’s not there you still have a serial that is string and not enclosed in " ".
That will make the parse fail

I think problem is ID and Key, He probably doesn’t get the information…

This is my problem, Where should this be entered?

Python :

data = "id=xy&auth_key=MTyxycC335F67B7F425ECFyxcyc5F3E2D280E43EA90yxcy315ABD32D39D"

Curl:

`curl -X POST https://shelly-58-eu.shelly.cloud/device/status -d 
"id=48sdsdsdea8&auth_key=sdsdsdsd7B7F425ECFAD4CE7144Dsdsdds3E2D280E43EA9063588sdsd315ABD32D39D"`

i think , this is in HA incorrect

resource: https://shelly-58-eu.shelly.cloud/device/status -d "id=48asdadasea8&auth_key=asdaU3dWlk2DB6asdaB7F425ECFAD4CE714asda55F3E2D280E43EA906358886CA315asda"

It looks like a GET request, but I’m not sure.

I would assume it’s

resource: "https://shelly-58-eu.shelly.cloud/device/status?id=48asdadasea8&auth_key=asdaU3dWlk2DB6asdaB7F425ECFAD4CE714asda55F3E2D280E43EA906358886CA315asda"
1 Like

yeeeeeees, IT WORKS, you’re the Screenshot 2022-12-17 091046
genius! thank you a thousand times!

1 Like

I really want to do this, but my knowledge of API functionality is poor. I have a remote shelly working with Apple Shortcuts on my phone directly but haven’t figured out how. to get Home Assistant to read a remote shelly using shelly’s cloud API.
ANYONE??

Please elaborate on what needs to be done in HA to achieve this?

You first need to understand the rest API. You should be able to fetch the API response as described in the first post in this thread using Curl. You will find your correct API web adress and auth_key in the shelly app under “user settings → Authorization Cloud Key” and the device id under your device you want to fetch under “device you want to fetch” → settings → device information → device id

When you have the response, paste it into the dev tools template editor in HA and figure out your needed json parsing string. Add {% set value_jason = on the top and %} {{ value_json."your needed parsing string }} at the bottom.

As an example, for me, testing the JSON parse string in the template editor and fetching the humidity value looks like this:

{% set value_json=
{
	"isok":true,
	"data":{
		"online":true,
		"device_status":{
			"ts":1681516631.68,
			"mqtt":{
				"connected":false
			},
			"id":"xxxxxxxxxx",
			"ble":[],
			"_updated":"2023-04-14 23:57:11",
			"ht_ui":[],
			"devicepower:0":{
				"id":0,
				"battery":{
					"V":6.46,
					"percent":100
				},
				"external":{
					"present":false
				}
			},
			"serial":yyyyyyyyyy,
			"wifi":{
				"sta_ip":"192.168.0.164",
				"status":"got ip",
				"ssid":"MoeNabbenN",
				"rssi":-52
			},
			"cloud":{
				"connected":true
			},
			"humidity:0":{
				"id":0,
				"rh":47.7
			},
			"sys":{
				"mac":"xxxxxxxxxxxxx",
				"restart_required":false,
				"time":"01:57",
				"unixtime":1681516631,
				"uptime":3,
				"ram_size":235244,
				"ram_free":160696,
				"fs_size":458752,
				"fs_free":118784,
				"cfg_rev":12,
				"kvs_rev":0,
				"webhook_rev":0,
				"available_updates":[],
				"wakeup_reason":{
					"boot":"deepsleep_wake",
					"cause":"periodic"},
					"wakeup_period":7200
			},
			"ws":{
				"connected":false
			},
			"code":"SNSN-0013A",
			"temperature:0":{
				"id":0,
				"tC":22,
				"tF":71.6
			},
			"_sleeping":true
		}
	}
}%}

{{ value_json.data.device_status['humidity:0'].rh }}

My JSON parsing string is value_json.data.device_status[‘humidity:0’].rh
This will parse out the humidity value (47.7%)

Now you have all the information you need to create the sensors in HA. You now need to create your rest sensor in your yaml config under “sensor:”

Using the same example, I use this config to create my humidity sensor called sensor.cabin_humidity_indoor (exchanged my device id an auth key with “x,y”, change to yours):

sensor:
 - platform: rest
    name: "Cabin humidity indoor"
    device_class: humidity
    unique_id: cabin_humidity_indoor_id
    resource: "https://shelly-68-eu.shelly.cloud/device/status?id=xxxxxx&auth_key=yyyyyyyyy"
    method: POST
    value_template: "{{ value_json.data.device_status['humidity:0'].rh }}"
    unit_of_measurement: "%"
1 Like

How and where to run that python code to get response? How to run curl in windows?

I got server address, key and device Id but where and how to run them?

{
  "isok": true,
  "data": {
    "online": true,
    "device_status": {
      "actions_stats": {
        "skipped": 0
      },
      "ext_temperature": [
        {
          "hwID": "xxxxxxxxxxxxx",
          "tC": 14,
          "tF": 57.2
        }
      ],
      "relays": [
        {
          "ison": false,
          "has_timer": false,
          "timer_started": 0,
          "timer_duration": 0,
          "timer_remaining": 0,
          "source": "http"
        },
        {
          "ison": true,
          "has_timer": false,
          "timer_started": 0,
          "timer_duration": 0,
          "timer_remaining": 0,
          "source": "input"
        }
      ],
      "inputs": [
        {
          "input": 0,
          "event": "",
          "event_cnt": 0
        },
        {
          "input": 0,
          "event": "",
          "event_cnt": 0
        }
      ],
      "time": "22:31",
      "ext_humidity": [],
      "adcs": [
        {
          "voltage": 12.21
        }
      ],
      "ext_sensors": {
        "temperature_unit": "C"
      },
      "fs_size": 233681,
      "ram_free": 39064,
      "wifi_sta": {
        "connected": true,
        "ssid": "GEO",
        "ip": "192.168.51.104",
        "rssi": -37
      },
      "unixtime": 1686259862,
      "ram_total": 50776,
      "cfg_changed_cnt": 0,
      "uptime": 212146,
      "has_update": false,
      "getinfo": {
        "fw_info": {
          "device": "shellyuni-xxxxxxxxxx",
          "fw": "20230503-102354/v1.13.0-g9aed950"
        }
      },
      "cloud": {
        "enabled": true,
        "connected": true
      },
      "mqtt": {
        "connected": false
      },
      "update": {
        "status": "idle",
        "has_update": false,
        "new_version": "20230503-102354/v1.13.0-g9aed950",
        "old_version": "20230503-102354/v1.13.0-g9aed950"
      },
      "_updated": "2023-06-08 21:31:53",
      "fs_free": 146835,
      "mac": "xxxxxxxxxc",
      "serial": xxxxx
    }
  }
}

How to get voltage and temperature out of this?

I managed to sort out yaml and pull out data from different device that is similar as in previous examples, so I’m on right track but still can’t make UNI voltage and temperature phrase properly to show up.
Could someone please help out with it?

As voltage sits in an array (with only one element) that is called adcs you need to first fetch the first element in the array, so try this:

value_json.data.device_status.adcs[0].voltage

Should give you the voltage.

Same with temp, also sits in an array with only one element, so if you want Celsius, use:

value_json.data.device_status.ext_temperature[0].tC

Thank You! It works!!! Finally i can see state of my my remote solar battery in HA!
Now to make it complite need to figure out how to control relay. It is normally in off state and it is used to reboot connected device, so on command and auto off after 5sec on Shelly device. Is there a way to make a button in HA to trigger relay to switch on?