Command line sensor more then 255 characters

Hello. My sensor returns a result longer than 255 characters.
This looks like this:

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.

  - sensor:
      name: pv
      scan_interval: 10
      command: curl -X POST --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: "{{ }}"

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

value_template: "{{ }}"

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.


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: