Core: json_attributes for RESTful sensor not working on nested values

Created on 8 Feb 2018  路  4Comments  路  Source: home-assistant/core

Make sure you are running the latest version of Home Assistant before reporting an issue.

You should only file an issue if you found a bug. Feature and enhancement requests should go in the Feature Requests section of our community forum:

Home Assistant release (hass --version):
0.61.1 (newer versions do not include changes to this sensor)

Python release (python3 --version):
3.6.3

Component/platform:
RESTful sensor (HA installed with AiO installer on a RPi3)

Description of problem:
json_attributes in config seem to be ignored when trying to access nested values.

Expected:
List of json_attributes should allow to retrieve any value within the JSON response, just like value_template does.

Problem-relevant configuration.yaml entries and steps to reproduce:

- platform: rest
  name: "Docker container grafana"
  method: GET
  resource: http://xxx.xxx.xxx.x:xxxx/api/endpoints/1/docker/containers/grafana/json
  value_template: >-
    {% if value_json.State is defined %}
      {{ value_json.State.Status }}
    {% else %}
      n/a
    {% endif %}
  json_attributes:
    - State.StartedAt
  headers:
    Content-Type: application/json
    Authorization: !secret PORTAINER_API_BEARER

The JSON result returned by the resource above is as follows (relevant bit at the end):

