Get sensor from XML request responded in JSON

My first post. Until now I’ve got quite good help by reading others posts in this forum but now I’m lost.

The problem is that I need to get info from Trafikverket.se and they only accepts XML-queries. They can, however, send the answer in JSON.

I’ve been stuck now during my xmas-vacation and I would really like som help.

  - platform: command_line
    name: mayas_tag
    command: "curl -kX POST https://api.trafikinfo.trafikverket.se/v2/data.json -H 'accept: application/json' -H 'Content-Type: application/xml' -d \"<REQUEST><LOGIN authenticationkey='d8655420528e4e1a803009fb414e4c2b' /><QUERY objecttype='TrainAnnouncement' schemaversion='1.8' orderby='AdvertisedTimeAtLocation'><FILTER><AND><EQ name='ActivityType' value='Avgang' /><EQ name='LocationSignature' value='Nä' /><EQ name='AdvertisedTrainIdent' value='7503' /><OR><AND><GT name='AdvertisedTimeAtLocation' value='$dateadd(-00:15:00)' /><LT name='AdvertisedTimeAtLocation' value='$dateadd(14:00:00)' /></AND><AND><LT name='AdvertisedTimeAtLocation' value='$dateadd(00:30:00)' /><GT name='EstimatedTimeAtLocation' value='$dateadd(-00:15:00)' /></AND></OR></AND></FILTER><INCLUDE>AdvertisedTrainIdent</INCLUDE><INCLUDE>AdvertisedTimeAtLocation</INCLUDE><INCLUDE>EstimatedTimeAtLocation</INCLUDE><INCLUDE>EstimatedTimeIsPreliminary</INCLUDE><INCLUDE>Canceled</INCLUDE></QUERY></REQUEST>\""
    value_template: "{{ value_json['TrainAnnouncement']['TrainAnnouncement[0]'] }}"
    json_attributes:
      - AdvertisedTimeAtLocation
      - AdvertisedTrainIdent
      - Canceled
      - EstimatedTimeIsPreliminary
    scan_interval: 600

My very long curl is what I think should be written/transformed from

<REQUEST>
      <LOGIN authenticationkey="openapiconsolekey" />
      <QUERY objecttype="TrainAnnouncement" schemaversion="1.8" orderby="AdvertisedTimeAtLocation">
            <FILTER>
                  <AND>
                        <EQ name="ActivityType" value="Avgang" />
                        <EQ name="LocationSignature" value="Nä" />
                        <EQ name="AdvertisedTrainIdent" value="7503" />
                        <OR>
                              <AND>
                                    <GT name="AdvertisedTimeAtLocation" value="$dateadd(-00:15:00)" />
                                    <LT name="AdvertisedTimeAtLocation" value="$dateadd(14:00:00)" />
                              </AND>
                              <AND>
                                    <LT name="AdvertisedTimeAtLocation" value="$dateadd(00:30:00)" />
                                    <GT name="EstimatedTimeAtLocation" value="$dateadd(-00:15:00)" />
                              </AND>
                        </OR>
                  </AND>
            </FILTER>
            <INCLUDE>AdvertisedTrainIdent</INCLUDE>
            <INCLUDE>AdvertisedTimeAtLocation</INCLUDE>
            <INCLUDE>EstimatedTimeAtLocation</INCLUDE>
            <INCLUDE>EstimatedTimeIsPreliminary</INCLUDE>
            <INCLUDE>Canceled</INCLUDE>
      </QUERY>
</REQUEST>

After restarting I finaly got this error

Logger: homeassistant.components.command_line.sensor
Source: components/command_line/sensor.py:122
Integration: command_line (documentation, issues)
First occurred: 00:44:25 (1 occurrences)
Last logged: 00:44:25

Empty reply found when expecting JSON data

I tried just the curl (after changing the $dateadd stuff to just “2023-01-01” to see what happened.

Firstly, there is a single quote after “-d”, but no closing quote (outside <REQUEST> and </REQUEST>).

Secondly, stuff within those XML tags also use single quote, which would cause problems. Rather than using single quotes around the REQUEST tag, use double quotes escaped with backslash (") so they don’t screw up the double quotes around the whole curl. Because I was typing in the curl I didn’t need the backslash, so haven’t tested this specifically.

However, after that I get an authentication error. I would expect you’d need to set some headers to access this site.

Thank you for your reply.
Fixed the missing single quote and changed them to \ " but nothing changed. Still got no respons in form of a json.

However, when I used the same code in cmd I got a respons so something went in the right direction.

{ "RESPONSE":{"RESULT":[{ "ERROR":{"SOURCE":"Request","MESSAGE":"Header Content-Type application/x-www-form-urlencoded is invalid."}}]}}curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: application
curl: (6) Could not resolve host: authenticationkey='secret-key'
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: objecttype='TrainAnnouncement'
curl: (6) Could not resolve host: schemaversion='1.8'
curl: (6) Could not resolve host: orderby='AdvertisedTimeAtLocation'><FILTER><AND><EQ
curl: (6) Could not resolve host: name='ActivityType'
curl: (6) Could not resolve host: value='Avgang'
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='LocationSignature'
curl: (6) Could not resolve host: value='Nä'
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='AdvertisedTrainIdent'
curl: (6) Could not resolve host: value='7503'
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='AdvertisedTimeAtLocation'
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='AdvertisedTimeAtLocation'
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='AdvertisedTimeAtLocation'
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (6) Could not resolve host: name='EstimatedTimeAtLocation'
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL

If that all came back in the body it would not be valid JSON, so “value_json” will be empty. I think you should focus on getting the curl working first. I suspect those “Could not resolve host” messages are because of a problem with quotes, and text after a space is being interpreted as another url to download.