Command line sensor more then 255 characters

Hello. My sensor returns a result longer than 255 characters.
This looks like this:
{“sn”:“xxxxxxxxxxx”,“ver”:“3.006.04”,“type”:14,“Data”:[2445,2471,2443,12,13,13,176,191,199,566,3026,3032,58,56,1758,1719,5000,5001,4998,2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,32340,870,2846,3208,89,2855,1,45,566,256,14867,6664,5896,100,0,35,0,0,0,0,0,0,0,0,0,0,0,0,0,43538,0,26,19,0,0,3491,0,4421,0,18,25,47352,0,34,0,0,0,13565,5,34534,0,0,0,51,0,0,0,0,0,0,0,0,0,1,35,1,29,32,256,3504,2400,264,270,235,224,33,33,43,1620,778,14906,14906,14906,0,0,0,3348,3341,49414,12,20564,12339,18753,12344,18743,12356,14386,20564,12339,18754,12856,18740,12358,14132,20564,12339,18754,12856,18740,12358,13620,20564,12339,18754,12856,18740,12614,13106,0,0,0,0,0,0,0,6403,4099,1282,259,0,32340,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],“Information”:[6.000,14,“H34C06I8261186”,8,1.33,0.00,1.31,1.09,0.00,1]}%

How can I save the result as an attribute, for example?

Try a shell command with a bash script where you can manipulate the data as necessary. Write the output to a file, then put a file sensor on top of that.

Instead of the file sensor you can also call the HA API to push a sensor value from the bash script (to api/states/<entity_id>, but I think the file sensor is easier.

One con is that the file sensor won’t update immediately, due to polling, so you can then also incorporate a file watcher to update the sensor when it changed.

This is probably more convoluted than you expected. I can’t think of a simpler way for now.

I understand what you mean, but I’m not sure I understood how to solve this.
Now I have a sensor that looks like this.

command_line:
  - sensor:
      name: pv
      scan_interval: 10
      command: curl -X POST 192.168.2.92 --data 'optType=ReadRealTimeData&pwd=xxxxxxxx'

The problem is that the query returns a string longer than 255 characters. That is why Home Assistant does not create this sensor. I can’t see it in the developer tools under states. In the log it says this error.

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity ID: sensor.pv. State max length is 255 characters.

I would like to write the result in a file and then read it out again, but I don’t know how.

It’s not clear if what you posted is valid JSON, partly because of the “%” at the end, but also you have not formatted it correctly, so the double-quotes get modified to ones not compatible with JSON.

When you post code, you should surround the lines with three backticks (```).

Assuming it is valid JSON, and you’re getting the data from a URL via a POST, you should be able to use a RESTful sensor (there are two flavors here and here).

Then in the “value_template” you can select something to represent the state, for example:

value_template: "{{ value_json.sn }}"

And specify which attributes you want to pull out, which I assume is mainly “Data”.

value_template: "{{ value_json.sn }}"

This works. I get the serial number stored as a value in the sensor. What I have not understood yet, how do I get the data part as attribute? Or even better would be of course the values separated with “,” as own attributes. Then I could make template sensors that read me the values.

Sorry this is still new to me and I am beginning to understand it.

So this is what I get when I type the command in Terminal on my Mac. You are right the last character % was from my terminal and was not part of the string.

{"sn":"xxxxxxxxx","ver":"3.006.04","type":14,"Data":[2442,2428,2438,11,12,12,135,143,153,431,2941,3144,5,3,149,124,4999,4998,5001,2,0,0,0,0,0,0,0,0,0,0,0,0,0,1,65473,65535,0,0,0,31160,65486,65381,3101,65531,65381,1,43,494,256,7719,7175,5896,100,0,32,0,0,0,0,0,0,0,0,0,0,0,0,0,44081,0,23,20,0,1,3607,0,4546,0,21,0,47925,0,2,0,0,0,16603,5,34697,0,0,0,37,0,0,0,0,0,0,0,0,0,1,42,1,29,38,256,3504,2400,252,300,227,214,32,32,44,1620,778,14906,14906,14906,0,0,0,3246,3232,8257,13,20564,12339,18753,12344,18743,12356,14386,20564,12339,18754,12856,18740,12358,14132,20564,12339,18754,12856,18740,12358,13620,20564,12339,18754,12856,18740,12614,13106,0,0,0,0,0,0,0,4355,7425,1281,1027,0,31160,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],"Information":[6.000,14,"H34C06I8261186",8,1.33,0.00,1.31,1.09,0.00,1]}

I got it. Thank for helping me to understand it.

value_template: "{{ value_json.Data[0] }}"

So I can separate the values and create a sensor for each value.
:grinning: :+1: