Why is my curl we sensor only returning 25 character?

I’ve been pulling my hair out for much of the day with this.

My command_line sensor pulls in some data as follows


  - platform: command_line  
    command: 'curl -s http://192.168.1.94/http-read.json?encrypted=1 | xxd -r -p'
    name: candy_washing_machine_100
    scan_interval: 20
    command_timeout: 30

So this is gathering the contents of http://192.168.1.94/http-read.json?encrypted=1

On here is an load of data, in a hex format

7B0D0A09227374617475734C6176617472696365223A7B0D0A09092257694669537461747573223A2231222C0D0A090922457272223A22323535222C0D0A0909224D6163684D64223A2231222C0D0A0909225072223A223135222C0D0A09092250725068223A2230222C0D0A090922534C6576656C223A2232222C0D0A09092254656D70223A223930222C0D0A0909225370696E5370223A2234222C0D0A0909224F707431223A2230222C0D0A0909224F707432223A2230222C0D0A0909224F707433223A2230222C0D0A0909224F707434223A2230222C0D0A0909224F707435223A2230222C0D0A0909224F707436223A2230222C0D0A0909224F707437223A2230222C0D0A0909224F707438223A2230222C0D0A090922537465616D223A2230222C0D0A09092244727954223A2230222C0D0A09092244656C56616C223A22323535222C0D0A09092252656D54696D65223A22313239222C0D0A0909225265636970654964223A2230222C0D0A090922436865636B55705374617465223A2230220D0A097D0D0A7D

Using Hex to ASCII | Hex to Text converter I can convert this back into ASCII and get

{
	"statusLavatrice":{
		"WiFiStatus":"1",
		"Err":"255",
		"MachMd":"1",
		"Pr":"15",
		"PrPh":"0",
		"SLevel":"2",
		"Temp":"90",
		"SpinSp":"4",
		"Opt1":"0",
		"Opt2":"0",
		"Opt3":"0",
		"Opt4":"0",
		"Opt5":"0",
		"Opt6":"0",
		"Opt7":"0",
		"Opt8":"0",
		"Steam":"0",
		"DryT":"0",
		"DelVal":"255",
		"RemTime":"129",
		"RecipeId":"0",
		"CheckUpState":"0"
	}
}

This is the data I would like to get into my sensor.

I believe using " | xxd -r -p" in my curl command is converting the hex to plain text for me, however my sensor is only returning th first 25 characters of this so I get incomplete JSON meaning I can’t then filter down to the data I need

Am I missing something obvious here. Any help much appreciated

Thank you

The state can only hold 255 characters.
What you have there is 788 characters.

I assume if you take the first 255 characters and convert them you get what you have in your state.
You need to make sure it becomes an attribute.
not sure how that is done when it is in hex, but normally you would add:

    json_attributes:
      - statusLavatrice

to the sensor.

Hi.

The 788 characters are in hex, they are converted to ASCII and command line level so should be returned at ASCII and then only 355 characters.

Your point still stands however that this is too much for a value, I just don’t understand why it would return 25 characters. (Coincidentally 255 characters of hex would equate to a lot more text too).

I have already tried to use a value_template and also attributes, but then I can’t get any data at all.


  - platform: command_line  
    command: 'curl -s http://192.168.1.94/http-read.json?encrypted=1 | xxd -r -p'
    name: candy_washing_machine_101
    value_template: '{{ value_json.statusLavatrice.Pr }}'
    scan_interval: 20
    command_timeout: 30
    json_attributes:
      - statusLavatrice

I’m should suspect that the sensor value should give me 15, being the value of Pr.

However, the value of the sensor is now shown as “unknown”.

It’s in these kind of situations I use Node red to just get it done.
Perhaps there is a error in the logs?

There is no “value_json” for a command-line sensor, only for rest/mqtt.
Use jq in you commad line to extract the value.

1 Like

I didn’t notice that. Good catch!

But then again… Why did he get a partial state?

Well, I tried the code and HA gave me an explicit error.

ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{ value_json.statusLavatrice.Pr }}'

There must be some confusion at hand…

EDIT: | xxd -r -p | jq .statusLavatrice.Pr works nicely in a plain shell, but not inside docker. There is something fishy with xxd

EDIT2: | xxd -r -p -c 1000 | jq .statusLavatrice.Pr works

Good morning.

Thank you very much for your help.

I had tried | jq yesterday too but to no avail.

Judging by your edits, you think -c fixed the issue I was having? Im not familiar with XXD but this is something to do with the number of “columns” it returns isnt it? Very odd, but thank you so much, i wouldnt have got here alone!