[SOLVED] Parsing a json value from an existing entity in a template sensor

I’d just try with sensor.unifi_gateway_wan.attributes.gw_system-stats.mem
It’s just json…

I just happened to have this on my screen when I came across this post looking for something else.

{{ state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ['mem'] }}

Here’s my whole template too if you want it.

  - platform: template
    sensors:
      usg_mem:
        unit_of_measurement: "%"
        icon_template: "mdi:timer"
        friendly_name: Unifi Gateway Memory Usage
        value_template: >
            {{ state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ["mem"] }}

Edit: …also, here’s my template for the ‘Up-time’ if you want it. It took me a while to figure out the math and syntax so maybe this will save you some trouble if you have a use for it:

  - platform: template
    sensors:
      usg_estimated_runtime:
        entity_id: sensor.time
        icon_template: "mdi:timer"
        friendly_name: Unifi Gateway Estimated Runtime
        value_template: >
            {% set day = (state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ["uptime"] | int // 86400) | round() %}
            {% set hr = (state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ["uptime"] | int % 86400) // 3600 %}
            {% set min = (state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ["uptime"] | int % 3600) // 60 %}
            {% if day <= 0 %}
            {% else %}
            {{ day }}{{ ' day' if day == 1 }}{{ ' days' if day > 1 }}{{ ', ' if day and hr and min > 0 }}{{ ' & ' if day and hr > 0 and min < 1 or day and min > 0 and hr < 1 }}
            {%- endif -%}
            {% if hr <= 0 %}
            {%- else -%}
            {{ hr }}{{ ' hour' if hr == 1 }}{{ ' hours' if hr > 1 }}{{ ' & ' if min > 0 }}
            {%- endif -%}
            {% if min <= 0 %}
            {%- else -%}
            {{ min }}{{ ' minute' if min == 1 }}{{ ' minutes' if min > 1 }}
            {% endif %}
1 Like

Hi Jason, thank you so very much. Funny I was still poking around trying to get this figured out and your email came in. Really appreciate it and it worked perfectly as you had listed.

{{ state_attr('sensor.unifi_gateway_wan', 'gw_system-stats') ['mem'] }}

1 Like

Great to hear!

I guess I was just in the right place at the right time. :slight_smile: