Instant Ink Countdown for Hp Printer Integration

Hmmm. Reading back, I can see that I was not very clear. That piece of Jinja script above was a start at producing a Next Renewal Date (as a timestamp), and not a Number Of Days Remaining.

I have no knowledge of HassOS, so sorry, I can’t help you there.
In HassOS, what does this command give you:

date -d '2020-01-01 1 month' +%Y-%m-%d

In Raspberry Pi OS (Raspbian) it gives 2020-02-01.

If you really must use Jinja for date processing, try replacing the sensor with this:

- platform: template
  sensors:

    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 %}

Here the date is split into year, month, and day parts, and

  • If the month is 12, the date is forced to year+1, month 01, and the day number.
  • If the month is not 12, the date is forced to year, month + 1, and the day number.

Again, it is not 100% tested, but I tried it in my configuration and it definitely works for this month. There’s also a correction in there for when the month number expressed as an integer is a single digit (so 1 becomes 01).

All things being equal, the existing ‘Number Of Days Remaining’ sensor should ‘just work.’

Caveats
For both sensor types, there are problems when the day number exceeds 28.

For example, in the OS Command version of the sensor:

date -d '2020-01-31 1 month' +%Y-%m-%d

In Raspberry Pi OS (Raspbian) it gives 2020-03-03, which obviously is incorrect.

For the Jinja sensor, the date returned for, e.g. 2020-01-30 plus 1 month, would be 2020-02-30, which is obviously not valid.

Thanks for your help! now it works perfectly!
now i’m thinking of creating an automation to update date, reset page counters.
do you think it can be done?
so as to automate everything

Glad to hear it!

The automation I gave above will reset the page counts at the monthly anniversary day. It works for me, anyway.

Edit: Confirmed the change above, to avoid setting the anniversary day in more than one place, does work, because my monthly stats have reset and rolled over as designed.

hi @EventuallyFixed , i’m doing some tests but i don’t understand why it works badly.
I find myself with 1 standard page remaining and 6 accumulated remaining.
I manually start the reset automation but on “rollower allowance pages” it doesn’t put 7 but 21. why?

testing the string it returns me the correct value

  - service: input_number.set_value
    data_template:
      entity_id: input_number.hp_envy_6230_pages_rollover_allowance
      value: >-
        {% if states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int %}
          {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int }}
        {% else %}
          {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}
        {% endif %}

stamp string

On the face of it, 21 = 15 + 6, so that’s likely to be the renewed Allowance Pages Remaining plus the Rollover Pages Remaining.

Please will you post here your adapted code for this automation.

certainly, here it is.
seems correct to me

- id: '1582325531280'
  alias: HP Envy 6230 Page Reset
  description: 'Sets: - Total number of pages printed at the start of the period -
    Start Date of this period (to store a date to calculate the next one) - Maximum
    Rollover Allowance value for this month - Rollover Remaining to new Maximum Rollover
    Allowance value for this month'
  trigger:
  - at: 00:00:00
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().day == 3 }}'
  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_6230_total_pages_printed'') }}'
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_6230_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'
  - service: input_number.set_value
    data_template:
      entity_id: input_number.hp_envy_6230_pages_rollover_allowance
      value: >-
        {% if states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int %}
          {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int }}
        {% else %}
          {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}
        {% endif %}

I’m doing tests:

so it returns me 6 as it should be

  - service: input_number.set_value
    data_template:
      entity_id: input_number.hp_envy_6230_pages_rollover_allowance
      value: '{{ states("sensor.hp_envy_6230_pages_rollover_remaining")}}'

so it returns me 15 instead of 1

  - service: input_number.set_value
    data_template:
      entity_id: input_number.hp_envy_6230_pages_rollover_allowance
      value: '{{ states("sensor.hp_envy_6230_pages_allowance_remaining")}}'

Hi,

There is a slight difference in the automations, which may be significant, in the ordering and indentation of data_template, entity_id, and value:

  • You have entity_id and value nested within data_template.
  • I have entity_id at the same level as data_template, and value nested within data_template

Did you use the Automations editor GUI in Home Assistant to edit it? For what it’s worth, I could not edit this automation using the front-end Automation Editor, and have it work, so I did advise that it could not be edited in the front-end.

Instead, please cut and paste the following code directly into the automations.yaml file, to replace the existing code, then let me know how you get on:

- id: '1588925532080'
  alias: HP Envy 6230 Page Reset
  description: 'Sets: - Total number of pages printed at the start of the period -
    Start Date of this period (to store a date to calculate the next one) - Maximum
    Rollover Allowance value for this month - Rollover Remaining to new Maximum Rollover
    Allowance value for this month'
  trigger:
  - at: 00:00:00
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().day|int == states(''input_datetime.hp_envy_6230_this_period_start_date'')[-2:]|int
      }}'
  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_6230_total_pages_printed'') }}'
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_6230_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_rollover_allowance
    data_template:
      value: "{% if states('sensor.hp_envy_6230_pages_allowance_remaining')|int +\
        \ states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int\
        \ + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"

@EventuallyFixed
Resolved!!!
I reversed the order of the actions.
rightly if you run them one at a time, you already find the value 15, instead so does the calculation on the current ones and then resets the counters.

  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_rollover_allowance
    data_template:
      value: "{% if states('sensor.hp_envy_6230_pages_allowance_remaining')|int +\
        \ states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int\
        \ + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_6230_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_6230_total_pages_printed'') }}'

now I ask you something (I hope to explain why I use google translate):
if I exceed the threshold of 15, I will pay 1 € and I will have 10 more pages.
of these I only consume 6.
according to the HP counts, the remaining 4 will accumulate the following month?
your code does not provide it

You beat me to it, the order these are set is the cause. Well spotted. :smile:
The date change causes the template sensor to set the new monthly allowance, and I guess on a machine faster than my Raspberry Pi, that makes a difference… I can only guess.

As for the over print: You are correct, my code does not allow for that, purely because I have never exceeded the monthly allowance plus rollover. So yes, not yet a complete solution.

I would amend the Template Sensor, like so, adding a clause at the start for the remaining overprint pages. This syntax-checks OK, but I have not tested it in any way, but logically I don’t see why it would not work.

It also assumes that the over print blocks are only ever in blocks of ten.


    hp_envy_6230_pages_rollover_remaining:
      friendly_name: "Rollover Pages Remaining"
      value_template: >-
        {% if states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if states('sensor.hp_envy_6230_pages_overprint')|int > 0 and (states('sensor.hp_envy_6230_pages_overprint')|int % 10) > 0 %}
            {{ 10 - (states('sensor.hp_envy_6230_pages_overprint')|int % 10)  }}
          {% elif states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int >= 0 %}
            {{ states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int }}
          {% elif states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int -   states('sensor.hp_envy_6230_total_pages_printed')|int < 0 %}
            0
          {% elif 1==1 %}
            {{ states('input_number.hp_envy_6230_pages_rollover_allowance')|int }}
          {% endif %}
        {% endif %}

