Template a REST sensor JSON attribute to set a default value

Hi all,

I have a REST sensor that returns some JSON attributes, but unfortunately it doesn’t always reply. HA throws this as a warning/error when this happens.

I’m trying to template the JSON attributes to define a default value, but not having much joy :frowning:

- platform: rest
  name: Wall Tablet Info HD7
  resource: !secret rest_url_wall_tablet_info_hd7
  json_attributes:
  - screenBrightness
  - screenOn
  value_template: 'OK'

What is the correct code/format (assuming it’s possible) to specify a default value for each of the JSON attributes above?

Thanks,

J.

You can’t provide default values for those attributes. Those fields aren’t templates.

Probably explains why I’m failing (I have limited knowledge/experience with HA/templates).

I have implemented a workaround by creating two separate sensors who’s states are set based on the JSON response (and templated a default value that way - which I’m hoping works):

- platform: rest
  name: Wall Tablet HD7 Brightness
  resource: !secret rest_url_wall_tablet_info_hd7
  value_template: "{{ value_json['screenBrightness']|default(0) }}"

Obviously this means twice the amount of HTTP requests/replies (one for ScreenOn and one for screenBrightness). Can you think of a way of more efficient way of specifying a default value for the JSON attributes (with a single HTTP request)?

Thanks for your help and speedy reply.

J.

Can’t you fix the actual issue, i.e. that “Wall Tablet HD7” doesn’t always reply?

Use the rest integration instead of the rest sensor integration. You can specify 1 resource and multiple sensors from the 1 resource.

Thanks for the reply.

I didn’t realise you could do that with the rest integration, I thought there was purely rest commands and rest sensors, I didn’t know there was a generic rest integration where you could create sensors based on the single call/response.
It still throws some errors due to the device not responding/etc, but at least my log isn’t being flooded by the sensor rest errors :+1: