Make text returned from HTTP call into a sensor

Hi,

I’m just getting started with HA and I wanted to ask if anyone might be able to point a Noob in the right direction?

I have two remote devices that return a simple string when you call via http. AFAIK there is no other API available.

What I am trying to do is setup a sensor that calls “http://192.168.0.28/temp” and then formats the string that is returned into sensor values.

The string that is returned is as follows;

, DHT22, 30.10 , 66.10

Where 30.10 is the temperature in celcius and 66.10 is the humidity.

I tried using the Scrape Sensor but I can’t get it to work as there are no CSS tags in the data returned from the device.

Can anyone help me out?

Thanks

Borto

Take a look at the command line sensor or if this is a rest API then the RestFul Senor.

Thanks Burningstone for the pointer. The command line sensor appears perfect. Now to learn how to use templates to get the individual values :slight_smile:

Cheers

Glad that it’s what you need. If the order of the elements is always the same, you can use this:

Temp:
value_template: '{{ value.split(",")[2] }}'

Humidity:
value_template: '{{ value.split(",")[3] }}'

Wow! Thank you again. I appreciate your prompt responses and willingness to help out.

Hi Burningstone,

I’ve got another related question if you can spare a few minutes. I’ve used the information you have shared and have been able to read the temperature and humidity from my sensor. (Thank you again)

I’ve been playing around with trying to get both values from a single command line call but I just can’t seem to crack the right syntax. Using your help, the code below works fine but I wondered if it was possible, as a learning experience, to do this more efficiently? I’ve found samples on this but they all use JSON formatting and my data is a simple string, not JSON.

# Read temperature from device
  - platform: command_line
    command: python3 -c "import requests; print(requests.get('http://192.168.0.63/temp').text)"
    name: bedroomtemp
    scan_interval: 300
    value_template: '{{ value.split(",")[2] }}'
    unit_of_measurement: "°C"
# Read humidity from device
  - platform: command_line
    command: python3 -c "import requests; print(requests.get('http://192.168.0.63/temp').text)"
    name: bedroomhumidity
    scan_interval: 300
    value_template: '{{ value.split(",")[3] }}'
    unit_of_measurement: "%"

Thanks

Anthony

As far as I know it is not possible to do this in a more efficient way. The only option I see to change things on the sensor side, so that it reports in JSON and then you can use json_attributes.

Thanks. I’m just glad it’s working and your original tip helped enormously as I learn HA.