great @EventuallyFixed , your solution works perfectly.
I found another bug on the “cost sensor”.
when you get to print n.10 it already indicates 2 € because (1+ (10/10) makes 2 but in reality you still don’t pay the second block … same thing with 20 etc …
I with my little knowledge of programming I have solved this (hoping never to overprint :smile: )

{% if states('input_datetime.hp_envy_6230_total_pages_printed') or states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if (0 < states('sensor.hp_envy_6230_pages_overprint')|int <= 10) %}
            1  
          {% elif (10 < states('sensor.hp_envy_6230_pages_overprint')|int <= 20) %}
            2
          {% elif (20 < states('sensor.hp_envy_6230_pages_overprint')|int <= 30) %}
            3
          {% elif (30 < states('sensor.hp_envy_6230_pages_overprint')|int <= 40) %}
            4
          {% endif %}    
        {% endif %}  

for the moment I have not found any other bugs :stuck_out_tongue_winking_eye:

Thanks @acuplush it has been a great team effort here! :+1: Thanks for helping with the testing and debugging.

I’ve made a few tests here too, and I can see that in an overprint scenario, the value for ‘Rollover Pages Remaining This Month’ increases and decreases with the total number of pages printed.

This is not how I designed it: What should happen is that once consumed, the ‘Rollover Pages Remaining This Month’ should remain at zero for the rest of the month.

Then then when the anniversary day is reached, the Automation should set the Maximum Rollover Allowance for the following month, to be what was left of that block of ten.

The updated automation:

  • This was changed to add a condition for the overprint remaining amount of the block, ahead of the other conditions. Again it uses the configurable overprint block size.
- id: '1588925532080'
  alias: HP Envy 6230 Page Reset
  description: 'Sets: - Total number of pages printed at the start of the period -
    Start Date of this period (to store a date to calculate the next one) - Maximum
    Rollover Allowance value for this month - Rollover Remaining to new Maximum Rollover
    Allowance value for this month'
  trigger:
  - at: 00:00:00
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().day|int == states(''input_datetime.hp_envy_6230_this_period_start_date'')[-2:]|int
      }}'
  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_rollover_allowance
    data_template:
      value: "
      {% if states('sensor.hp_envy_6230_pages_overprint')|int > 0 and (states('sensor.hp_envy_6230_pages_overprint')|int % states('input_number.hp_envy_6230_pages_overprint_block_size')|int) > 0 %}
        {{ states('input_number.hp_envy_6230_pages_overprint_block_size')|int - (states('sensor.hp_envy_6230_pages_overprint')|int % states('input_number.hp_envy_6230_pages_overprint_block_size')|int)  }}
      {% elif states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int %}
        {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int }}
      {% else %}
        {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}
      {% endif %}"
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_6230_total_pages_printed'') }}'
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_6230_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'

The updated sensors

  • The Rollover Pages Remaining, to remove the overprint rollover pages
  • The Over Allowance Cost, to make it generic, so that it uses the configurable block size, and can handle any number of over-print pages.

The Over Allowance Prints is there only because it was in between these two, and got caught in the copy & paste! :slight_smile:

# The number of rollover pages remaining for this period.
# Calculated when each page is printed, when the Rollover Allowance is reset, and at the start of each new Instant Ink period
    hp_envy_6230_pages_rollover_remaining:
      friendly_name: "Rollover Pages Remaining"
      value_template: >-
        {% if states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int >= 0 %}
            {{ states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int }}
          {% elif states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int - states('sensor.hp_envy_6230_total_pages_printed')|int < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int -   states('sensor.hp_envy_6230_total_pages_printed')|int < 0 %}
            0
          {% elif 1==1 %}
            {{ states('input_number.hp_envy_6230_pages_rollover_allowance')|int }}
          {% endif %}
        {% endif %}
#
# The number pages printed over and above the Monthly Allowance plus the Rollover Allowance
# Calculated when each page is printed, and at the start of each new Instant Ink period
    hp_envy_6230_pages_overprint:
      friendly_name: "Over Allowance Prints"
      value_template: >-
        {% if states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if states('input_number.hp_envy_6230_pages_at_month_start')|int + states('input_number.hp_envy_6230_pages_monthly_allowance')|int + states('input_number.hp_envy_6230_pages_rollover_allowance')|int < states('sensor.hp_envy_6230_total_pages_printed')|int %}
            {{ states('sensor.hp_envy_6230_total_pages_printed')|int - states('input_number.hp_envy_6230_pages_rollover_allowance')|int - states('input_number.hp_envy_6230_pages_monthly_allowance')|int - states('input_number.hp_envy_6230_pages_at_month_start')|int }}
          {% else %}
            0
          {% endif %}    
        {% endif %}
