How to set status based on REST

Hi,
As I’m trying to get data from my SolarEdge API and want some automation based on the resulting data.


first. How can I set a state of the sensor?

  - platform: rest
    name: solar_inverter
    method: GET
    scan_interval: 1200
    #value_template: '{{ value_json.data.telemetries[0].inverterMode }}'
    #value_template: '{{ value_json.data.telemetries[0].L1Data.acVoltage }}'
    #value_template: '{{ value_json.data.count }}'
    value_template: '{{ value_json.value }}'
    json_attributes:
      #- data
      #- data.telemetries[0].inverterMode
      #- data.telemetries[0].L1Data.acVoltage
    resource_template: >-
      {% if is_state('sun.sun','above_horizon') %}
        {% set endTime = (as_timestamp(now()) - (60)) | timestamp_custom('%Y-%m-%d %H:%M:00') %} 
        {% set startTime = (as_timestamp(now()) - (420)) | timestamp_custom('%Y-%m-%d %H:%M:00') %}
        https://monitoringapi.solaredge.com/equipment/abc/123/data.json?startTime={{startTime}}&endTime={{endTime}}&api_key=XXXXX
      {% else %}
        'Sleep Mode'
      {% endif %}

Am I doing this the right way?
I’m only interested in the specific data to set a status (based on a combination ) and trigger an alarm if something is wrong. How do I get this specific data? (I tried combination as remarked lines above, buit without the expected results)

thx in advance

First, change your api key! You posted it here,I’m not really interested in your data, but I was able to use the result to write the code to extract your desired values.

This worked for me:

"{{ value_json.data.telemetries[0].L1Data.acVoltage }}"

What do you get when you try this?

Thanks for pointing me the exposed API key (very sloppy of me, need to work on secrets.yaml). I removed it form the post.
I can retrieve the values from the JSON string as you describe, but I was wondering why no STATE is how or how I should set this based on the returned data.

I’m sorry I don’t really understand what you want to do.
Could you please explain the end goal of this? What state do you want to set?

Hi.
As you can see in the uploaded image: there is no STATE given for the REST sensor. How will this be set to a certain value, depending on the JSON response?

Hope to give you a better understanding of the goal by describing it deeper.

The end goal is to validate if the inverter is oke.
To do this, my thought is to check on conditions:

  • sun above horizon
  • have a count > 0 (if not status: SLEEPING)
    I think That I’ve covered this partly by the polling only after sunrise and before sunset in the resource_template. However, after sunset it gives an UNKNOWN.

Getting a status that can be used later in template for the GUI
Inverter Status is varying MPPT, THROTTLED or FAULT and can only be read when count > 0

So, after sunset I have:

{
    "data": {
        "count": 0,
        "telemetries": []
    }
}

then when sun is starting to get above horizon it starts: “THROTTLED”

{
"data": {
        "count": 1,
        "telemetries": [
                "date": "2020-06-15 05:16:09",
                "totalActivePower": 0,
                "dcVoltage": 20.7669,
                "powerLimit": 0,
                "totalEnergy": 986058,
                "temperature": 29.7652,
                "inverterMode": "THROTTLED",
                "operationMode": 0,
                "L1Data": {
                    "acCurrent": 0,
                    "acVoltage": 232.145,
                    "acFrequency": 50.0013,
                    "apparentPower": 0,
                    "activePower": 0,
                    "reactivePower": 0,
                    "cosPhi": 0
                }
            }
        ]
      }
     }

then the next status, when production is done

{
"data": {
        "count": 1,
        "telemetries": [
               "date": "2020-06-15 05:41:09",
                "totalActivePower": 0,
                "dcVoltage": 369.97,
                "groundFaultResistance": 11000,
                "powerLimit": 100,
                "totalEnergy": 986058,
                "temperature": 35.7711,
                "inverterMode": "MPPT",
                "operationMode": 0,
                "L1Data": {
                    "acCurrent": 1.03152,
                    "acVoltage": 231.508,
                    "acFrequency": 49.9963,
                    "apparentPower": 238.512,
                    "activePower": 0,
                    "reactivePower": 238.319,
                    "cosPhi": 1
                }
            }
        ]
      }
     }

In the end I want to have a Human understandable icon / text displayed in the GUI (like SLEEPING, WAKING UP, PRODUCTION, FAULT)

The sensor’s state is defined using the value_template expression. From your original configuration I can see that you have already tried different expressions. Has none of them yielded any value? It seems that you are on the right path and just need to translate all your conditions into template code.

Actually, it’s not really gone. Look at the upper right hand corner of your first post. See the pencil icon? If you click it, it reveals the history of the post’s modifications.

Contact a moderator and ask them to purge the information.

I’ve made some progress. Thx.
Currently I have configured following. This value_template shows values (for both solar_inverter_mode and solar_inverter_acvoltage) in DeveloperTools -> Template. However it does not show data in real.

