Command Line sensor with a Value Template - Struggling


I have a command_line sensor to run an xml_grep on a file served from a printer that returns the % values of the Black ink tank and the Colour ink tank.

sensor printer:

The result of this returns the two values, separated by a space, e.g.

86 30

where 86% is the value of the Black Ink Tank and 30% is the value of the colour tank.

Struggling with a value_template to split these into two separate sensors. Would appreciate any pointers. Have been trying things like the following but with no success;

value_template: “{{ states.sensor.printer.state.split(’ ')[0] }}”

Many thanks.

Have you tested in the dev-template?

Hi, I did have a brief look at that but if I’m honest, didn’t properly understand how to use it, but admittedly only spent 10 mins or so looking at it. Will have to have a longer look tomorrow I think.

Try this:

- platform: command_line
  name: 'Black ink'
  command: "xml_grep 'dd:ConsumableRawPercentageLevelRemaining' --text_only"
  value_template: "{{ value.split(' ')[0] }}"

Ta for the suggestion - sadly, I still end up with “86 30” as the sensor value instead of just “86”.

Ah - all sorted - there was a “\n” delimiter between the values and not a space - all working. Many thanks for the pointer Taras.