# The cost so far of the pages that have been printed over the Allowance plus Rollover
# Calculated when each page is printed, and at the start of each new Instant Ink period
    hp_envy_6230_pages_overprint_cost:
      friendly_name: "Over Allowance Cost"
      value_template: >-
        {% if states('input_datetime.hp_envy_6230_total_pages_printed') or states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if states('sensor.hp_envy_6230_pages_overprint')|int == 0 %}
            0
          {% elif states('sensor.hp_envy_6230_pages_overprint')|int > 0 and ( (states('sensor.hp_envy_6230_pages_overprint')|int - 1) % states('input_number.hp_envy_6230_pages_overprint_block_size')|int == 0 ) %}
            {{ (1 + ((states('sensor.hp_envy_6230_pages_overprint')|int - 1) / states('input_number.hp_envy_6230_pages_overprint_block_size')|int)) | multiply( states('input_number.hp_envy_6230_pages_overprint_block_cost')|int)|int  }}
          {% else %}
            {{ states('sensor.hp_envy_6230_pages_overprint_cost') }}
          {% endif %}    
        {% endif %}    
      unit_of_measurement: '£'

It depends on your point of view.
in my opinion it is right as my code works, because when I pay, the 9 pages become available, so they must be visible in the “remaining pages” counter.

I had designed the Monthly Allowance and Rollover Amount to be what was available for this month, without having to pay. The fact that you’re in over-print means that these are exhausted, so to me, they should show zero.

But this is Home Assistant, and you can adapt it the way you want, so feel free.

However if I were you, I’d have a new sensor for the over-print amount remaining, which would be like:

    hp_envy_6230_pages_overprint_remaining:
      friendly_name: "Over Allowance Prints Remaining"
      value_template: >-
        {% if states('sensor.hp_envy_6230_pages_overprint')|int == 0 or (states('sensor.hp_envy_6230_pages_overprint')|int % states('input_number.hp_envy_6230_pages_overprint_block_size')|int == 0) %}
          0
        {% else %}
          {{ states('input_number.hp_envy_6230_pages_overprint_block_size')|int - (states('sensor.hp_envy_6230_pages_overprint')|int % states('input_number.hp_envy_6230_pages_overprint_block_size')|int) }}
        {% endif %}  

Additionally I’ve made the Overprint Cost sensor more simple:

# The cost so far of the pages that have been printed over the Allowance plus Rollover
# Calculated when each page is printed, and at the start of each new Instant Ink period
    hp_envy_6230_pages_overprint_cost:
      friendly_name: "Over Allowance Cost"
      value_template: >-
        {% if states('input_datetime.hp_envy_6230_total_pages_printed') or states('input_datetime.hp_envy_6230_this_period_start_date') %}
          {% if states('sensor.hp_envy_6230_pages_overprint')|int == 0 %}
            0
          {% elif states('sensor.hp_envy_6230_pages_overprint')|int > 0 and ( (states('sensor.hp_envy_6230_pages_overprint')|int - 1) % states('input_number.hp_envy_6230_pages_overprint_block_size')|int == 0 ) %}
            {{ (1 + ((states('sensor.hp_envy_6230_pages_overprint')|int - 1) / states('input_number.hp_envy_6230_pages_overprint_block_size')|int)) | multiply( states('input_number.hp_envy_6230_pages_overprint_block_cost')|int)|int  }}
          {% else %}
            {{ (1 + ((states('sensor.hp_envy_6230_pages_overprint')|int - 1) / states('input_number.hp_envy_6230_pages_overprint_block_size')|int)) | multiply( states('input_number.hp_envy_6230_pages_overprint_block_cost')|int)|int  }}
          {% endif %}    
        {% endif %}    
      unit_of_measurement: '£'

And finally, the Lovelace Card is modified to add the over print pages remaining:

