Assistance with a REST sensor (Converting a working cURL command in HA terminal)

Assuming that the below cURL command (and respective response) in the HA’s terminal:

[core-ssh ~]$ curl --location 'http://192.168.1.2:34400/api/' --header 'Content-Type: application/json' --data '{"cmd": "status"}'             
{                                                                                                                                          
    "epg.source": "XEPG",                                                                                                                    
    "status": true,                                                                                                                          
    "streams.active": 271,                                                                                                                   
    "streams.all": 271,                                                                                                                      
    "streams.xepg": 271,                                                                                                                     
    "url.dvr": "192.168.1.2:34400",                                                                                                          
    "url.m3u": "http://192.168.1.2:34400/m3u/threadfin.m3u",                                                                                 
    "url.xepg": "http://192.168.1.2:34400/xmltv/threadfin.xml",                                                                              
    "version.threadfin": "1.0"
}[core-ssh ~]$ 

QUESTION: How do I capture the above streams.active or streams.all values as HA sensors; or as a sensor’s respective attributes?

The below code is what I have currently in my configuration.yaml. However I’m missing something fundamental; as, the sensor name: threadfin_active_streams isn’t even recognized in HA’s developer tools:

CODE:

sensor:
  - platform: rest
    resource: http://192.168.1.2:34400/api/
    name: Threadfin active streams
    headers:
        Content-Type: application/json
    method: POST
    payload: '{"cmd": "status"}'
    json_attributes_path: "$.."
    json_attributes:
      - streams.active
      - streams.all
      - streams.xepg

Thank you in advance… I think I’m pretty close; however, I’m not sure.

I cannot tell if the request is correct, never done such a thing but…

  • set debug on rest and see what it loads, it should present the json if that is fine
  • the attributes path seems to be plain $, not sure why you added …
  • I am not sure if it accepts a dot in the attribute name, probably not, check with just ‘status’ to see if that works. If the dot is an issue then my solution would be to go command_line with a curl and jq to rework the output…or a replace . by _

You need to set value_template — without that, it’ll try to put the entire response in the state, which is probably over the 255 character limit.

Add

value_template: "{{ value_json['status'] }}"

for example, and delete the unneeded json_attributes_path line, as they’re all in the root of the response.

If that doesn’t sort it, check the logs — I wonder if the dots in the attribute names are causing a problem…?

1 Like

Thanks, @Troon that’s exactly the advice that I needed to get it all working!

Working Code:

sensor:
  - platform: rest
    resource: http://192.168.1.2:34400/api/
    name: Threadfin active streams
    headers:
        Content-Type: application/json
    method: POST
    payload: '{"cmd": "status"}'
    value_template: "{{ value_json['status'] }}"
    json_attributes:
      - streams.active
      - streams.all
      - streams.xepg

Result:

1 Like

Thanks, did not know that…I always use a value_template so never had this issue but good to know for certain cases.