Help with state_attr

Hey everyone,

Still trying to get the hang of HA, I’ve read through the documentation but am still having trouble defining a template to pull an attribute of a sensor. So I have this sensor:

And I am trying to pull out the WNow attribute. I’ve set up this template, but it responds with null.

{{ state_attr('sensor.solar_api_productions', 'WNow') }}

Any idea’s what I am doing wrong?

Look at the sensor in the Developer Tools → States menu. What is shown in the right hand column?

Also does this really have an “s” on the end?

sensor.solar_api_productions
1 Like

State is blank, but the attributes have values:

Which integration was used to create this sensor?

I used Restful to pull from my enphase system using an API call.

Here is the code I used to generate that:

sensor:
      - name: "Solar API Production"
        unique_id: solar_api_production_production
        value_template: "{{ value_json.value }}"
        json_attributes_path: $.production[1]
        json_attributes:
          - wNow
          - whLifetime
          - varhLeadLifetime
          - varhLagLifetime
          - vahLifetime
          - rmsCurrent
          - rmsVoltage
          - reactPwr
          - apprntPwr
          - pwrFactor
          - whToday
          - whLastSevenDays
          - vahToday
          - varhLeadToday
          - varhLagToday

Without seeing the raw returned data I am guessing but if there really is a key called “value” you can’t access it like that. In the context of the template value means the entire raw value. You have to do this to access a specific key called “value”:

value_template: "{{ value_json['value'] }}"

That should fix the state, and maybe then the attributes will be accessible too.

Okay so I fixed the state by just hardcoding the value “OK” in there for the state:

sensor:
      - name: "Solar API Production"
        unique_id: solar_api_production_production
        value_template: "{{ "OK" }}"
        json_attributes_path: $.production[1]

So the state is now reading OK, and the attributes still have their value, but the template still is reporting null.

Here is a copy of the API output from postman:

{
    "production": [
        {
            "type": "inverters",
            "activeCount": 61,
            "readingTime": 1686677300,
            "wNow": 3505,
            "whLifetime": 503564
        },
        {
            "type": "eim",
            "activeCount": 1,
            "measurementType": "production",
            "readingTime": 1686677315,
            "wNow": 12337.654,
            "whLifetime": 506850.003,
            "varhLeadLifetime": 4062.696,
            "varhLagLifetime": 143163.31,
            "vahLifetime": 657712.576,
            "rmsCurrent": 106.54,
            "rmsVoltage": 245.089,
            "reactPwr": -34.016,
            "apprntPwr": 12784.688,
            "pwrFactor": 0.97,
            "whToday": 25968.003,
            "whLastSevenDays": 480657.003,
            "vahToday": 40354.576,
            "varhLeadToday": 163.696,
            "varhLagToday": 15101.31,
            "lines": [
                {
                    "wNow": 6173.031,
                    "whLifetime": 253371.51,
                    "varhLeadLifetime": 1985.219,
                    "varhLagLifetime": 71561.673,
                    "vahLifetime": 328705.962,
                    "rmsCurrent": 53.266,
                    "rmsVoltage": 122.613,
                    "reactPwr": -16.208,
                    "apprntPwr": 6395.309,
                    "pwrFactor": 0.97,
                    "whToday": 12983.51,
                    "whLastSevenDays": 240275.51,
                    "vahToday": 20170.962,
                    "varhLeadToday": 79.219,
                    "varhLagToday": 7551.673
                },
                {
                    "wNow": 6164.623,
                    "whLifetime": 253478.493,
                    "varhLeadLifetime": 2077.477,
                    "varhLagLifetime": 71601.637,
                    "vahLifetime": 329006.614,
                    "rmsCurrent": 53.274,
                    "rmsVoltage": 122.477,
                    "reactPwr": -17.809,
                    "apprntPwr": 6389.379,
                    "pwrFactor": 0.97,
                    "whToday": 12984.493,
                    "whLastSevenDays": 240381.493,
                    "vahToday": 20183.614,
                    "varhLeadToday": 84.477,
                    "varhLagToday": 7549.637
                }
            ]
        }
    ],
    "consumption": [
        {
            "type": "eim",
            "activeCount": 1,
            "measurementType": "total-consumption",
            "readingTime": 1686677315,
            "wNow": 8726.829,
            "whLifetime": 548359.324,
            "varhLeadLifetime": 56837.031,
            "varhLagLifetime": -112314.571,
            "vahLifetime": 356435.183,
            "rmsCurrent": 73.764,
            "rmsVoltage": 244.688,
            "reactPwr": 264.274,
            "apprntPwr": 9023.963,
            "pwrFactor": 0.99,
            "whToday": 41893.324,
            "whLastSevenDays": 288.324,
            "vahToday": 27116.183,
            "varhLeadToday": 6864.031,
            "varhLagToday": 0.0,
            "lines": [
                {
                    "wNow": 3830.833,
                    "whLifetime": 281610.842,
                    "varhLeadLifetime": 25746.034,
                    "varhLagLifetime": -53642.536,
                    "vahLifetime": 203836.373,
                    "rmsCurrent": 32.576,
                    "rmsVoltage": 122.416,
                    "reactPwr": -166.611,
                    "apprntPwr": 3987.873,
                    "pwrFactor": 0.96,
                    "whToday": 22732.842,
                    "whLastSevenDays": 133.842,
                    "vahToday": 16205.373,
                    "varhLeadToday": 2887.034,
                    "varhLagToday": 0.0
                },
                {
                    "wNow": 4895.995,
                    "whLifetime": 266748.482,
                    "varhLeadLifetime": 31090.997,
                    "varhLagLifetime": -58672.035,
                    "vahLifetime": 152598.81,
                    "rmsCurrent": 41.187,
                    "rmsVoltage": 122.272,
                    "reactPwr": 430.885,
                    "apprntPwr": 5036.09,
                    "pwrFactor": 0.97,
                    "whToday": 19160.482,
                    "whLastSevenDays": 154.482,
                    "vahToday": 10910.81,
                    "varhLeadToday": 3977.0,
                    "varhLagToday": 0.0
                }
            ]
        },
        {
            "type": "eim",
            "activeCount": 1,
            "measurementType": "net-consumption",
            "readingTime": 1686677315,
            "wNow": -3610.826,
            "whLifetime": 41555.689,
            "varhLeadLifetime": 60899.727,
            "varhLagLifetime": 30848.738,
            "vahLifetime": 356435.183,
            "rmsCurrent": -32.776,
            "rmsVoltage": 244.688,
            "reactPwr": 298.29,
            "apprntPwr": -4010.608,
            "pwrFactor": -1.0,
            "whToday": 0,
            "whLastSevenDays": 0,
            "vahToday": 0,
            "varhLeadToday": 0,
            "varhLagToday": 0,
            "lines": [
                {
                    "wNow": -2342.198,
                    "whLifetime": 28261.044,
                    "varhLeadLifetime": 27731.253,
                    "varhLagLifetime": 17919.137,
                    "vahLifetime": 203836.373,
                    "rmsCurrent": -20.69,
                    "rmsVoltage": 122.416,
                    "reactPwr": -150.404,
                    "apprntPwr": -2532.724,
                    "pwrFactor": -1.0,
                    "whToday": 0,
                    "whLastSevenDays": 0,
                    "vahToday": 0,
                    "varhLeadToday": 0,
                    "varhLagToday": 0
                },
                {
                    "wNow": -1268.628,
                    "whLifetime": 13294.644,
                    "varhLeadLifetime": 33168.474,
                    "varhLagLifetime": 12929.602,
                    "vahLifetime": 152598.81,
                    "rmsCurrent": -12.087,
                    "rmsVoltage": 122.272,
                    "reactPwr": 448.694,
                    "apprntPwr": -1477.883,
                    "pwrFactor": -1.0,
                    "whToday": 0,
                    "whLastSevenDays": 0,
                    "vahToday": 0,
                    "varhLeadToday": 0,
                    "varhLagToday": 0
                }
            ]
        }
    ],
    "storage": [
        {
            "type": "acb",
            "activeCount": 0,
            "readingTime": 0,
            "wNow": 0,
            "whNow": 0,
            "state": "idle"
        }
    ]
}

Btw thank you for your help.

This:

Should be:

{{ state_attr('sensor.solar_api_productions', 'wNow') }}

(lower case “w”).

Sorry for the late reply, thats frustrating. But thank you!