Rest Sensor with JSON input

Hi fellas,

I have a new question. I have a xmpp Server running and I would like to use the Rest sensor to control/read out some data from it.#

I have an example here. This is a curl command:

curl -u '[email protected]:password' -X POST -d '{"user": "thorsten", "host": "emevth.no-ip.biz"}' 192.168.1.100:5280/api/user_sessions_info

This is the output:

[{"connection":"c2s_tls","ip":"192.168.1.1","port":63985,"priority":0,"node":"ejabberd@localhost","uptime":16462,"status":"available","resource":"dino.f165760e","statustext":""},{"connection":"c2s_tls","ip":"192.168.1.1","port":49774,"priority":0,"node":"ejabberd@localhost","uptime":15467,"status":"available","resource":"Pix-Art Messenger.ifvW","statustext":"Renovieren.... Renovieren..Mund nochmals Renovieren šŸ¾"},{"connection":"c2s_tls","ip":"192.168.1.1","port":56410,"priority":0,"node":"ejabberd@localhost","uptime":16556,"status":"available","resource":"3825852600576074643603970","statustext":"thorsten is online"}]

Any idea how I can get this into a rest sensor? Please see that I use username and password for the curl command

Thanks for the answers :slight_smile:

So, have you looked at the rest sensor documentation and are missing something in particular? Username, password, authentication, payload - itā€™s all there.

I tried but failedā€¦ could you point me to the right direction? Would be awesome :slight_smile:

Try this as a start:

sensor:
  - platform: rest
    resource: http://192.168.1.100:5280/api/user_sessions_info
    username: '[email protected]'
    password: 'password'
    authentication: 'basic'
    method: POST
    payload: '{"user": "thorsten", "host": "emevth.no-ip.biz"}'
    value_template: '{{ value_json[0]["connection"] }}'

The JSON response appears to be an array, and I donā€™t know what data exactly you want to extract. Your main challenge will be that the sensor stateā€™s size is limited and the whole JSON snippet wonā€™t fit. So, you may need to come up with a smart template to extract the values you are interested in.

That did it! Thanks! I am now able to build all sensors needed for my needs :slight_smile:

1 Like