RESTful and XML Integration

@salog thanks for your help. Now it works fine. I’m ignoring the warning in the log.

1 Like

I’m trying to fetch XML data from my wallbox, but the REST endpoint does not return any content type and so the XML to JSON transformation fails. Any hints what I can do here?

If you can’t change the source’s content type, then the only workaround I could think of is to externalise fetching the data and converting it into JSON, for example using a shell script or a custom component.

Maybe an idea in the long term: Extend the rest integration to force a conversion regardless of the content type sent by the source?

1 Like

Thanks, I think the only “good” solutions would be to create a custom component or to “fix” the REST integration.

1 Like

Hello, I have an issue similar to this but XML to JSON seems to work fine. However I cannot get data from JSON attributes as it seems not to find those attributes.

The http request to xxx.xxx.xxx/xml produces the following output:

<xml>
<VERSION_FIRMWARE>2.7.5</VERSION_FIRMWARE>
<HEAP>6736</HEAP>
<ERROR_CONEXION_IOT>5</ERROR_CONEXION_IOT>
<ERROR_MENSAJE_IOT>0</ERROR_MENSAJE_IOT>
<DATO_INDEX>NRO:17</DATO_INDEX>
<DATO_INGRESADO>2-74-0-1-203</DATO_INGRESADO>
<SEGUNDOS_VIVO>3259454</SEGUNDOS_VIVO>
<TIEMPO_EPOCH>1645815737</TIEMPO_EPOCH>
<REAL_TIME>22:02:17</REAL_TIME>
<NRO_SERIE>2109E00025</NRO_SERIE>
<V_BATERIAS>50.64</V_BATERIAS>
<RANGO_CARGA>50</RANGO_CARGA>
<A_IN_AC>0.00</A_IN_AC>
<A_OUT_AC>0.35</A_OUT_AC>
<V_OUT_AC>220</V_OUT_AC>
<V_IN_AC>18</V_IN_AC>
<HZ_IN_AC>132</HZ_IN_AC>
<TEMP_TRAFO_UNO>47</TEMP_TRAFO_UNO>
<TEMP_TRAFO_DOS>47</TEMP_TRAFO_DOS>
<TEMP_ELCA>30</TEMP_ELCA>
<TEMP_BATERIAS>- -</TEMP_BATERIAS>
<MV_COMPENSACION_BATERIA>3</MV_COMPENSACION_BATERIA>
<ESTADO>INVERSOR INVIRTIENDO</ESTADO>
<ESTADO_ANTERIOR>CARGADOR ETAPA 1</ESTADO_ANTERIOR>
<ERROR>SIN ERRORES</ERROR>
<ERROR_ANTERIOR>SIN ERRORES</ERROR_ANTERIOR>
<INVERSOR_HABILITADO>HABILITADO</INVERSOR_HABILITADO>
<CARGADOR_HABILITADO>HABILITADO</CARGADOR_HABILITADO>
<TTA_ESTADO>EN ESPERA</TTA_ESTADO>
<TTA_MODO_DE_TRABAJO>2</TTA_MODO_DE_TRABAJO>
<TTA_TENSION_PARA_ARRANQUE>44.00</TTA_TENSION_PARA_ARRANQUE>
<TTA_TENSION_PARA_PARADA>56.00</TTA_TENSION_PARA_PARADA>
<TTA_SEG_DE_ARRANQUE>3</TTA_SEG_DE_ARRANQUE>
<TTA_ARRANCAR_AHORA>0</TTA_ARRANCAR_AHORA>
<TTA_SEG_DE_CONTACTO>3</TTA_SEG_DE_CONTACTO>
<TTA_REINTENTOS>3</TTA_REINTENTOS>
<TTA_INSTALADO>0</TTA_INSTALADO>
<TTA_SEG_DE_ESPERA>10</TTA_SEG_DE_ESPERA>
<AMP_BATERIAS>1</AMP_BATERIAS>
<MODELO>QM-4048SP-C</MODELO>
<VERSION_SOFTWARE>213</VERSION_SOFTWARE>
<ECUALIZACION_ACTIVADA>DESHABILITADO</ECUALIZACION_ACTIVADA>
<TENSION_NOMINAL>3</TENSION_NOMINAL>
<KWHPORDIA>0.90</KWHPORDIA>
<KWHPORDIA_ANTERIOR>1.34</KWHPORDIA_ANTERIOR>
<KWHPORDIACARGADO>0.00</KWHPORDIACARGADO>
<KWHPORDIACARGADO_ANTERIOR>0.00</KWHPORDIACARGADO_ANTERIOR>
<POTENCIA_SALIDA>77</POTENCIA_SALIDA>
<POTENCIA_ENTRADA>0</POTENCIA_ENTRADA>
<ESTADO_NTP>0</ESTADO_NTP>
<DIA_ACTUAL>6741</DIA_ACTUAL>
<IP_LOCAL>172.16.36.121</IP_LOCAL>
<UPDT_RESULT>- -</UPDT_RESULT>
<MODO_BUSQUEDA>1</MODO_BUSQUEDA>
<V_CONSTANTE>57.60</V_CONSTANTE>
<V_FLOTANTE>53.20</V_FLOTANTE>
<V_ECUALIZACION>62.00</V_ECUALIZACION>
<COMP_TEMP_BATERIAS>327.67</COMP_TEMP_BATERIAS>
<V_MIN_AC>185.00</V_MIN_AC>
<V_MAX_AC>250.00</V_MAX_AC>
<V_MIN_BATERIAS>42.00</V_MIN_BATERIAS>
<V_MAX_BATERIAS>70.00</V_MAX_BATERIAS>
<V_ALARMA_BATERIAS>44.00</V_ALARMA_BATERIAS>
<V_RETORNO_BATERIAS>57.60</V_RETORNO_BATERIAS>
<V_BYPASS_BATERIAS>46.00</V_BYPASS_BATERIAS>
<MODO_FUNCIONAMIENTO>1</MODO_FUNCIONAMIENTO>
<PROTEC_DESCARGA_BAT>0</PROTEC_DESCARGA_BAT>
<A_BANCO_BATERIAS>450</A_BANCO_BATERIAS>
</xml>

