Availability property for RESTful Sensor

I use the RESTful sensor to gather data from certain devices. However, these devices are not always online. When offline, the sensor yields errors as expected.

I do have knowledge about whether these devices are online, so it would be nice if I could add a template that immediately marks a RESTful sensor as unavailable, which could in turn prevent the sensor to execute the REST call. It will reduce errors in the logs and save resources on polling known-unavailable devices.

Current configuration:

- platform: rest
  name: "prusa_api_sl1s_state"
  authentication: digest
  username: !secret sl1s_user
  password: !secret sl1s_password
  scan_interval: 30
  force_update: true
  resource: "http://1.2.3.4/api/job"
  value_template: "{{ value_json.state }}"

Suggested configuration:

- platform: rest
  name: "prusa_api_sl1s_state"
  authentication: digest
  username: !secret sl1s_user
  password: !secret sl1s_password
  scan_interval: 30
  force_update: true
  resource: "http://1.2.3.4/api/job"
  value_template: "{{ value_json.state }}"
  availability: "{{ is_state('switch.pdu_sl1s', 'on') }}"

You could set the scan interval to a huge number (years) and automate the update using an automation with a time pattern trigger for every 30 seconds and a condition for your availability. The action to update the entities is:

action:
  - service: homeassistant.update_entity
    target:
      entity_id: sensor.prusa_api_sl1s_state

Note: if you use the rest integration (as opposed to your current rest sensor platform config) you can update many sensors with the one call to a resource. e.g.

rest:
  - authentication: digest
    username: !secret sl1s_user
    password: !secret sl1s_password
    scan_interval: 31540000000
    resource: "http://1.2.3.4/api/job"
    sensor:
      - name: "prusa_api_sl1s_state"
        value_template: "{{ value_json.state }}"

      - name: "Some other sensor on the resource"
        value_template: "{{ value_json.state.blah_blah }}"

      - name: "And another one"
        value_template: "{{ value_json.foo_bar }}"
1 Like

Thanks for thinking along.

Interesting solution. Would it be possible to set the sensor states to unavailable when the devices are unavailable?

Hmm. No. Not without a python script: