Command Line Sensor Command Failed

Trying to setup an ink sensor for an Epson printer.

I get an xml from a webpage

This command:

/usr/bin/curl "http://192.168.1.134/DevMgmt/ProductUsageDyn.xml" 2>&1 | grep -oPm1 "(?<=<dd:ConsumableRawPercentageLevelRemaining>)[^<]+"

works fine in terminal and returns the value but not in command line sensor, in log is says Command Failed

Any ideas ?

Hass.io is Home Assistant in a docker container running on a minimal version of Linux. Given these constraints, is curl located in /usr/bin ?


EDIT
Clarified my question.

Not really sure what that means, how would I check. Are you following me !

Change your command_line sensor to use curl instead of /usr/bin/curl.

It may still fail because I found a few threads (here’s one) stating that the version of grep used by hass.io does not support all options (you are using -oPm1). Some of these threads are over a year old so this limitation may have been eliminated. Maybe.

I just checked on my system and it appears that curl is located at usr/bin/curl, but grep that comes with hassio doesn’t support the -P option (the other options are):

Just to clarify, what ‘terminal’ were you using to test the curl and grep commands?

I admit I really have no idea what I’m doing here with this, I just copied it from somewhere on this site.

Am new to linux too (this week, came from RPi with HASSIO).

Am running ubuntu and the terminal I mean is the terminal ‘app’ in ubuntu.

I tried removing the -P option, still get the same error

Removing usr/bin is no different either

OK, you’re testing it in ubuntu (a feature-complete installation of Linux) and then mistakenly assuming it will work equally well on Hass.io (a slimmed down installation of Linux). No worries, it’s a common assumption but, as you’ve seen, a bit too optimistic.

Please post the configuration of your command_line sensor.

Oh I see.

This is the sensor, basically copied from elsewhere:

- platform: command_line
  name: Magenta Ink
  command: 'curl "http://192.168.1.134/DevMgmt/ProductUsageDyn.xml" 2>&1 | grep -oPm1 "(?<=<dd:ConsumableRawPercentageLevelRemaining>)[^<]+"'
  scan_interval: 60

In ubuntu it returns a number (percentage)
The IP address is my printer and that returns a large XML file with lots of status in it.
I was going to try scrape sensor as I have used on other sites but the webpage has bargraphs of the levels and not text to scrape.

If you included the -P option, it will fail because that’s not supported in Hass.io’s version of grep. If you leave it out, odds are the grep command won’t process the xml file correctly (and produce a result containing more than 255 characters which Home Assistant will reject because that the limit of what it will store in an entity’s state).

This problem was encountered by someone else and they resolved it by employing grep and sed (stream editor).

However, it’s not a simple solution because you have to specify appropriate commands for sed to process the xml correctly.

2 Likes

Maybe " should be ’

Does your printer support SNMP? If so, that is probably a better way forward.

Something new for me !

Yep it does and managed to get it working, thanks very much for all your help

for future reference, grep won’t work in hassio as stated above but you can do something similar using regex_findall_index.

As an example here is the result of a curl command from a security camera:

<result>0</result>
<IOAlarm>0</IOAlarm>
<motionDetectAlarm>0</motionDetectAlarm>
<soundAlarm>0</soundAlarm>
<record>0</record>
<sdState>0</sdState>
<sdFreeSpace>0k</sdFreeSpace>
<sdTotalSpace>0k</sdTotalSpace>
<ntpState>2</ntpState>
<ddnsState>2</ddnsState>
<url></url>
<upnpState>0</upnpState>
<isWifiConnected>0</isWifiConnected>
<wifiConnectedAP></wifiConnectedAP>
<infraLedState>1</infraLedState>
</CGI_Result>

Here is the value template for the sensor to extract a specified portion of that result:

   {% set status = value | regex_findall_index('<motionDetectAlarm>(.*)</motionDetectAlarm>') %}
   {% if status == '0' %}
     Disabled
   {%- elif status   == "1" -%}
     None
   {%- elif status == "2" -%}
     Detected
   {%- endif -%}

and be aware also that depending on the install method hassio might not be able to natively run curl commands on the host without additional configuration.