The DEBUG log confirms that it is translated to JSON

But I cannot read each individual value.

This is the configuration in use now:

#Rest QMAX Integration
sensor:
  - platform: rest
    resource: http://172.16.36.121/xml
    name: "qmaxinverter"
    json_attributes:
       - "VERSION_FIRMWARE"
       - "HEAP"
       - "ERROR_CONEXION_IOT"
       - "ERROR_MENSAJE_IOT"
       - "DATO_INDEX"
       - "DATO_INGRESADO"
       - "SEGUNDOS_VIVO"
       - "TIEMPO_EPOCH"
       - "REAL_TIME"
       - "NRO_SERIE"
       - "V_BATERIAS"
       - "RANGO_CARGA"
       - "A_IN_AC"
       - "A_OUT_AC"
       - "V_OUT_AC"
       - "V_IN_AC"
       - "HZ_IN_AC"
       - "TEMP_TRAFO_UNO"
       - "TEMP_TRAFO_DOS"
       - "TEMP_ELCA"
       - "TEMP_BATERIAS"
       - "MV_COMPENSACION_BATERIA"
       - "ESTADO"
       - "ESTADO_ANTERIOR"
       - "ERROR"
       - "ERROR_ANTERIOR"
       - "INVERSOR_HABILITADO"
       - "CARGADOR_HABILITADO"
       - "TTA_ESTADO"
       - "TTA_MODO_DE_TRABAJO"
       - "TTA_TENSION_PARA_ARRANQUE"
       - "TTA_TENSION_PARA_PARADA"
       - "TTA_SEG_DE_ARRANQUE"
       - "TTA_ARRANCAR_AHORA"
       - "TTA_SEG_DE_CONTACTO"
       - "TTA_REINTENTOS"
       - "TTA_INSTALADO"
       - "TTA_SEG_DE_ESPERA"
       - "AMP_BATERIAS"
       - "MODELO"
       - "VERSION_SOFTWARE"
       - "ECUALIZACION_ACTIVADA"
       - "TENSION_NOMINAL"
       - "KWHPORDIA"
       - "KWHPORDIA_ANTERIOR"
       - "KWHPORDIACARGADO"
       - "KWHPORDIACARGADO_ANTERIOR"
       - "POTENCIA_SALIDA"
       - "POTENCIA_ENTRADA"
       - "ESTADO_NTP"
       - "DIA_ACTUAL"
       - "IP_LOCAL"
       - "UPDT_RESULT"
       - "MODO_BUSQUEDA"
       - "V_CONSTANTE"
       - "V_FLOTANTE"
       - "V_ECUALIZACION"
       - "COMP_TEMP_BATERIAS"
       - "V_MIN_AC"
       - "V_MAX_AC"
       - "V_MIN_BATERIAS"
       - "V_MAX_BATERIAS"
       - "V_ALARMA_BATERIAS"
       - "V_RETORNO_BATERIAS"
       - "V_BYPASS_BATERIAS"
       - "MODO_FUNCIONAMIENTO"
       - "PROTEC_DESCARGA_BAT"
       - "A_BANCO_BATERIAS"
  - platform: template
    sensors:
      serie:
        value_template: "{{ json_attr('qmaxinverter', 'NRO_SERIE') }}"
      estado:
        value_template: "{{ json_attr('qmaxinverter', 'ESTADO') }}"
      vbat:  
        value_template: "{{ json_attr('qmaxinverter', 'V_BATERIAS') }}"

