Rest Sensor and Json with more levels

Tags: #<Tag:0x00007f326cc5cee8>

Hi All,

I have some rest sensors. But was easy. Now I want have a sensor that contains info from:

ocs > meta > status
ocs > data > nextcloud > system > version
ocs > data > nextcloud > storage > num_files

How do i do this?

###########################################################################################
# SENSOR REST
# https://www.home-assistant.io/integrations/rest/
###########################################################################################
---
platform: rest
resource: https://192.168.100.xxx:xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json
scan_interval: 60
name: "Nextcloud Json"
json_attributes:
  - ???
value_template: "OK"
{
	"ocs": {
		"meta": {
			"status": "ok",
			"statuscode": 200,
			"message": "OK"
		},
		"data": {
			"nextcloud": {
				"system": {
					"version": "19.0.4.2",
					"theme": "",
					"enable_avatars": "yes",
					"enable_previews": "yes",
					"memcache.local": "\\OC\\Memcache\\APCu",
					"memcache.distributed": "none",
					"filelocking.enabled": "yes",
					"memcache.locking": "\\OC\\Memcache\\Redis",
					"debug": "no",
					"freespace": 368117645312,
					"cpuload": [0.17, 0.17, 0.11],
					"mem_total": 3970388,
					"mem_free": 3290748,
					"swap_total": 102396,
					"swap_free": 44176,
					"apps": {
						"num_installed": 36,
						"num_updates_available": 0,
						"app_updates": []
					}
				},
				"storage": {
					"num_users": 1,
					"num_files": 307246,
					"num_storages": 3,
					"num_storages_local": 2,
					"num_storages_home": 1,
					"num_storages_other": 0
				},
				"shares": {
					"num_shares": 0,
					"num_shares_user": 0,
					"num_shares_groups": 0,
					"num_shares_link": 0,
					"num_shares_mail": 0,
					"num_shares_room": 0,
					"num_shares_link_no_password": 0,
					"num_fed_shares_sent": 0,
					"num_fed_shares_received": 0
				}
			},
			"server": {
				"webserver": "Apache\/2.4.38 (Raspbian)",
				"php": {
					"version": "7.3.19",
					"memory_limit": 671088640,
					"max_execution_time": 3600,
					"upload_max_filesize": 2097152
				},
				"database": {
					"type": "mysql",
					"version": "10.3.25",
					"size": 259022848
				}
			},
			"activeUsers": {
				"last5minutes": 1,
				"last1hour": 1,
				"last24hours": 1
			}
		}
	}
}

You can get the status with the value template:

value_template: "{{ value_json.ocs.meta.status }}"

But for the attributes you can only specify one attributes path per rest sensor. So the common part of your path is

json_attributes_path: '$.ocs.data.nextcloud'

And unfortunately you have to grab everything in system and storage:

json_attributes:
  - system
  - storage

You can then extract the two attributes you want (version and num_files) using template sensors.

1 Like

Thanks again. You are my hero

[email protected]:/usr/share/hassio/homeassistant# wget https://192.168.100.xxx:xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json
--2020-11-05 17:07:36--  https://192.168.100.xxx:xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json
Connecting to 192.168.100.xxx:xxx... connected.
ERROR: The certificate of â192.168.100.xxxâ is not trusted.
ERROR: The certificate of â192.168.100.xxxâ doesn't have a known issuer.

Not getting any info because of this error. SO have to search how to solve this first :smiley:

verify_ssl: false

already tried.

Strange. It works for me.

Does it have an http endpoint you can use with the ip address, or a domain name you can use with https?

tried this:

https://192.168.100.xxx:xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json
http://192.168.100.xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json

https://nextcloud.domain.name:xxx/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json
http://nextcloud.domain.name/nextcloud/ocs/v2.php/apps/serverinfo/api/v1/info?format=json

where nextcloud.domain.name is a valid name on the nextcloud server. I can access server via:

https://nextcloud.domain.name:xxx/nextcloud/

port is a different one than standard 443. Is nicely port forwarded in router (otherwise I was not able to access it)

Any reason you are not using this?

This is the list of sensors it produces:

SENSORS = (
    "nextcloud_system_version",
    "nextcloud_system_theme",
    "nextcloud_system_memcache.local",
    "nextcloud_system_memcache.distributed",
    "nextcloud_system_memcache.locking",
    "nextcloud_system_freespace",
    "nextcloud_system_cpuload",
    "nextcloud_system_mem_total",
    "nextcloud_system_mem_free",
    "nextcloud_system_swap_total",
    "nextcloud_system_swap_free",
    "nextcloud_system_apps_num_installed",
    "nextcloud_system_apps_num_updates_available",
    "nextcloud_system_apps_app_updates_calendar",
    "nextcloud_system_apps_app_updates_contacts",
    "nextcloud_system_apps_app_updates_tasks",
    "nextcloud_system_apps_app_updates_twofactor_totp",
    "nextcloud_storage_num_users",
    "nextcloud_storage_num_files",
    "nextcloud_storage_num_storages",
    "nextcloud_storage_num_storages_local",
    "nextcloud_storage_num_storages_home",
    "nextcloud_storage_num_storages_other",
    "nextcloud_shares_num_shares",
    "nextcloud_shares_num_shares_user",
    "nextcloud_shares_num_shares_groups",
    "nextcloud_shares_num_shares_link",
    "nextcloud_shares_num_shares_mail",
    "nextcloud_shares_num_shares_room",
    "nextcloud_shares_num_shares_link_no_password",
    "nextcloud_shares_num_fed_shares_sent",
    "nextcloud_shares_num_fed_shares_received",
    "nextcloud_shares_permissions_3_1",
    "nextcloud_server_webserver",
    "nextcloud_server_php_version",
    "nextcloud_server_php_memory_limit",
    "nextcloud_server_php_max_execution_time",
    "nextcloud_server_php_upload_max_filesize",
    "nextcloud_database_type",
    "nextcloud_database_version",
    "nextcloud_database_version",
    "nextcloud_activeUsers_last5minutes",
    "nextcloud_activeUsers_last1hour",
    "nextcloud_activeUsers_last24hours",
)

Also:

BINARY_SENSORS = (
    "nextcloud_system_enable_avatars",
    "nextcloud_system_enable_previews",
    "nextcloud_system_filelocking.enabled",
    "nextcloud_system_debug",
)

tried that I past, didn’t work also… will try that again… :smiley: