Issues getting data from an external IP address

The enclosed image is copied directly from the HA Help topic: RESTful Sensor.

Here’s the actual code:

sensor:
  - platform: rest
    name: "External IP"
    resource: "https://api.ipify.org/?format=json"
    value_template: "{{ value_json.ip }}"

When you enter the ip address in a browser, you get back: {"ip":"185.98.169.39"}

But when I paste this example code into the Developer tools Template Editor, it displays the result: ‘value_json’ is undefined. Shouldn’t it display the above data?

Since it doesn’t, what other coding is needed to retrieve the result from the ip address such that value_json is filled with data?

Regards,
Mark

It returns your external IP address. Verify this by opening www.myip.com

All you can put into your template editor is a template. For example,
{{ states('sensor.date_time') }} becomes the date and time.

That’s what a template does- it becomes whatever is enclosed in the double braces.

If you paste the example code into your configuration.yaml file then restart Home Assistant, you will find an entity named “External IP” in states.

But- let’s start from the start. Just what do you want to do with rest?

I want to get the xml data from my ControlByWeb X-410 device and read the temperature.

The device ip address that displays the date in a browser is: 192.168.86.33/state.xml
In Home Assistant, I need to load the raw data from that response into a value or variable that I can then parse to isolate the temperature. I want to poll the device every 15 minutes. I then want to be able to use that temp for Automations for skylights to open/close, etc.

FYI, I purchased a YoLink temp/humidity sensor and it already has HA integration set up, so it’s easy to use. I’d like to have the same functionality with my multiple ControlByWeb devices as some have additional features other than temperature (ie relays, binary sensors, etc.).

Regards,

Mark

Only few can guess the content/data that is returned from state.xml … so paste the response in your post, properly formatted of course.

Re-reading gave me more insight to the disconnect. Template is used mostly to fetch the state or value of an entity. You need to create the entity using the rest platform.

As @vingerha says, we need to see the format that [Preformatted text](http://192.168.86.33/state.xml) returns to attempt to create the value_template to match.

<datavalues>
<units>F</units>
<sensor1>54.5</sensor1>
<s1Alrm>0</s1Alrm>
<battery>0</battery>
<batteryVoltage>0.0</batteryVoltage>
<powersource>External</powersource>
</datavalues>

Actually I was incorrect with returned into Home Assistant. I was able to create a simple sensor in yaml and this is the data stored in the State:

{"datavalues":{"units":"F","sensor1":"59.9","s1Alrm":"0","battery":"0","batteryVoltage":"0.0","powersource":"External"}}

so…with the json_attributes_path: "$.datavalues" … you can add the content as individual attributes, see the rest integration … or create individual sensors for them with the value in ‘state’…the latter is needed if you want to track history (e.g. show in graphs).

Thank you all for your help! I finally got it working properly with the temperature now available for adding a condition to Automations. Here is the final code:

sensor:
  - platform: rest
    name: "Garage Exterior Temp"
    resource: "http://192.168.86.33/state.xml"
    unit_of_measurement: °F
    device_class: temperature
    state_class: measurement    
    value_template: "{{ value_json.datavalues.sensor1 }}"

Don’t you have a Router-integration, which provides your external IP

That’s why I asked just what the OP was trying to do.

1 Like

Ok, sorry i didn’t even saw there was alot responses in this topic