What is your configuration?
I tried some different:
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
name: eta_panna_temp
json_attributes_path: $.eta.value.@strValue
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12204
name: eta_panna_begardeffekt
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12242
name: eta_tank_topptemp
json_attributes:
- "eta"
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12244
name: eta_tank_bottentemp
json_attributes:
- eta
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/120/10251/0/0/12207
name: eta_tank_status
json_attributes_path: $.eta.value.@strValue
value_template: '{{ value_json["eta"]["value"]["@strValue"] }}'
All with the same result except if I use the value templet then the state becomes Unknown
As I said before, I think the json_attributes_path
tells the sensor to try XML conversion. You then need a json_attributes
entry (I believe)
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
name: eta_panna_temp
json_attributes_path: "$.eta"
value_template: 'OK'
json_attributes:
- "value"
Unfortunately it didn´t work.
I get the following error message
2020-03-18 15:46:28 DEBUG (SyncWorker_13) [homeassistant.components.rest.sensor] Updating from http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
2020-03-18 15:46:28 WARNING (SyncWorker_13) [homeassistant.components.rest.sensor] REST result could not be parsed as JSON
2020-03-18 15:46:28 DEBUG (SyncWorker_13) [homeassistant.components.rest.sensor] Erroneous JSON: <?xml version="1.0" encoding="utf-8"?>
660
when i tried:
- platform: rest
resource: http://192.168.0.xxx:8080/user/var/24/10301/0/0/12161
name: eta_panna_temp
json_attributes_path: "$.eta.value"
value_template: 'OK'
json_attributes:
- "@strValue"
The value for the sensor is then OK but i dont´t get any attributes to the sensor
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
orapplication/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.
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.
Ok great, then I wait and try with the 0.107 version
Thanks!
I have now updated to 0.107 and it works perfect
Thanks a lot!
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.
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
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 %}
How Genius, it works!!!
Thank you !!
Now i can see solar production in real time!
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