Need some help with Variable URL and senser template

Hi,
As a newbee, I’m struggling (get stuck) with reading data from an URL that needs variables. After many tries and reading the documentation and inspiration from [Templates in command line sensor + Octopus Energy API], I would like to put my question for help in here

Currently my code is: (in configuration.yaml)

- sensor:
- platform: rest
    name: solar_inverter
    method: GET
    resource_template: >
      {% set startTime = (as_timestamp(now()) - (300)) | timestamp_custom('%Y-%m-%d %H:%m:%s', True) %}
      {% set endTime = (as_timestamp(now()))| timestamp_custom('%Y-%m-%d %H:%m:%s', True) %}
      https://monitoringapi.solaredge.com/equipment/<siteid>/<serialnr>/data?startTime={{starttime}}&endTime={{endtime}}&api_key=<api_key>

the url needs to be like this: https://monitoringapi.solaredge.com/equipment/{{site_id}}/{{serial}}/data?startTime=2020-05-21 00:20:00&endTime=2020-05-21 11:25:00&api_key={{API_KEY}}

Currenlty I have url data between “<>” hardcoded , serialnr, siteid and api_key

this results in an error:

Invalid state encountered for entity id: {entity_id}. "
homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.solar_inverter. State max length is 255 characters.'

I want to read a JSON element from the return data

value_template: '{{ value_json.data.telemetries.inverterMode }}'

But when I add this beneath the previous code, I don’see anything.
How Can I “debug” the URL ( in order to see if it has the correct format).

the goal is to trigger an alarm if the return value equals “FAULT”.

many thanks in advance
What is the thing I’m missing here.

It’s reporting that the value it received from the URL exceeds 255 characters. The entity’s state cannot hold more than 255 characters.

That’s what I do not understand. When reading the URL and xpath I expect a max 6 letter answer.
How Can I examine the URL that is produced by the template?
I expect something like :slight_smile:

