Trying to make a Rest Rainfall sensor... need help

I am very new to this and I’m trying to add a Rainfall sensor to HA. I have my rainfall data in a database and can retrieve it with a request:

http://192.168.2.230/getRainData.php

Which will return:

{
  "WS_Rain":
  {
     "lastHR":"0",
     "last24":"0",
     "last48":"0",
     "lastWK":"0",
     "lastYR":"4254",
     "lastRainFall":"2022-10-01 05:18:03",
     "lastRainAmount":"72",
     "maxRate":"15",
     "maxRateT":"2019-04-12 17:15:22",
     "maxIntv":"595"
  }
}

My sensor yaml looks like this:

- platform: rest
  name: rainfall_sensor
  resource: http://192.168.2.230/getRainData.php
  #json_attributes_path: "$.['WS_Rain']"
  json_attributes:
    - "lastHR"
    - "last24"
    - "last48"
    - "lastWK"
    - "lastYR"
    - "lastRainFall"
    - "lastRainAmount"
  value_template: "OK"
- platform: template
  sensors:
    rainfall_lasthr:
      value_template: "{{ state_attr('sensor.rainfall_sensor', 'WS_Rain')['lastHR'] | float | multiply(0.0521325) | round(2) }}"
      device_class: distance
      unit_of_measurement: "in"
    rainfall_lastamount:
      value_template: "{{ state_attr('sensor.rainfall_sensor', 'WS_Rain')['lastRainAmount'] | float | multiply(0.0521325) | round(2) }}"
      device_class: distance
      unit_of_measurement: "in"

When I try to display the Rainfall in an entity card, displaying either rainfall_lasthr or rainfall_lastamount it displays a value Unavailable.
What am I doing wrong?

Is the rest sensor itself even working? That is, does it correctly store all those attributes?
The template sensors are either not working because the rest sensor’s attributes are missing, or because that value_template is wrong - you are trying to access a JSON structure that isn’t there, and there is not attribute “WS_Rain”.

I would suggest trying the new rest integration syntax where you can directly specify the sensors you want. Something like this:

rest:
  - resource: http://192.168.2.230/getRainData.php
    sensor:
      - name: "Rainfall last hour"
        value_template: "{{value_json['WS_Rain']['lastHR']}}"
        unit_of_measurement: "in"
      - name: "Rainfall last 24 hours"
        value_template: "{{value_json['WS_Rain']['last24']}}"
        unit_of_measurement: "in"
1 Like

Thanks for replying!

Yes, this is what is in the debug log:

2022-10-09 20:16:08.686 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {"WS_Rain":{"lastHR":"0","last24":"0","last48":"0","lastWK":"0","lastYR":"4254","lastRainFall":"2022-10-01 05:18:03","lastRainAmount":"72","maxRate":"15","maxRateT":"2019-04-12 17:15:22","maxIntv":"595"}}

Ok, I’ll try that syntax and let you know…

Thanks again.

Ok, I tried that syntax, but it didn’t work as I failed to mention the YAML was in my included sensor.yaml file, rather than configuration.yaml so the topology(?) wasn’t correct. However after you sort of nudged in the right direction, this worked:

- platform: rest
  name: rainfall_sensor
  resource: http://192.168.2.230/getRainData.php
  json_attributes:
    - "WS_Rain"
  value_template: "OK"

- platform: template
  sensors:
    rainfall_lasthr:
      value_template: "{{ state_attr('sensor.rainfall_sensor', 'WS_Rain')['lastHR'] | float | multiply(0.0521325) | round(2) }}"
      device_class: distance
      unit_of_measurement: "in"
    rainfall_lastamount:
      value_template: "{{ state_attr('sensor.rainfall_sensor', 'WS_Rain')['lastRainAmount'] | float | multiply(0.0521325) | round(2) }}"
      device_class: distance
      unit_of_measurement: "in"

And returns the correct values to my card.
Thanks for your help!