Help need for custom template in a rest binary sensor

Hi! Noob in Jinga 2 here.

The rest binary sensor is working but not reading the values correctly.

binary_sensor:
  - platform: rest
    name: Proxmox Status Rest
    unique_id: proxmox_status_rest
    resource: https://10.10.10.13:8006/api2/json/nodes/Proxmox/status
    method: GET
    verify_ssl: false
    value_template: >
      {% if data.cpuinfo.sockets == 1 %}
        on
      {% else %}
        off
      {% endif %}
    headers:
      Authorization: !secret proxmox_token

This is an example of the output: {“data”:{“cpuinfo:”{“sockets”:1,“cores”:4}}}

I’m trying to make it interpret that if the value of sockets is 1 it sould be ON and any other value should be OFF. Any other idea that validates via the rest API that the node is ON or OFF is welcome.

Use value_json to access the json object returned by the rest call.

binary_sensor:
  - platform: rest
    name: Proxmox Status Rest
    unique_id: proxmox_status_rest
    resource: https://10.10.10.13:8006/api2/json/nodes/Proxmox/status
    method: GET
    verify_ssl: false
    value_template: >
      {% if value_json.data.cpuinfo.sockets == 1 %}
        on
      {% else %}
        off
      {% endif %}
    headers:
      Authorization: !secret proxmox_token
1 Like

Thank you very much.