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: "%"
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?
Like this:
Would possibly also work to use a RestFul command but that will end up as an service that you then need to handle:
Thanks for this post! It really helped a lot!
Thanks to everyone else who contributed as well.
I can now control a shelly device that is NOT on the same network as the Home Assitant instance, and receive status updates from the shelly.
Great use cases are:
- Garages or gates that are out of range (Though some internet connections is still required.)
- Garages or gates you have permission to access, where there is internet but no Home Assistant. (Offices, family member homes)
- Basic Monitoring of remote properties
NB: Always beware of the security risks of putting access control onto a cloud system. If you don’t understand them, don’t do it.
Can anyone give me some help?
I’m trying to get the voltage from a Plus Uni
rest:
- resource: https://shelly-77-eu.shelly.cloud/device/status
method: POST
payload: 'auth_key=123456789abcdefg&id=AAAAAAAAAAA'
headers:
User-Agent: Home Assistant
Content-Type: application/x-www-form-urlencoded
scan_interval: 5
sensor:
- name: "Caddy Battery (Cloud API) - Voltage"
value_template: '{{ value_json.data.device_status.voltmeter.100.voltage }}'
device_class: voltage
unit_of_measurement: 'V'
Im sure my value_template is wrong
With recent HA updates this is broken now… not sure how to resolve.
Logger: homeassistant.helpers.template
Source: helpers/template.py:2652
First occurred: 09:41:21 (21 occurrences)
Last logged: 09:51:21
Template variable error: dict object has no element 0 when rendering '{{ value_json.data.device_status.ext_temperature[0].tC }}'
I used to be able to get reading with this, but it is not working anymore…
- platform: rest
name: "Solar Battery"
device_class: voltage
unique_id: solar_battery_id
resource: "https://shelly-31-eu.shelly.cloud/device/status?id=12345678&auth_key=12345678"
method: POST
value_template: "{{ value_json.data.device_status.adcs[0].voltage }}"
unit_of_measurement: "V"
actualy it works, but it is not updating.
Temperature is not working…
{{ value_json.data.device_status.ext_temperature[0].tC }}
Looks like shelly output is different now… Voltage works now, i found that shelly cloud server changed, now updated yaml. Temperature is still not working.
{
"isok": true,
"data": {
"online": false,
"device_status": {
"mqtt": {
"connected": false
},
"wifi_sta": {
"connected": true,
"ssid": "GEO",
"ip": "192.168.55.51",
"rssi": -23
},
"actions_stats": {
"skipped": 0
},
"time": "16:22",
"ext_sensors": {
"temperature_unit": "C"
},
"getinfo": {
"fw_info": {
"device": "shellyuni-12345678",
"fw": "20230913-114521/v1.14.0-gcb84623"
}
},
"update": {
"status": "idle",
"has_update": false,
"new_version": "20230913-114521/v1.14.0-gcb84623",
"old_version": "20230913-114521/v1.14.0-gcb84623",
"beta_version": "20231107-165239/v1.14.1-rc1-g0617c15"
},
"uptime": 495321,
"unixtime": 1717082012,
"ram_total": 50776,
"fs_size": 233681,
"ext_temperature": {
"0": {
"hwID": "12345678",
"tC": 15.31,
"tF": 59.56
}
},
"ext_humidity": {},
"inputs": [
{
"input": 0,
"event": "",
"event_cnt": 0
},
{
"input": 0,
"event": "",
"event_cnt": 0
}
],
"serial": 49980,
"has_update": false,
"mac": "12345678",
"cloud": {
"enabled": true,
"connected": true
},
"ram_free": 39088,
"relays": [
{
"ison": false,
"has_timer": false,
"timer_started": 0,
"timer_duration": 0,
"timer_remaining": 0,
"source": "input"
},
{
"ison": false,
"has_timer": false,
"timer_started": 0,
"timer_duration": 0,
"timer_remaining": 0,
"source": "input"
}
],
"adcs": [
{
"voltage": 14.29
}
],
"fs_free": 147086,
"_updated": "2024-05-30 15:22:01",
"cfg_changed_cnt": 0
}
}
}
so now need to get reading from:
"ext_temperature": {
"0": {
"hwID": "12345678",
"tC": 22.12,
"tF": 71.83
}
},
Is there a solution to this?
Interestingly, the switch (relays) works, but the temperature (ext_temperature) does not.
Template variable error: dict object has no element 0 when rendering ‘{{ value_json.data.device_status.ext_temperature[0].tC | round(1) }}’
You probably need to make it ["0"]
Hi,
I tried all the suggestions what you’ve all posted here, but nothing works.
I have a Shelly Plus Uni and want to see the Voltage in HASS.
Here is the code:
sensor:
- platform: rest
name: "Trabant Garage Batterie"
device_class: Battery
unique_id: trabant_garage_batterie
resource: 'https://shelly-118-eu.shelly.cloud/device/status?id=123456&auth_key=ABCDEFG'
method: POST
value_template: '{{ value_json.data.device_status.adcs[0].voltage }}'
unit_of_measurement: 'V'
When I add the complete ressource string to the address bar in the browser I see the data, this is working.
In HASS I get this:
I would be glad if someone can help me to solve this.
Many thanks in advance
Has no one an answer how to solve this?
It still will not work and I don’t understand why.
Is there anything further to do in HASS to activate REST or something?
Did you try what I posted just above your comment?
Yes, but still not working.
I tried all solutions in this article before I raised my comment.
Do you have any other idea?
I have this in my configuration.yaml
- platform: rest
name: front_water_tank_rest2
unit_of_measurement: "v"
resource: https://shelly-76-eu.shelly.cloud/device/status
method: POST
payload: auth_key=insert your key&id=insert your id
scan_interval: 30
headers:
User-Agent: Home Assistant
Content-Type: application/x-www-form-urlencoded
value_template: "{{ value_json.data.device_status.adcs[0].voltage | default('N/A') }}"
This creates a sensor called sensor.front_water_tank_rest2 and returns the voltage. I have cloud enabled on the device.