For the xml_grep: Are there two tags with ‘<dd:TotalImpressions PEID=“5082”>’? If so, that is why there are two answers. Do you have a unique tag & attribute in the document that can be used, one which increments every time a page is printed? If so use that.
If not you could try something like adding | head -qn1, or | awk 'NR==1{print $1}’ to take only the first line of the output:
xml_grep 'dd:TotalImpressions[@PEID="5082"]' http://[Your_Printer_IP_Address]/DevMgmt/ProductUsageDyn.xml --text_only | head -qn1
or
xml_grep 'dd:TotalImpressions[@PEID="5082"]' http://[Your_Printer_IP_Address]/DevMgmt/ProductUsageDyn.xml --text_only | awk 'NR==1{print $1}'
For the date: Your OS is not doing the date calculation. Please tell me what OS and shell are you using? For the shell, drop to the OS and give the output from this:
echo "$SHELL"
Also, the result of this could be helpful:
ls /bin/bash
To get the date part working, it would be easier for you to use the Template Sensor instead of the command line sensor. So change this:
# hp_envy_5540_next_renewal_date:
# friendly_name: HP Envy 5540 Next Renewal Date
# value_template: >-
# {% if (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1 > 12) %}
# {{ (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[0]|int + 1)|string + "-01-" + states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[2] }}
# {% else %}
# {{ states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[0]|int|string + "-" + ('%02d' % (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1)) + "-" + states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[2] }}
# {% endif %}
to this, uncommenting the code by removing the ‘#’ at the start of the line:
hp_envy_5540_next_renewal_date:
friendly_name: HP Envy 5540 Next Renewal Date
value_template: >-
{% if (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1 > 12) %}
{{ (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[0]|int + 1)|string + "-01-" + states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[2] }}
{% else %}
{{ states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[0]|int|string + "-" + ('%02d' % (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1)) + "-" + states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[2] }}
{% endif %}
and then change this:
- platform: command_line
name: "HP Envy 5540 Next Renewal Date"
command: "date -d '{{ states('input_datetime.hp_envy_5540_this_period_start_date') }} 1 month' +%Y-%m-%d"
value_template: "{{ value }}"
to this, commenting-out the code by adding a ‘#’ at the start of the line
#- platform: command_line
# name: "HP Envy 5540 Next Renewal Date"
# command: "date -d '{{ states('input_datetime.hp_envy_5540_this_period_start_date') }} 1 month' +%Y-%m-%d"
# value_template: "{{ value }}"