{
    "data": {
        "count": 73,
        "telemetries": [
            {
                "date": "2020-05-20 14:06:09",
                "totalActivePower": 0,
                "dcVoltage": 417.867,
                "groundFaultResistance": 11000,
                "powerLimit": 100,
                "totalEnergy": 182168,
                "temperature": 52.4508,
                "inverterMode": "FAULT",
                "operationMode": 0,
                "L1Data": {
                    "acCurrent": 0,
                    "acVoltage": 146.378,
                    "acFrequency": 50,
                    "apparentPower": 0,
                    "activePower": 0,
                    "reactivePower": 0,
                    "cosPhi": 0
                }
            }

This is over 360 characters in length and exceeds the 255 character limit:

{"data":{"count":73,"telemetries":[{"date":"2020-05-2014:06:09","totalActivePower":0,"dcVoltage":417.867,"groundFaultResistance":11000,"powerLimit":100,"totalEnergy":182168,"temperature":52.4508,"inverterMode":"FAULT","operationMode":0,"L1Data":{"acCurrent":0,"acVoltage":146.378,"acFrequency":50,"apparentPower":0,"activePower":0,"reactivePower":0,"cosPhi":0}}

Whatever your template is trying to do to just get “6 characters” out of it doesn’t appear to be working.


EDIT

I just pasted the JSON string you provided into JSONPathfinder and it reported that it is invalid.

Was that just a portion of what is actually received?

Understand, ofcourse, but how do I check if thats what is returned ie the URL is correct?
When I add it all

sensor:
  - platform: rest
    name: solar_inverter
    method: GET
    resource_template: >
      {% set startTime = (as_timestamp(now()) - (300)) | timestamp_custom('%Y-%m-%d %H:%m:%s', True) %}
      {% set endTime = (as_timestamp(now()))| timestamp_custom('%Y-%m-%d %H:%m:%s', True) %}
      https://monitoringapi.solaredge.com/equipment/acb/123F5/data?startTime={{starttime}}&endTime={{endtime}}&api_key=XXX
      value_template: '{{ value_json.data.telemetries.inverterMode }}'
      content_type: 'application/json; charset=utf-8'

I cannot find an entitiy that looks like “solar_inverter” in the developer tools → states

I think your value_template needs adjustment. telemetries is a list so if you want the first item in the list you must supply an index of zero:

{{ value_json.data.telemetries[0].inverterMode }}

That’s a configuration problem. Run Configuration > Server Controls > Check Configuration.

thanks for pointing me to the malformed xpath to the element! Stupid I didn’t check earlier. Normally i use NP++ for that, but thanks for the online version. I don’t have this option: Run Configuration > Server Controls > Check Configuration? Saving the configuration.yaml did’n report issues. I restarted hassio again, and found no entity.

sorry for my incorrect pasting: it should be as follows.

{
	"data": {
		"count": 73,
		"telemetries": [
			{
				"date": "2020-05-20 14:06:09",
				"totalActivePower": 0,
				"dcVoltage": 417.867,
				"groundFaultResistance": 11000,
				"powerLimit": 100,
				"totalEnergy": 182168,
				"temperature": 52.4508,
				"inverterMode": "FAULT",
				"operationMode": 0,
				"L1Data": {
					"acCurrent": 0,
					"acVoltage": 146.378,
					"acFrequency": 50,
					"apparentPower": 0,
					"activePower": 0,
					"reactivePower": 0,
					"cosPhi": 0
				}
			}
		]
	}
}

As you already posted, indeed the correct xpath is: data.telemetries[0].inverterMode

If the entity is not listed in Developer Tools > States then you need to fix that first, otherwise the rest of this discussion is fruitless.

To make this visible:
Configuration > Server Controls > Check Configuration
your user account must operate in Advanced mode.

Click your account icon (located at the bottom of the lefthand menu in the UI) and enable Advanced Mode.

Screenshot from 2020-05-21 11-24-00

Run Configuration Check and see if it reports any errors. If there are none and the entity is missing then there’s something unusual happening in your system.

Thanks
I updated to the last version of hassio and checked the config with the add-on (after enabling it in Advanced). No issues found.
Still no entity with name: solar_inverter

any clues?

In your first post, the configuration of the sensor begins like this:

- sensor:
- platform: rest

Is that exactly how you have it in the configuration.yaml file?

It should look like this:

sensor:
- platform: rest

I got t working now (at least the right data is coming in). Thanks @123 for pointing me in the right directions. I tested the templates in Developer Tools -> template and these work fine now.

Solution / correct syntax below.
Seems that I had wrongly formatted time Minutes %m which I set to %M
that sensor part of configuration.yaml looks like:

sensor:
# Solar Edge Solarpanels
  - platform: template
    sensors:
      solar_energy_current_power:
        friendly_name: "Current Power"
        unit_of_measurement: 'Wh'
        value_template: "{{ (states('sensor.solaredge_current_power') | float ) | round(2) }}"
      solar_energy_today:
        friendly_name: "Energy Today"
        unit_of_measurement: 'KWh'
        value_template: "{{ (states('sensor.solaredge_energy_today') | float / 1000) | round(1) }}"
      solar_energy_this_month:
        friendly_name: "Energy This Month"
        unit_of_measurement: 'KWh'
        value_template: "{{ (states('sensor.solaredge_energy_this_month') | float / 1000) | round(1) }}"
      solar_energy_this_year:
        friendly_name: "Energy This year"
        unit_of_measurement: 'MWh'
        value_template: "{{ (states('sensor.solaredge_energy_this_year') | float / 1000000) | round(2) }}"
      solar_energy_lifetime_energy:
        friendly_name: "Energy Since"
        unit_of_measurement: 'MWh'
        value_template: "{{ (states('sensor.solaredge_lifetime_energy') | float / 1000000) | round(2) }}"
      solar_energy_earnings:
        friendly_name: "Annual Earning"
        unit_of_measurement: 'EUR'
        value_template: "{{ (states('sensor.solaredge_energy_this_year') | float * 0.00008866) | round(2) }}"
  - platform: rest
    name: solar_inverter
    method: GET
    value_template: '{{ value_json.data.telemetries[0].inverterMode }}'
    resource_template: >-
       {% set startTime = (as_timestamp(now())) | timestamp_custom('%Y-%m-%d %H:%M:00') %}
      {% set endTime = (as_timestamp(now()) + (300)) | timestamp_custom('%Y-%m-%d %H:%M:00') %}
      https://monitoringapi.solaredge.com/equipment/XXXXXXXX/XXXX/data?startTime={{startTime}}&endTime={{endTime}}&api_key=XXXXXXXXX
      value_template: '{{ value_json.data.telemetries[0].inverterMode }}'

Still needs some logic around this, while at night (power falls back to 0 or null the return data is different

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

(I’m still looking for a solid way to monitor my SolarEdge system. Since I have no Admin rights at the portal, I’m told that its not possible to alert us when something is wrong…(amazing right))

The original question concerned the error message you received (received data exceeds 255 characters). That was due to an incorrect template. I provided a template with the correct JSONpath. That was the solution to the original question.

Anyone with a similar question will read your first post and then be led to your Solution that doesn’t directly answer, or explain, the original question (changing the appearance of the time was not the root-cause of the problem). In addition, it is the custom of the community to mark the post that introduces the solution as being the Solution. Your post simply copies the original solution and without explanation.