Extracting a key from a (Hikvision) CCTV camera API using RESTful

I’m trying to extract the value of “mode” from my camera using RESTful but it’s returning no data into the sensor. This is what I’ve come up with so far but I have to admit I’m flying blind regarding curl / rest ::

rest:
  - authentication: digest
    username: "username"
    password: "password"
    scan_interval: 60
    method: GET
    resource: "http://192.168.10.201:80/ISAPI/Image/channels/1/mountingScenario"
    headers:
      Content-Type: "application/xml"
    payload: '<?xml version="1.0" encoding="UTF-8"?><MountingScenario><mode>normal</mode></MountingScenario>'
    
    sensor:
      - name: "CameraMountingMode"
        value_template: "{{ value | regex_findall('<mode>(.*?)</mode>') | first }}"


This is the response I get when I use curl ::


curl -k -H "Content-Type:application/xml" -X GET -d '<?xml version="1.0 encoding="UTF-8"?><MountingScenario><mode></mode></MountingScen
ario>' http://192.168.10.201:80/ISAPI/Image/channels/1/mountingScenario --digest -u username:password
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   178    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
<?xml version="1.0" encoding="UTF-8"?>
<MountingScenario version="2.0" xmlns="http://www.std-cgi.com/ver20/XMLSchema">
<mode>custom1</mode>
</MountingScenario>
100   251  100   164  100    87   5646   2995 --:--:-- --:--:-- --:--:--  8641

It’s the value inside the mode key (custom1) that I’m trying to store.

value_template: "{{ value_json["mode"] }}" should do.

HA does an automatic XML to JSON conversion.

Use Free Online XML to JSON Converter - FreeFormatter.com to see what the json will looks like.

That gives an indentation error but thanks for the pointer regarding JSON/XML

value_template: "{{ value_json.MountingScenario.mode }}"

Fixed it and is now working.

Full working code with extraneous parameters removed for people searching (works with Annke cameras too)

rest:
  - authentication: digest
    username: "username"
    password: "password"
    scan_interval: 300
    method: GET
    resource: "http://IPAddress:80/ISAPI/Image/channels/1/mountingScenario"
    headers:
      Content-Type: "application/xml"
    payload: '<?xml version="1.0" encoding="UTF-8"?><MountingScenario><mode>normal</mode></MountingScenario>'
    
    sensor:
      name: "CameraMode"
      value_template: "{{ value_json.MountingScenario.mode }}"

And the curl command for moving to a PTZ preset on these same cameras ::

/usr/bin/curl -u username:password --digest -X PUT "http://IPAddress/ISAPI/PTZCtrl/channels/1/presets/PresetNumber/goto" 

Replace user/pass/IPAddress AND PresetNumber