Help with restful URL can't find any examples of this

I have an enigma2 decoder and I’m trying to add a sensor to see what channel TV’s are streaming.
The TV’s will be identified by the IP address which I have statically set

192.168.33.61 = Bedroom TV
192.168.33.243 = Lounge TV
192.168.33.244 = Office TV

As you can see from the below example, 192.168.33.244 has no entry because its currently off and not streaming.

  "streams": [
    {
     "eventname": "Storage Wars", 
     "ip": "192.168.33.61", 
     "ref": "1:0:19:4CB:A:A4:3200000:0:0:0:", 
     "name": "Prime", 
     "type": "S"
    }, 
    {
     "eventname": "", 
     "ip": "192.168.33.243", 
     "ref": "1:0:19:4AA:7:A1:3200000:0:0:0:", 
     "name": "Movies Family", 
     "type": "S"
    }
   ],

I’m trying to use the rest sensor as follows, but not sure how to use the value template to search the json for the IP and then output the relevant channel name

sensor:
  - platform: rest
    resource: http://192.168.33.3/api/about
    name: Bedroom_TV_Stream
    value_template: "{{ value_json.[0].name }}"

You can use “selectattr” to filter the array to the ones with the IP you’re after:

{{ (value_json.streams
  | selectattr('ip', 'eq', '192.168.33.243')
  | first).name }}

Wow you are a legend, this worked in the developer template, but can’t get it to load in configuration

rest:
  - authentication: basic
    username: "user"
    password: "pass"
    scan_interval: 60
    resource: http://192.168.33.3/api/about
    sensor:
        - name: "Master Bedroom Stream Channel"
        value_template: {{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.243') | first).name }}
    sensor:
        - name: "Barry Laptop Stream Channel"
        value_template: {{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.243') | first).name }}

P.s. for anyone (including myself in the future) needing to test this in the dev tools template

{% set value_json = {
 "info": {
  "streams": [
   {
    "eventname": "The Simpsons", 
    "ip": "192.168.33.61", 
    "ref": "1:0:1:40C:16:2F:3200000:0:0:0:", 
    "name": "TVNZ 2", 
    "type": "S"
   }, 
   {
    "eventname": "The Chase", 
    "ip": "192.168.33.243", 
    "ref": "1:0:1:40B:16:2F:3200000:0:0:0:", 
    "name": "TVNZ 1", 
    "type": "S"
   }
  ], 
  "textinputsupport": false, 
  "chipset": "bcm7252s", 
  "model": "UHD QUAD 4K", 
  "friendlychipsettext": "Broadcom 7252s"
 
 }
} 
%}
    value_template: {{ (value_json.info.streams
  | selectattr('ip', 'eq', '192.168.33.243')
  | first).name }}

The “value_template” needs to be a string, so either surround it in double-quotes, or put it on the next line like this:

 value_template: >-
  {{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.243') | first).name }}
1 Like

Thanks, fixed one issue came up with another :stuck_out_tongue:

The system cannot restart because the configuration is not valid: Invalid config for [rest]: [value_template] is an invalid option for [rest]. Check: rest->rest->0->value_template. (See /config/configuration.yaml, line 29).

Does this mean I cannot use rest and do it in a single endpoint hit?

There are two REST sensors (this and this). You’re using the latter, which doesn’t have great documentation and I haven’t used it, so I’m not sure it uses “value_json”. Having said, your problem is probably identation - “value_template” should come under “name”, but because it’s below the “-”, it’s actually under “sensor”. Try changing to:

sensor:
  - name: blahblah
    value_template: >-
      your template

I’ve gone to individual sensors for now which is working; just means I have to make multiple calls to the same endpoint. Tried what you said but looks like it works differently and it doesn’t seem possible to do what I want :confused:

sensor:
  - platform: rest
    name: "Barry Laptop Stream Channel"
    resource: http://192.168.33.3/api/about
    username: "user"
    password: "pass"
    value_template: >-
       {{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.243') | first).name }}
    method: GET
    timeout: 10
  - platform: rest
    name: "Bedroom TV Stream Channel"
    resource: http://192.168.33.3/api/about
    username: "user"
    password: "pass"
    value_template: >-
       {{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.61') | first).name }}
    method: GET
    timeout: 10

You were spot on with indent, found example here How to get multiple sensors for 1 rest call? - #6 by stanekl

Thanks for all your help!

Final config as follows for others:

rest:
  - authentication: basic
    username: "user"
    password: "pass"
    scan_interval: 60
    resource: http://192.168.33.3/api/about
    sensor:
      - name: "Bedroom TV Stream Channel"
        value_template: "{{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.61') | first).name }}"
      - name: "Barry Laptop Stream Channel"
        value_template: "{{ (value_json.info.streams | selectattr('ip', 'eq', '192.168.33.243') | first).name }}"