sensor:
# Solar Edge Solarpanels
  - platform: template
    sensors:
      solar_inverter_acvoltage:
        friendly_name: 'Solar_Inverter_ACVoltage'
        #value_template: '{{ states.sensor.solar_inverter.attributes.data.telemetries[0].L1Data.acVoltage }}'
        value_template: >-
         {% if is_state('sun.sun','above_horizon') and ((value_json.data.count) > 0) %}
            {{ value_json.data.telemetries[0].L1Data.acVoltage | round(1) }}
         {% else %}
            SLEEP
         {% endif %}
        unit_of_measurement: 'VAC'
      solar_inverter_mode:
        friendly_name: 'Solar_Inverter_Mode'
        value_template: >-
         {% if is_state('sun.sun','above_horizon') and ((value_json.data.count) > 0) %}
           {% if ((value_json.data.telemetries[0].inverterMode) == 'MPPT') %}
              MPPT
           {%endif%}
           {% if ((value_json.data.telemetries[0].inverterMode) == 'FAIL') %}
               ERROR
           {%endif%}
           {% if ((value_json.data.telemetries[0].inverterMode) == 'THROTTLED') %}
                WAKEUP
           {%endif%}
         {% else %}
                SLEEP
         {% endif %}
  - platform: rest
    name: solar_inverter
    method: GET
    scan_interval: 1200
    value_template: '{{ value_json.value }}'
    json_attributes:
      - data
      #- data.telemetries[0].inverterMode
      #- data.telemetries[0].L1Data.acVoltage
    resource_template: >-
      {% if is_state('sun.sun','above_horizon') %}
        {% set endTime = (as_timestamp(now()) - (60)) | timestamp_custom('%Y-%m-%d %H:%M:00') %} 
        {% set startTime = (as_timestamp(now()) - (420)) | timestamp_custom('%Y-%m-%d %H:%M:00') %}
        https://monitoringapi.solaredge.com/equipment/1234/1234/data.json?startTime={{startTime}}&endTime={{endTime}}&api_key=XXX
      {% endif %}


I’m not sure where to set the value_template in order to set the State in order to see a status of sensor.solar_inverter

So, this line in the rest sensor is the one you want to replace. value_json does give you the whole JSON, but your JSON does not have a value, that’s why that sensor does not have any state.

value_template: '{{ value_json.value }}'

The template sensors are not linked to your rest sensor, hence using value_json is not possible there. If you prefer to create separate template sensors then you have to put something like this into your template expressions there: {{ sensor. solar_inverter.attributes.telemetries[0].L1Data.acVoltage }}. Preferably you want to use functions such as state_attr in there, but I don’t know if that for example supports evaluating a JSON path.

Thx a lot. I missed that during copy/paste

I’ve tested the options with following results

value_template: "{{ state_attr('sensor.solar_inverter.attributes.data.telemetries[0].L1Data', 'acVoltage') }}" 

shows state: NONE --> doen’t work

value_template: "{{ states.sensor.solar_inverter.attributes.data.telemetries[0].L1Data.acVoltage }}" 

Shows a valid state. --> does Work :slight_smile:

Regarding the REST sensor
I Understand that value_template: '{{ value_json.value }}' returns the whole JSON. Am I correct that I need to do that this way in order to get data for the template sensors? Or what else to put in the value_template in order to have a state having the value of the key, count

thx

1 Like

Not quite. value_json contains the whole JSON. If you add .value then there must be a key value in the JSON payload, otherwise the result will be empty.

No, your template sensors retrieve data from the rest sensor’s attributes which you have configured with an simple json_attributes: line that just picks the whole JSON as an attribute. Alternatively you could define top-level keys present in the JSON payload to be converted to sensor attributes.

Please note that the state’s length is limited so you can’t keep the whole JSON as a state. Looking at your sample JSON you could for example try value_template: '{{ value_json.data.count }}'.

BTW: There are several examples of combinations of rest and template sensors in the documentation.

thx. that did the trick! I had read the docs you referred to, but did not quit understand (couldn’t get it clear) this particular piece.(I can now continue with the GUI part).

the following code is the working end result:

sensor:
# Solar Edge Solarpanels
  - platform: template
    sensors:
      solar_inverter_acvoltage:
        friendly_name: 'Solar_Inverter_ACVoltage'
        value_template: >-
        {% if is_state('sun.sun','above_horizon') and (states('sensor.solar_inverter') | int > 0) %}
            {{ states.sensor.solar_inverter.attributes.data.telemetries[0].L1Data.acVoltage | round(1) }}
         {% else %}
            SLEEP
         {% endif %}
        unit_of_measurement: 'VAC'
      solar_inverter_mode:
        friendly_name: 'Solar_Inverter_Mode'
        value_template: >-
         {% if is_state('sun.sun','above_horizon') and (states('sensor.solar_inverter') | int > 0) %}
           {% if ((states.sensor.solar_inverter.attributes.data.telemetries[0].inverterMode) == 'MPPT') %}
              MPPT
           {%endif%}
           {% if ((states.sensor.solar_inverter.attributes.data.telemetries[0].inverterMode) == 'FAIL') %}
               ERROR
           {%endif%}
           {% if ((states.sensor.solar_inverter.attributes.data.telemetries[0].inverterMode) == 'THROTTLED') %}
                WAKEUP
           {%endif%}
         {% else %}
                SLEEP
         {% endif %}      
  - platform: rest
    name: solar_inverter
    method: GET
    scan_interval: 1200
    value_template: '{{ value_json.data.count }}'
    json_attributes:
      - data
    resource_template: >-
      {% if is_state('sun.sun','above_horizon') %}
        {% set endTime = (as_timestamp(now()) - (60)) | timestamp_custom('%Y-%m-%d %H:%M:00') %} 
        {% set startTime = (as_timestamp(now()) - (420)) | timestamp_custom('%Y-%m-%d %H:%M:00') %}
        https://monitoringapi.solaredge.com/equipment/xxxx/xxxx/data.json?startTime={{startTime}}&endTime={{endTime}}&api_key=xxxx
      {% endif %}
1 Like