{
    "AppArmorProfile": "docker-default",
    "Args": [
        "--config",
        "/etc/grafana/grafana.ini"
    ],
    "Config": {
        "AttachStderr": false,
        "AttachStdin": false,
        "AttachStdout": false,
        "Cmd": [
            "--config",
            "/etc/grafana/grafana.ini"
        ],
        "DDSM": false,
        "Domainname": "",
        "Entrypoint": [
            "/run.sh"
        ],
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExposedPorts": {
            "3000/tcp": {}
        },
        "Hostname": "Set",
        "Image": "grafana/grafana:latest",
        "Labels": {
            "com.docker.compose.config-hash": "0017249bd7ce9161c7c6bef2cf368dd1ef451dc065df7e350f7234c3d9f00d19",
            "com.docker.compose.container-number": "1",
            "com.docker.compose.oneoff": "False",
            "com.docker.compose.project": "influxdbgrafana",
            "com.docker.compose.service": "grafana",
            "com.docker.compose.version": "1.14.0"
        },
        "OnBuild": null,
        "OpenStdin": false,
        "StdinOnce": false,
        "Tty": false,
        "User": "",
        "Volumes": {
            "/etc/grafana": {},
            "/etc/grafana/grafana.ini": {},
            "/var/lib/grafana": {},
            "/var/log/grafana": {}
        },
        "WorkingDir": ""
    },
    "Created": "2018-01-23T03:50:32.783326225Z",
    "Driver": "btrfs",
    "ExecIDs": null,
    "GraphDriver": {
        "Data": null,
        "Name": "btrfs"
    },
    "HostConfig": {
        "AutoRemove": false,
        "Binds": [
            "/volume1/docker/influxdb-grafana/grafana:/var/lib/grafana:rw",
            "/volume1/docker/influxdb-grafana/grafana/grafana.ini:/etc/grafana/grafana.ini:rw"
        ],
        "BlkioDeviceReadBps": null,
        "BlkioDeviceReadIOps": null,
        "BlkioDeviceWriteBps": null,
        "BlkioDeviceWriteIOps": null,
        "BlkioWeight": 0,
        "BlkioWeightDevice": null,
        "CapAdd": null,
        "CapDrop": null,
        "Cgroup": "",
        "CgroupParent": "",
        "ConsoleSize": [
            0,
            0
        ],
        "ContainerIDFile": "",
        "CpuCount": 0,
        "CpuPercent": 0,
        "CpuPeriod": 0,
        "CpuQuota": 0,
        "CpuRealtimePeriod": 0,
        "CpuRealtimeRuntime": 0,
        "CpuShares": 0,
        "CpusetCpus": "",
        "CpusetMems": "",
        "DeviceCgroupRules": null,
        "Devices": null,
        "DiskQuota": 0,
        "Dns": null,
        "DnsOptions": null,
        "DnsSearch": null,
        "Env": [
            "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
        ],
        "ExtraHosts": null,
        "GroupAdd": null,
        "IOMaximumBandwidth": 0,
        "IOMaximumIOps": 0,
        "IpcMode": "",
        "Isolation": "",
        "KernelMemory": 0,
        "Links": null,
        "LogConfig": {
            "Config": {},
            "Type": "db"
        },
        "Memory": 0,
        "MemoryReservation": 0,
        "MemorySwap": 0,
        "MemorySwappiness": -1,
        "NanoCpus": 0,
        "NetworkMode": "host",
        "OomKillDisable": false,
        "OomScoreAdj": 0,
        "PidMode": "",
        "PidsLimit": 0,
        "PortBindings": {},
        "Privileged": false,
        "PublishAllPorts": false,
        "ReadonlyRootfs": false,
        "RestartPolicy": {
            "MaximumRetryCount": 0,
            "Name": "always"
        },
        "Runtime": "runc",
        "SecurityOpt": null,
        "ShmSize": 67108864,
        "UTSMode": "",
        "Ulimits": null,
        "UsernsMode": "",
        "VolumeDriver": "",
        "VolumesFrom": []
    },
    "HostnamePath": "/volume1/@docker/containers/af2e228dd2eb48060e53eed1d3c27e55480c24ae12438e76c7a8e5206cd5c823/hostname",
    "HostsPath": "/volume1/@docker/containers/af2e228dd2eb48060e53eed1d3c27e55480c24ae12438e76c7a8e5206cd5c823/hosts",
    "Id": "af2e228dd2eb48060e53eed1d3c27e55480c24ae12438e76c7a8e5206cd5c823",
    "Image": "sha256:5d1d85717268409e99546b284ebd8d6d9adc0021588808dc5669b4cb63c6159c",
    "LogPath": "/volume1/@docker/containers/af2e228dd2eb48060e53eed1d3c27e55480c24ae12438e76c7a8e5206cd5c823/log.db",
    "MountLabel": "",
    "Mounts": [
        {
            "Destination": "/etc/grafana/grafana.ini",
            "Mode": "rw",
            "Propagation": "",
            "RW": true,
            "Source": "/volume1/docker/influxdb-grafana/grafana/grafana.ini",
            "Type": "bind"
        },
        {
            "Destination": "/etc/grafana",
            "Driver": "local",
            "Mode": "",
            "Name": "c79711b037f2f94a27fcf4969ee8452bd55dffa684fd7e22e55c4a8e35687a0e",
            "Propagation": "",
            "RW": true,
            "Source": "/volume1/@docker/volumes/c79711b037f2f94a27fcf4969ee8452bd55dffa684fd7e22e55c4a8e35687a0e/_data",
            "Type": "volume"
        },
        {
            "Destination": "/var/log/grafana",
            "Driver": "local",
            "Mode": "",
            "Name": "20458e3e6262a00aeb55f7dca16f563b37748ba2054ab616c91adbde40aa4b50",
            "Propagation": "",
            "RW": true,
            "Source": "/volume1/@docker/volumes/20458e3e6262a00aeb55f7dca16f563b37748ba2054ab616c91adbde40aa4b50/_data",
            "Type": "volume"
        },
        {
            "Destination": "/var/lib/grafana",
            "Mode": "rw",
            "Propagation": "",
            "RW": true,
            "Source": "/volume1/docker/influxdb-grafana/grafana",
            "Type": "bind"
        }
    ],
    "Name": "/grafana",
    "NetworkSettings": {
        "Bridge": "",
        "EndpointID": "",
        "Gateway": "",
        "GlobalIPv6Address": "",
        "GlobalIPv6PrefixLen": 0,
        "HairpinMode": false,
        "IPAddress": "",
        "IPPrefixLen": 0,
        "IPv6Gateway": "",
        "LinkLocalIPv6Address": "",
        "LinkLocalIPv6PrefixLen": 0,
        "MacAddress": "",
        "Networks": {
            "host": {
                "Aliases": null,
                "EndpointID": "6f408fa5c346ac7e1db2ce25d842b674df7f805100263a17a7a1d001c1d14ce2",
                "Gateway": "",
                "GlobalIPv6Address": "",
                "GlobalIPv6PrefixLen": 0,
                "IPAMConfig": null,
                "IPAddress": "",
                "IPPrefixLen": 0,
                "IPv6Gateway": "",
                "Links": null,
                "MacAddress": "",
                "NetworkID": "dbeb5e04870c85422f5e8f04264f0d9ea0002def0656fce09cd8aa36ecd7f8ab"
            }
        },
        "Ports": {},
        "SandboxID": "727f324eadeb167f1afc248aa7183c594ad13d027d35b5b9bc347e38f8d59778",
        "SandboxKey": "/var/run/docker/netns/default",
        "SecondaryIPAddresses": null,
        "SecondaryIPv6Addresses": null
    },
    "Path": "/run.sh",
    "ProcessLabel": "",
    "ResolvConfPath": "/volume1/@docker/containers/af2e228dd2eb48060e53eed1d3c27e55480c24ae12438e76c7a8e5206cd5c823/resolv.conf",
    "RestartCount": 0,
    "State": {
        "Dead": false,
        "Error": "",
        "ExitCode": 0,
        "FinishedAt": "0001-01-01T00:00:00Z",
        "FinishedTs": -62135596800,
        "OOMKilled": false,
        "Paused": false,
        "Pid": 9245,
        "Restarting": false,
        "Running": true,
        "StartedAt": "2018-01-23T03:50:43.775362399Z",
        "StartedTs": 1516679443,
        "Status": "running"
    }
}

This is what the sensor looks like:

schermafbeelding 2018-02-08 om 00 52 22

Traceback (if applicable):
No errors to be found in the log.

Additional info:
I'm basically just interested in 2 values within the State JSON object: Status as the state and StartedAt as as attribute of the sensor. I don't want to use the entire State object in either case because it would pollute the recorder with information I don't want/need. Additionally, the State object exceeds 255 characters so it couldn't be set as the state of the sensor anyway.

I looked at #10753 but didn't find any evidence that I'm doing something wrong. Searching around the community forum seems to indicate that this issue is caused by the fact that I'm trying to access a property inside an object, rather than a property directly. I hope my wording here makes sense, as I'm not familiar with the correct terminology.

waiting-for-reply

Most helpful comment

This would be a good addition as many json responses include a status and response dictionary instead of just responding with parent values

All 4 comments

This would be a good addition as many json responses include a status and response dictionary instead of just responding with parent values

Also facing this issues with nested JSON. Any update to an updated fix?

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment :+1:

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sibbl picture sibbl  路  3Comments

sogeniusio picture sogeniusio  路  3Comments

neonandu picture neonandu  路  3Comments

bdraco picture bdraco  路  3Comments

moskovskiy82 picture moskovskiy82  路  3Comments