Command line sensor to query printer ink level

Hi,

Based on this instruction I tried to setup a similar command line sensor to query the ink level of my HP printer and also the printer status. I’ve tried to run the commands in terminal via SSH and it returns the value I expected (see screenshot).


The strings circled in blue are the expected values.

Now I tried to integrate the commands using command line sensor as follows :

### QUERY HP 3835 INK LEVELS
  - platform: command_line
    name: HP 3835 Color Ink Level
    unit_of_measurement: "%"
    scan_interval: 60
    command: 'hp-info 2>&1 | grep -oP "(?<=agent1-level\s{18})(.*\S)" | tr "\n" " "'
  - platform: command_line
    name: HP 3835 Black Ink Level
    unit_of_measurement: "%"
    scan_interval: 60
    command: 'hp-info 2>&1 | grep -oP "(?<=agent2-level\s{18})(.*\S)" | tr "\n" " "'
  - platform: command_line
    name: HP 3835 Status
    scan_interval: 60
    command: 'hp-info 2>&1 | grep -oP "(?<=status-desc\s{18})(.*\S)" | tr "\n" " "' 

However I’m not seeing any result in entity page.

HA log didn’t show any error… Could you please help me to get this command line sensor working?
Thank you very much!

Some fiddling around and I find something about ‘end of line’ character in Unix, and so I tried running ‘sed’ in terminal to clean the code. The resulting code is like this :

  - platform: command_line
    name: HP 3835 Color Ink Level
    unit_of_measurement: "%"
    scan_interval: 60
    command: hp-info 2>&1 | grep -oP "(?<=agent1-level\\s{18})(.*\\S)"
  - platform: command_line
    name: HP 3835 Black Ink Level
    unit_of_measurement: "%"
    scan_interval: 60
    command: hp-info 2>&1 | grep -oP "(?<=agent2-level\\s{18})(.*\\S)"
  - platform: command_line
    name: HP 3835 Status
    scan_interval: 60
    command: hp-info 2>&1 | grep -oP "(?<=status-desc\\s{18})(.*\\S)"

However it still gives ‘unknown’ result in entity state page. A look in the HA log shows like this (debug mode) :

2019-05-28 13:49:16 DEBUG (SyncWorker_17) [homeassistant.components.command_line.sensor] Running command: hp-info 2>&1 | grep -oP "(?<=status-desc\\s{18})(.*\\S)"
2019-05-28 13:49:16 ERROR (SyncWorker_17) [homeassistant.components.command_line.sensor] Command failed: hp-info 2>&1 | grep -oP "(?<=status-desc\\s{18})(.*\\S)"

Is there anything I can do? I can do MQTT way, but I really prefer command line method…