I’m trying get the title element from this xml file.

This is what I have…

 - platform: rest
   resource: http://weather.gc.ca/rss/warning/on-151_e.xml
   name: Weather Alert
    json_attributes:
      - title
    value_template: "{{ value_json.title }}"

The sensor gives me a value of “unknown”. Any thoughts?

The root element is feed, so you will need to change your template to:

    value_template: "{{ value_json.feed.title }}"

When you change your logging configuration to debug for this integration you can actually see the original XML and parsed JSON:

logger:
  default: info
  logs:
    homeassistant.components.rest: debug
1 Like

Thank you very much! Great tip! I was wondering how to see the converted json file!

Hi again,

I hope it’s ok that I post this here as it is related. I have this XML feed…

<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet href="rss.xsl" type="text/xsl" media="screen"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="http://simcoecountyschoolbus.ca/rss.xml" rel="self" type="application/rss+xml" />
<title>Simcoe County Student Transportation Consortium</title>
<link>http://simcoecountyschoolbus.ca</link>
<description>School Bus Arrival Schedule for Simcoe County</description>
<ttl>1</ttl>
<language>en-us</language>
<pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate>
<lastBuildDate>Mon, 22 Nov 2021 20:55:03 -0500</lastBuildDate>
<item><title>North Zone</title><description>No Cancellations</description><pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate><guid isPermaLink="false">16376325031</guid></item>
<item><title>South Zone</title><description>No Cancellations</description><pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate><guid isPermaLink="false">16376325032</guid></item>
<item><title>Central Zone</title><description>No Cancellations</description><pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate><guid isPermaLink="false">16376325033</guid></item>
<item><title>West Zone</title><description>No Cancellations</description><pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate><guid isPermaLink="false">16376325034</guid></item>
<item><title>Muskoka</title><description>No Cancellations</description><pubDate>Mon, 22 Nov 2021 20:55:03 -0500</pubDate><guid isPermaLink="false">16376325035</guid></item>
</channel>
</rss>

I have setup the following sensor which successfully converts the feed to JSON…

  - platform: rest
    resource: https://simcoecountyschoolbus.ca/rss.xml
    scan_interval: 3600
    name: School Bus Cancellations
    value_template: "{{ value_json.rss.channel.item[0].title }}"

That value template works and gives me “North Zone”. But I would like to get all 5 zone titles and descriptions in one sensor. Is that possible? Thank you very much in advance.

Put them in attributes as you can only have one state, but you can have many attributes.

Hi,
for me this is working well without the Warning. I have used the value_template without “ignorecase=TRUE”.

value_template: '{{ value | regex_findall("strValue=\"([a-zA-Z]+)\"") | first == "Ein" }}'

It also produces much less log entries if you use the “silent” and “show error” switch for the curl GET

command_state: /usr/bin/curl -sS -X GET "http://192.168.0.1:8080/user/var//120/10101/0/0/12080"
1 Like

Hello, I have a rest REST sensor which outputs this in HA “state”:
{ "temp_pool": 27.020000 }

Please how to prepare template_value to get only XX.X °C output? In this case: 27.0
Tried: value_template: “{{ "temp_pool"[’@Value’] }}” without ability to restart HA.
Thank you

value_template: "{{ value_json['temp_pool']|float(0)|round(1) }}"

Nope, shows Unknown after implementing yours value_template.

Huh. Try this, and if it still doesn’t work, please provide the entire sensor configuration, properly formatted.

value_template: "{{ (value|from_json)['temp_pool']|float(0)|round(1) }}"
1 Like

oh, sorry. Works after restart. Didn´t worked after reload of yaml only. Don´t know why…
THANK YOU VERY MUCH.

1 Like