Or use the rest integration rather than the rest sensor platform. You can have as many sensors as you want from the one resource call. The examples shown here aren’t great, each value_template is just set to “OK” but each one can be a full template like your example above. This negates the need for template sensors to retrieve values from attributes.
rest:
- resource: http://192.168.1.12/status.xml
scan_interval: 60 # the default is 30 seconds if you leave this line out, or change it to what you need.
sensor:
- name: "Rain"
value_template: "{{ value_json.rain }}"
- name: "Rain Density"
value_template: "{{ value_json.raindensity }}"
You should add a unit_of_measurement to that last sensor.
Is the rain sensor state only on or off?
If so it should be a binary sensor. Like this:
rest:
- resource: http://192.168.1.12/status.xml
scan_interval: 60 # the default is 30 seconds if you leave this line out, or change it to what you need.
binary_sensor:
- name: "Rain"
value_template: "{{ value_json.rain == 'on' }}"
sensor:
- name: "Rain Density"
value_template: "{{ value_json.raindensity }}"
Binary sensor templates should resolve to true or false. if the state == ‘on’ then the template evaluates to true, turning the binary sensor on. Any other value will result in the template evaluating to false which will turn off the binary sensor.
Error loading /Users/server/.homeassistant/configuration.yaml: while scanning for the next token found character '%' that cannot start any token in "/Users/server/.homeassistant/rest.yaml", line 6, column 10
In simple terms > indicates the template will be on more than one line. - removes the last carriage return and is almost never needed but seems to be included a lot by people here for some unknown reason. It’s not in any of the document examples.
Play with the radio button options here and observe the result on the lower right of the page:
I’ve just been steered onto using rest vs restful sensor to do exactly the same as above (ie. grabbing multiple values in a single call and creating multiple sensors).
It seems that restful sensor can do something similar via json_attributes, but they are still just attributes of a single sensor.
Given that the restful seems to be able to do everything that restful sensor can do, why even have restful sensor ? Whats it use case ?
Backwards compatibility for people who were using it before the new integration was made. There are a few integrations like this, e.g: template sensor platform vs template integration, legacy groups vs new groups.
One doubt I have regarding the rest platform, and the OP title ‘REST sensor with atributes’ is,
Why does Sensor allow for attributes to be defined, but binary_sensor doesn’t?
I want to have a connectivity binary_sensor setup, but I can’t show details of that connectivity in the same entity. Not sure if this comes after and architectural decision regarding binary_Sensors or just noone has had such request before (smells like a ‘WTH’ candidate then)?