How to use RESTful Sensor availability option

I’m unclear on how to use the availability variable when using the RESTful Sensor. I’ve read through the doc page, but there are no examples and what I’ve found here so far does not definitively tell me how to use it.

I’m either over thinking it or just missing it. Ultimately, I’m wondering if it is something that can help me tell my template sensors to not update if the payload is not there or malformed, or if that JSON part is missing.

Thank you for any direction on this.

Hi ,

I’m not sure but there is a plethora of info on the subject here. Look at the more recent ones first.
Try to put this search term in your search engine:

site:home-assistant.io restful availability

Not really, if the availability template detects those issues it can set the sensor state to unavailable.

You can detect the issues and use this.state in your template to retain the state though.

Great. Thanks for the insight.

I have issue where my rest sensor doesnt receive data and it goes unavailable, but i want that to go 0 instead.
How can i make that happen, it worked in template editor but not when put into sensor.

  - resource: http://192.168.0.46/cgi-bin/api.py
    scan_interval: 60
    timeout: 60
    method: POST
    sensor:
## DEVS ##
      - name: "Innosilicon A6 HB0"
        unique_id: unique_id_innosilicon_a6_hb0
        value_template: " {{ value_json.DEVS[0]['MHS 5s'] | float(0) }} "
        json_attributes_path: "$.DEVS[0]"
        json_attributes:
          - "Status"
          - "Device Elapsed"
          - "Accepted"
          - "Rejected"
          - "TempAVG"
          - "MHS av"
          - "Hardware Errors"
value_template: >
  {% if value_json.DEVS[0]['MHS 5s'] is defined %}
    {{ value_json.DEVS[0]['MHS 5s'] }}
  {% else %}
    0
  {% endif %}

I have tried that, it works in template editor but not in yaml.
But i spot small differense, could that make that… I used {{ 0 }} instead of 0, like this.

value_template: >
  {% if value_json.DEVS[0]['MHS 5s'] is defined %}
    {{ value_json.DEVS[0]['MHS 5s'] }}
  {% else %}
    {{ 0 }}
  {% endif %}

Ill try again and see what happens.

That should not make any difference.

What is the exact error you are getting when the resource is unavailable?

This is error with original yaml code.

WARNING (MainThread) [homeassistant.components.rest.util] Empty reply found when expecting JSON data

ERROR (MainThread) [homeassistant.components.rest.data] Error fetching data: http://192.168.0.46/cgi-bin/api.py failed with All connection attempts failed

Ok so it’s not even getting to the value_template because it cant reach the resource. Not sure there is anything you can do about that, other than work out why http://192.168.0.46/cgi-bin/api.py was offline.

Its offline because device is powered off.
Then i have to think other way to make sensor.
But how about availability: line for resource?

Not going to help. If the integration can’t reach the resource it will raise an error. The availability template is only checked after a response.

I allmost found solution…

  - resource_template: >-
        {% if states("device_tracker.innosilicontechnology_ltd") == 'Home' %}
            http://192.168.0.46/cgi-bin/api.py
        {% else %}
            http://192.168.0.143:8123/config/www/innosilicon/api.py
        {% endif %}
    scan_interval: 60
    timeout: 60
    method: POST

What im trying to do, is access file that has same JSON message, now with zero values, but now im facing new problem…
WARNING (MainThread) [homeassistant.components.rest.util] REST result could not be parsed as JSON
I get this error, i have tested this JSON message with two JSON validator, but problem seems to be that that file can not be accessed.
Im trying to access file inside HA, how i can make this work.

Maybe add:

    headers:
      Content-Type: application/json

I tried that also, but now i got it, it was just to change POST to GET
Also my address was wrong, i had file in /config/www/ and that can be accessed with /local/, atleast that way i did see file with my browser.

Now my config looks like

  - resource_template: >-
        {% if states("device_tracker.innosilicontechnology_ltd") == 'home' %}
            http://192.168.0.46/cgi-bin/api.py
        {% else %}
            http://192.168.0.143:8123/local/community/innosilicon/api.json
        {% endif %}
    headers:
        Content-Type: application/json
    scan_interval: 60
    timeout: 60
    method: GET
1 Like

Nice solution. I’ll have to remember that one.

Im still having some minor hickup…
My setting are

  - resource_template: >-
        {% if states("device_tracker.innosilicontechnology_ltd") == 'home' %}
            http://192.168.0.46/cgi-bin/api.py
        {% else %}
            http://192.168.0.143:8123/local/community/innosilicon/api.json
        {% endif %}
    headers:
        Content-Type: application/json
    scan_interval: 45
    timeout: 120
    method: GET
    sensor:
## DEVS ##
      - name: "Innosilicon A6 HB0"
        unique_id: unique_id_innosilicon_a6_hb0
        unit_of_measurement: "MH/s"
        state_class: "measurement"
        value_template: >-
            {% if value_json.DEVS[0]['MHS 5s'] is defined %}
                {{ value_json.DEVS[0]['MHS 5s'] }}
            {% else %}
                {{ this.state }}
            {% endif %}

And im getting error and sensor goes unavailable
WARNING (MainThread) [homeassistant.components.rest.util] Empty reply found when expecting JSON data
If i look into History, it clearly shows that there is first message and then two empty messages and fourt has data again.
Why im getting unavailable, even i have is defined and this.state ??