cards:
  - entities:
      - entity: sensor.hp_envy_6230_allowance_days_remaining
      - entity: sensor.hp_envy_6230_pages_allowance_remaining
      - entity: sensor.hp_envy_6230_pages_rollover_remaining
      - entity: sensor.hp_envy_6230_pages_overprint
      - entity: sensor.hp_envy_6230_pages_overprint_cost
      - entity: sensor.hp_envy_6230_pages_overprint_remaining
    title: HP Envy Instant Ink
    type: entities
  - cards:
# The IPP Integration
      - entity: sensor.hp_envy_6230_series_black_ink
        max: 100
        min: 0
        name: Black Ink
        type: gauge
      - entity: sensor.hp_envy_6230_series_tri_color_ink
        max: 100
        min: 0
        name: Tri-Colour Ink
        type: gauge
    type: horizontal-stack
type: vertical-stack

Hope that helps!

great job, thanks!

Hi
I would like to user your integration but I can’t install xml-twig-tools. Home assistant is installed via docker and the command pip install xml-twig-tools give me these errors :

ERROR: Could not find a version that satisfies the requirement xml-twig-tools (from versions: none)
ERROR: No matching distribution found for xml-twig-tools

Can you help me with the package installation ?
Thanks

Finally, I installed the perl-xml-twig package and it seems to work.
Now I have errors when I execute the automation.

HP Envy 6230 Page Reset: Error executing script. Invalid data for call_service at pos 2: expected float for dictionary value @ data['value']

In the logs : Command failed: date -d '2020-10-25 1 month' +%Y-%m-%d

@EventuallyFixed Can you make a last post with all the code please ?
I grab code from multiple post so maybe I forgot something

Thanks a lot

hi @elyisum , i didn’t use “perl-xml-twig”, but hp official integration of “home assistant”.

for the reset automation error I can’t help you, this is mine and it works:

- alias: HP Envy 6230 Reset Mese
  trigger:
    - platform: time
      at: '00:30:00'
  condition:
    - condition: template
      value_template: "{{ now().day == 25 }}"
  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_rollover_allowance
    data_template:
      value: "{% if states('sensor.hp_envy_6230_pages_allowance_remaining')|int +\
        \ states('sensor.hp_envy_6230_pages_rollover_remaining')|int > states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_envy_6230_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_envy_6230_pages_allowance_remaining')|int\
        \ + states('sensor.hp_envy_6230_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_6230_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'
  - service: input_number.set_value
    entity_id: input_number.hp_envy_6230_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_6230_total_pages_printed'') }}'

Thanks for your reply
I installed perl-xml-twig to get the Totalimpression from the XML
I tried your code : no erros but there are no improvements. Sensors values are always 0.
Can you post all the sensors please ?

Here is my configuration…

Inputs

# Inputs for the HP Ink Print Plan
input_number:
# The total number of pages that had been printed
# at the start of this period of the printing plan
# Set by an automation
  hp_envy_5540_pages_at_month_start:
    name: Printed Pages Total at month start
    min: 0
    max: 999999
    step: 1
    icon: mdi:printer
    mode: box
# The current monthly allowance number of pages
# Set manually
  hp_envy_5540_pages_monthly_allowance:
    name: Monthly Allowance Pages
    min: 0
    max: 999999
    step: 1
    icon: mdi:printer
    mode: box
# The maximum number of rollover pages allowed.
# Ten on the free tier, 2x pages on the paid tier.
# Set manually
  hp_envy_5540_pages_rollover_monthly_max_allowance:
    name: Rollover Maximum Pages
    min: 0
    max: 999999
    step: 1
    icon: mdi:printer
    mode: box
# The rollover allowance for this month
# Set by an automation
  hp_envy_5540_pages_rollover_allowance:
    name: Rollover Allowance Pages
    min: 0
    max: 999999
    step: 1
    icon: mdi:printer
    mode: box
