Rest Sensor - Mapping CSV to attributes

Hello,

I’m trying to load data from an CSV feed into a sensor. I’m only interested in the latest record. I am using a Restful Sensor and I am already at the point where I have the data in my hands thanks to the value_template attribute. What I want to do now is to map these variables to the attributes of the sensor so that I can use them else there like:

state_attr('sensor.csv', 'some_attribute')

But I don’t know if this is at all possible since I am only thus far that I can set the value of the sensor, but whatever I do, I don’t seem to be able to set the attributes.

The json_attributes option is used to create attributes. In order for it to work, it expects the payload to be in JSON format.

So if the payload is this:

{ "status:" "on", "color": "red", "intensity": 75, "blink": "false" }

and json_attributes is this:

json_attributes:
  - color
  - intensity
  - blink

it will create three attributes (color, intensity, blink) and assign values to them (red, 75, false).

Your restful sensor’s received data is in CSV format, not JSON format, so I don’t see how it’s possible to create attributes using json_attributes.

Yes, I have tinkered with json_attributes and tries to set value_json to a valid JSON. But it does not work.

I don’t understand what you mean. value_json is a variable used in value_template to access the received JSON-formatted data. The received data must already be in JSON-format in order to use value_json.

I thought that perhaps I could get it to work by setting value_json from within value_template but that does not work. Just letting you know what I have tried.

The variable called value is used within a value_template to access the received data. It’s normally used when the data is not in JSON format. If the data is in JSON format (specifically a dictionary, where the data is structured in key-value pairs) then you can use value_json to access each key’s value.

Using the previous example, is this is the received data:

{ "status:" "on", "color": "red", "intensity": 75, "blink": "false" }

then I can access the value of intensity like this:

value_template: "{{ value_json.intensity }}"