RESTful and XML Integration

I edited my suggestion as the OK should be single quoted.

I’ve played around with a similar XML source - it is fussy, but I cannot see why my suggestion won’t work although I wonder if the use of value is the problem.

Sorry I’m out of ideas. Raise an Issue on GitHub?

Alright, thanks for that.

  • “Updating from …”: That indicates that the update from that URL is about to happen
  • Then there is missing a DEBUG message “Data fetched from resource…”.
  • Then it checks for presence of HTTP response headers.
  • Next, because there is neither a message “JSON converted from XML…” nor “Erroneous XML…” I assume that either no HTTP response header has been found at all, or no “content-type” header was found, or the content type header does not start with text/xml or application/xml.
  • The next message “REST result could not be parsed as JSON” occurs because you still have json_attributes configured. But because the conversion from XML to JSON has not actually happened as seen above, this will then of course fail.

A quick test that I have just done is:

  • Create test.xml in my Apache webserver’s root folder and pasted the XML content you posted in your first post.
  • Use the following configuration:
sensor:
  - platform: rest
    resource: http://localhost/test.xml
    name: eta_tanktopp_temp
    device_class: temperature
    value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'
  • This works as expected. Sensor value is set to 79.

So, this all may indicate that your webserver (http://192.168.xxx.xxx:8080/...) does not quite work as expected, i.e. it appears as - despite you getting a correct content-type header in a web browser - the rest integration somehow does not.

Next step that I would take if this was my webserver is to configure the sensor in a developer instance of HA and add more and more DEBUG statements into the code (homeassistant/components/rest/sensor.py) to find out where the HTTP response is not as expected.

2 Likes

I was just reading through the 0.107 release notes and saw that application/xml has only been added to this release (Add application/xml as an XML to JSON auto converted mime types for rest sensors by bdraco · Pull Request #32289 · home-assistant/core · GitHub). That explains why it was working for me (I was running a pre-release of 0.107), but not for you.
Sorry for not checking this earlier. If you upgrade to 0.107, the solution I proposed using value_template: '{{ value_json["eta"]["value"]["@strValue"] }}' should work fine.

1 Like

Ok great, then I wait and try with the 0.107 version

Thanks!

I have now updated to 0.107 and it works perfect :slight_smile:

Thanks a lot!

2 Likes

I have a similar question, and try to readout an xml file.
Does anyone know how to apply the resolution in this topic on my xml file?

<root>

<Device Name="StecaGrid 3010" Type="Inverter" Platform="HSSNet11" HmiPlatform="HMI13" NominalPower="3000" UserPowerLimit="nan" CountryPowerLimit="nan" Serial="748609ZD005345830026" OEMSerial="" BusAddress="1" NetBiosName="INV005345830026" WebPortal="Steca sunCloud" ManufacturerURL="www.steca.com" IpAddress="192.168.1.26" DateTime="2021-05-03T09:09:13">

<Measurements>

<Measurement Value="234.5" Unit="V" Type="AC_Voltage"/>

<Measurement Value="2.703" Unit="A" Type="AC_Current"/>

<Measurement Value="618.6" Unit="W" Type="AC_Power"/>


</Measurements>

</Device>

</root>

I need this value:

<Measurement Value="618.6" Unit="W" Type="AC_Power"/>

Thanks

Try this as a starting point.

value_json['root']['Device']['Measurements']['Measurement'][?(@['@Type']=='AC_Power')]['@Value']

If that doesn’t work, I’d recommend using the link in the first post to convert your XML into JSON, and then use a JSON Path evaluator to play around with the path until it works. And hopefully that will then also work in HA.

1 Like

Thanks for your answer, still some issues with the syntax:

'{{ value_json["root"]["Device"]["Measurements"]["Measurement"][?(@["@Type"]=="AC_Power")]["@Value"] }}'. (
Invalid config for [sensor.rest]: invalid template (TemplateSyntaxError: unexpected char '?' at 63) for dictionary value @ data['value_template']. Got '{{ value_json["root"]["Device"]["Measurements"]["Measurement"][?(@["@Type"]=="AC_Power")]["@Value"] }}'. (See ?, line ?).

Maybe I do something wrong :slight_smile:

There is a chance that the filter syntax using a question mark is not supported here, or may need to be encoded somehow…?

Alternatively, here is a less pretty version that iterates through the remaining array:

{% for x in value_json["root"]["Device"]["Measurements"]["Measurement"] -%}
{% if x["@Type"] == "AC_Power" %}{{x["@Value"]}}{% endif %}
{%- endfor %}
1 Like

How Genius, it works!!!
Thank you !!

Now i can see solar production in real time!

1 Like

Can the value template also be modified if there is no response (at night this ip doesn’t respond). That the value will be 0?

Not sure how exactly the sensor handles this case, but you could try surrounding the above template with an if-expression like this:

{% if value_json is defined %}
... insert template from above...
{% else %}
0
{% endif %}

If the ip does not respond, you will get an error from rest adapter as well in the log. I don’t like this and would like to get a round this error with a clause in the sensor or whatever as well.

Any way to have this without filtering in logger settings, because this is not a special or unique error message to filter.

Maybe by changing the logger component in home assistant?

logger:
  default: info
  logs:
    homeassistant.components.rest.sensor: info

As I said: Any way to have this without filtering in logger settings, because this is not a special or unique error message to filter.

Of course, I want to have still errors for this component or this sensor, but not if this sensor is not available.

Should be possible to round that @Value retrieved?

Thanks

Maybe with the round option?

{{x["@Value"]} | round(1) }

where you can change 1 as needed.

I got it!,

Just in case:

This is the correct format:

{{x[“@Value”] | round(1) }}

Hi,

Do you have an idea to read out this code:

var myChart;var chartData =
{
"labels":
[
"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"],
"datasets":
[
{
"strokeColor": style_strokecolor,
"fillColor": style_fillcolor,
"data": [
8.017000,13.462000,12.060000,18.398000,16.043000,11.227000,12.979000,14.212000,10.465000,11.991000,15.286000,17.721000,0.000000,0.000000,0.000000,
0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,
0.000000]
}
]
}
var max = 25;
var steps = 5;
var input = document.getElementById("inputId");
input.setAttribute("min",   "2020-09");
input.setAttribute("max",   "2021-08");
input.setAttribute("value", "2021-08");
document.getElementById("labelValueId").innerHTML = " 161.867kWh 08.2021";
document.getElementById("buttonPrevId").disabled  = false;
document.getElementById("buttonNextId").disabled  = true;
window.onresize = function(event) { myChart.resize();  }
myChart = new Chart(document.getElementById("canvasId")
.getContext("2d"))
.Bar(chartData,
{
"pointDot": false,
"datasetFill": false,
"scaleOverride": true,
"scaleLabel": "<%=value%> kWh",
"scaleSteps": steps,
"scaleStartValue": 0,
"scaleStepWidth": Math.ceil(max / steps),
"scaleLineColor": style_linecolor,"scaleFontColor":  style_fontcolor,"responsive": true});

labels are the day’s of the month.
and data is the amount of KWH my solar produces that day.
I need a sensor that gets the amount of the current date.

It’s a Java JS file on my solar inverter (gen.yield.month.chart.js).

any info would me great!

I don’t think there is any suitable integration that you could use to parse this JavaScript code.

The way I would approach this is as follows:

  • Develop a simple custom component that fetches this JavaScript file. (May this code here can give you some inspiration, but instead of binary_sensor you want to implement a sensor.)
  • Find the content of the chartData variable.
  • Parse its content as JSON, find today’s value and populate a sensor/entity with the right meta-data.