# The number of pages in each overprint block, when
# the Monthly Allowance plus Rollover are exceeded
# Set manually
  hp_envy_5540_pages_overprint_block_size:
    name: Over Print Block Size
    min: 0
    max: 999999
    step: 1
    icon: mdi:printer
    mode: box
# The cost of each overprint block, in £
# Set manually
  hp_envy_5540_pages_overprint_block_cost:
    name: Over Print Block Cost
    min: 0
    max: 999999
    step: 0.01
    icon: mdi:currency-gbp
    mode: box
# The start date of this period of the print plan
# Set by automation    
input_datetime:
  hp_envy_5540_this_period_start_date:
    name: This Period Start Date
    has_date: true
    has_time: false
    icon: mdi:calendar

Date/Time Sensor

- platform: time_date
  display_options:
    - 'time'
    - 'date'
    - 'date_time'
    - 'date_time_utc'
    - 'date_time_iso'
    - 'time_date'
    - 'time_utc'
    - 'beat'

Template Sensors

- platform: template
  sensors:
  
# Used to track days remaining of HP Ink Printer Pages period
# Needs to have the date sensors installed as per: https://www.home-assistant.io/integrations
# Use of sensor.date ought to force a daily update, according to https://www.home-assistant.io/integrations/template/
    hp_envy_5540_allowance_days_remaining:
      friendly_name: "Allowance Days Remaining"
      value_template: >-
         {{ ((as_timestamp(strptime(states('sensor.hp_envy_5540_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}
#
# The number of allowance pages remaining for this period
# Calculated when each page is printed, and at the start of each new Instant Ink period
# "{% (if states('input_datetime.hp_envy_5540_this_period_start_date') %}" is a workaround to set a listener)
    hp_envy_5540_pages_allowance_remaining:
      friendly_name: "Allowance Pages Remaining"
      value_template: >-
        {% if states('input_datetime.hp_envy_5540_this_period_start_date') %}
          {% if states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int >= 0 %}
            {{ states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int }}
          {% else %}
            0
          {% endif %}
        {% endif %}
#
# The number of rollover pages remaining for this period.
# Calculated when each page is printed, when the Rollover Allowance is reset, and at the start of each new Instant Ink period
    hp_envy_5540_pages_rollover_remaining:
      friendly_name: "Rollover Pages Remaining"
      value_template: >-
        {% if states('input_datetime.hp_envy_5540_this_period_start_date') %}
          {% if states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int < 0 and states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int + states('input_number.hp_envy_5540_pages_rollover_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int >= 0 %}
            {{ states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int + states('input_number.hp_envy_5540_pages_rollover_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int }}
          {% elif states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int - states('sensor.hp_envy_5540_total_pages_printed')|int < 0 and states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int + states('input_number.hp_envy_5540_pages_rollover_allowance')|int -   states('sensor.hp_envy_5540_total_pages_printed')|int < 0 %}
            0
          {% elif 1==1 %}
            {{ states('input_number.hp_envy_5540_pages_rollover_allowance')|int }}
          {% endif %}
        {% endif %}
#
# The number pages printed over and above the Monthly Allowance plus the Rollover Allowance
# Calculated when each page is printed, and at the start of each new Instant Ink period
    hp_envy_5540_pages_overprint:
      friendly_name: "Over Allowance Prints"
      value_template: >-
        {% if states('input_datetime.hp_envy_5540_this_period_start_date') %}
          {% if states('input_number.hp_envy_5540_pages_at_month_start')|int + states('input_number.hp_envy_5540_pages_monthly_allowance')|int + states('input_number.hp_envy_5540_pages_rollover_allowance')|int < states('sensor.hp_envy_5540_total_pages_printed')|int %}
            {{ states('sensor.hp_envy_5540_total_pages_printed')|int - states('input_number.hp_envy_5540_pages_rollover_allowance')|int - states('input_number.hp_envy_5540_pages_monthly_allowance')|int - states('input_number.hp_envy_5540_pages_at_month_start')|int }}
          {% else %}
            0
          {% endif %}    
        {% endif %}
# The cost so far of the pages that have been printed over the Allowance plus Rollover
# Calculated when each page is printed, and at the start of each new Instant Ink period
    hp_envy_5540_pages_overprint_cost:
      friendly_name: "Over Allowance Cost"
      value_template: >-
        {% if states('input_datetime.hp_envy_5540_total_pages_printed') or states('input_datetime.hp_envy_5540_this_period_start_date') %}
          {% if states('sensor.hp_envy_5540_pages_overprint')|int == 0 %}
            0
          {% elif states('sensor.hp_envy_5540_pages_overprint')|int > 0 and ( (states('sensor.hp_envy_5540_pages_overprint')|int - 1) % states('input_number.hp_envy_5540_pages_overprint_block_size')|int == 0 ) %}
            {{ (1 + ((states('sensor.hp_envy_5540_pages_overprint')|int - 1) / states('input_number.hp_envy_5540_pages_overprint_block_size')|int)) | multiply( states('input_number.hp_envy_5540_pages_overprint_block_cost')|int)|int  }}
          {% else %}
            {{ (1 + ((states('sensor.hp_envy_5540_pages_overprint')|int - 1) / states('input_number.hp_envy_5540_pages_overprint_block_size')|int)) | multiply( states('input_number.hp_envy_5540_pages_overprint_block_cost')|int)|int  }}
          {% endif %}    
        {% endif %}    
      unit_of_measurement: '£'

    hp_envy_5540_pages_overprint_remaining:
      friendly_name: "Over Allowance Prints Remaining"
      value_template: >-
        {% if states('sensor.hp_envy_5540_pages_overprint')|int == 0 or (states('sensor.hp_envy_5540_pages_overprint')|int % states('input_number.hp_envy_5540_pages_overprint_block_size')|int == 0) %}
          0
        {% else %}
          {{ states('input_number.hp_envy_5540_pages_overprint_block_size')|int - (states('sensor.hp_envy_5540_pages_overprint')|int % states('input_number.hp_envy_5540_pages_overprint_block_size')|int) }}
        {% endif %}     
        
# EITHER use this, or the Command Line sensor below named the same:
    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 %}        

Scrape Sensors

# Sensor to get 'HP Envy 5540 Total Pages Printed' from the printer's web service.
# The whole project depends on getting this value.
# The tag, '<dd:TotalImpressions PEID="5082">' is used here, BUT THE TAG YOU NEED MAY BE DIFFERENT.
- platform: scrape
  resource: http://[Your_Printer_IP_Address]/DevMgmt/ProductUsageDyn.xml
  name: hp_envy_5540_total_pages_printed
  select: 'dd\:TotalImpressions[PEID="5082"]'
  value_template: >-
    {% if value == "" %}
      0
    {% else %}
      {{ value }}
    {% endif %}

Command Line Sensors
These are alternative sensors that may be tried and used, if the sensors above don’t work. You will need to comment/remove the sensors above, and uncomment these.

# Used with input_datetime, & input_number, to track the state of HP Printer Instant Ink

# This is a command line sensor alternative to the scrape sensor, hp_envy_5540_total_pages_printed, above
# It needs package, 'xml-twig-tools', to be installed in Raspbian
# Inspired by: https://community.home-assistant.io/t/command-line-sensor-with-a-value-template-struggling/125957/3
# The whole project depends on getting this value.
# The tag, '<dd:TotalImpressions PEID="5082">' is used here, BUT THE TAG YOU NEED MAY BE DIFFERENT.
# ------
# Gets the HP Printer Total Pages Printed from the printer's web service
#- platform: command_line
#  name: "HP Envy 5540 Total Pages Printed"
#  command: "xml_grep 'dd:TotalImpressions[@PEID=\"5082\"]' http://[Your_Printer_IP_Address]/DevMgmt/ProductUsageDyn.xml --text_only"
#  value_template: "{{ value }}"


# This is a command line sensor alternative to the template sensor, hp_envy_5540_next_renewal_date, above
# ------
# Gets the HP Ink Next Allowance Renewal Date, using the command line for the calculation
#- 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 }}"

Automation

- id: '1588925532080'
  alias: HP Envy 5540 Page Reset
  description: 'Sets: - Total number of pages printed at the start of the period -
    Start Date of this period (to store a date to calculate the next one) - Maximum
    Rollover Allowance value for this month - Rollover Remaining to new Maximum Rollover
    Allowance value for this month - Rollover from overprint pages'
  trigger:
  - at: 00:00:00
    platform: time
  condition:
  - condition: template
    value_template: '{{ now().day|int == states(''input_datetime.hp_envy_5540_this_period_start_date'')[-2:]|int
      }}'
  action:
  - service: input_number.set_value
    entity_id: input_number.hp_envy_5540_pages_rollover_allowance
    data_template:
      value: ' {% if states(''sensor.hp_envy_5540_pages_overprint'')|int > 0 and (states(''sensor.hp_envy_5540_pages_overprint'')|int
        % states(''input_number.hp_envy_5540_pages_overprint_block_size'')|int) >
        0 %} {{ states(''input_number.hp_envy_5540_pages_overprint_block_size'')|int
        - (states(''sensor.hp_envy_5540_pages_overprint'')|int % states(''input_number.hp_envy_5540_pages_overprint_block_size'')|int)  }}
        {% elif states(''sensor.hp_envy_5540_pages_allowance_remaining'')|int + states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int
        > states(''input_number.hp_envy_5540_pages_rollover_monthly_max_allowance'')|int
        %} {{ states(''input_number.hp_envy_5540_pages_rollover_monthly_max_allowance'')|int
        }} {% else %} {{ states(''sensor.hp_envy_5540_pages_allowance_remaining'')|int
        + states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int }} {% endif
        %}'
  - service: input_number.set_value
    entity_id: input_number.hp_envy_5540_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_envy_5540_total_pages_printed'') }}'
  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_envy_5540_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'

Lovelace Cards

Configuration

cards:
  - entities:
      - entity: input_datetime.hp_envy_5540_this_period_start_date
      - entity: input_number.hp_envy_5540_pages_at_month_start
      - entity: sensor.hp_envy_5540_total_pages_printed
      - entity: input_number.hp_envy_5540_pages_monthly_allowance
      - entity: input_number.hp_envy_5540_pages_rollover_allowance
      - entity: input_number.hp_envy_5540_pages_rollover_monthly_max_allowance
      - entity: input_number.hp_envy_5540_pages_overprint_block_size
      - entity: input_number.hp_envy_5540_pages_overprint_block_cost
    title: HP Envy Instant Ink Configuration
    type: entities
type: vertical-stack

Sensors

cards:
  - entities:
      - entity: sensor.hp_envy_5540_allowance_days_remaining
      - entity: sensor.hp_envy_5540_pages_allowance_remaining
      - entity: sensor.hp_envy_5540_pages_rollover_remaining
      - entity: sensor.hp_envy_5540_pages_overprint
      - entity: sensor.hp_envy_5540_pages_overprint_cost
      - entity: sensor.hp_envy_5540_pages_overprint_remaining
    title: HP Envy Instant Ink
    type: entities
  - cards:
# These twp are using the IPP Integration
      - entity: sensor.hp_envy_5540_series_black_ink
        max: 100
        min: 0
        name: Black Ink
        type: gauge
      - entity: sensor.hp_envy_5540_series_tri_color_ink
        max: 100
        min: 0
        name: Tri-Colour Ink
        type: gauge
    type: horizontal-stack
type: vertical-stack
1 Like