Instant Ink Countdown for Hp Printer Integration

Can someone please help me set this up? to many information in here, don’t know where to start X :see_no_evil:

Hi, as I’m the original creator of this thread, I don’t think the other contributors will get notifications. My basic sensor works fine but I don’t have the roll over pages.

Try reading through EventuallyFixed posts and if you have specific questions you’ll have to tag them.

It would be great if we could get this as something you could add through HACS but we’d need a developer to take it on.

Hope you manage to get it sorted :+1:

1 Like

Based on those automation, can we set another automation for example whenever I print something I’ll get a notification on my phone saying “you have printed total 10 pages”?

It would be much easier to use the pages remaining sensor. Otherwise you’d need a new sensor that is looking at how many pages you’ve printed during your current period.

Just add another action to the Automation using the notify integration

service: notify.mobile_app_what_ever_yours_is_called
data:
  message: You have {{states('sensor.pages_left')}} pages remaining.
1 Like

@EventuallyFixed

Hello,

In your input sometimes says “add manualy” but where do i add this? and do i need to add the rollover pages every month manually?

edit:
this is how i have it now

@AJStubbsy ,

I also can not seem to get the basic counter sensor to work, my ‘days remain’ says -18000 :S

Hi, I’m using the very basic setup that just shows pages remaining. I didn’t implement EventuallyFixed full solution, as I don’t have any roll-over pages. Hopefully he will get back to you and help you along.

For pages remaining, you have to include an offset, to get the correct starting value. Maybe it’s the same for days remaining?

1 Like

@acuplush
Hello,

I am trying to make this for my HA to, but i have the same problem as here, can you maybe help me with how you solved it?

Bye! I apologize if you find it hard to understand me but I use google translate.
yes, everything works fine for me.
I am attaching all my working code to you.
try it and see if it works for you

the 25th is the day that the offer is renewed

I use the official hp integration to receive the number of printed pages and ink levels
and the indispensable data_time sensor

input_number:
  hp_envy_6230_pages_at_month_start:
    name: Stampe inizio mese
    min: 50
    max: 10000
    step: 1
    icon: mdi:printer
    mode: box
# The current monthly allowance number of pages
# Set manually in a front-end card
  hp_envy_6230_pages_monthly_allowance:
    name: Stampe mensili standard
    min: 15
    max: 300
    step: 1
    icon: mdi:printer
    mode: box
# The maximum number of rollover pages allowed
# Set manually in a front-end card
  hp_envy_6230_pages_rollover_monthly_max_allowance:
    name: Accumulo massimo
    min: 0
    max: 600
    step: 1
    icon: mdi:printer
    mode: box
# The rollover allowance for this month
# Set by the automation - id: '1588925532080'; alias: HP Envy 5540 Page Reset.
  hp_envy_6230_pages_rollover_allowance:
    name: Stampe accumulate
    min: 0
    max: 600
    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 in a front-end card
  hp_envy_6230_pages_overprint_block_size:
    name: Stampe blocco extra
    min: 0
    max: 100
    step: 5
    icon: mdi:printer
    mode: box
# The cost of each overprint block, as a decimal
# Set manually in a front-end card
  hp_envy_6230_pages_overprint_block_cost:
    name: Costo blocco extra
    min: 0
    max: 50
    step: 0.05
    icon: mdi:currency-gbp
    mode: box



input_datetime:
  hp_envy_6230_this_period_start_date:
    name: This Period Start Date
    has_date: true
    has_time: false
    icon: mdi:calendar

#AUTOMAZIONI
automation:
- alias: HP Envy 6230 Reset Mese
  trigger:
  - platform: time
    at: '00:30'
  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'') }}'


#sensori
sensor:

- platform: time_date
  display_options:
    - 'time'
    - 'date'

- platform: template
  sensors:
    hp_envy_6230_pagine_disponibili:
      friendly_name: "Pagine Disponibili"
      value_template: "{{ states ('sensor.hp_envy_6230_pages_allowance_remaining')|int (0) + states ('sensor.hp_envy_6230_pages_rollover_remaining')|int (0) + states ('sensor.hp_envy_6230_pages_overprint_remaining')|int (0) }}"
 
  
    hp_envy_6230_next_renewal_date:
      friendly_name: "HP Envy 6230 Prossima Data Rinnovo"
      value_template: >-
        {% if (states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[1]|int (0) + 1 > 12) %}
          {{  (states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[0]|int (0) + 1)|string + "-01-" + states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[2]  }}
        {% else %}
          {{  states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[0]|int|string + "-" + (states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[1]|int + 1)|string + "-" + states('input_datetime.hp_envy_6230_this_period_start_date').split("-")[2]  }}
        {% endif %} 
  
  
    hp_envy_6230_total_pages_printed:
      friendly_name: "HP Envy 6230 Pagine Stampate"
      value_template: "{{ states('sensor.stampante_printer') }}"
  

# 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_6230_allowance_days_remaining:
      friendly_name: "Giorni Rimanenti"
      value_template: >-
         {{ ((as_timestamp(strptime(states('sensor.hp_envy_6230_next_renewal_date'), '%Y-%m-%d'))|int (0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int (0) ) / 86400)|int (0) }}
#
        
# 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_6230_pages_allowance_remaining:
      friendly_name: "Pagine Standard Rimanenti"
      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 (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) >= 0 %}
            {{ states('input_number.hp_envy_6230_pages_at_month_start')|int (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) }}
          {% 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_6230_pages_rollover_remaining:
      friendly_name: "Pagine Accumulate Rimanenti"
      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 (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) + states('input_number.hp_envy_6230_pages_rollover_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) >= 0 %}
            {{ states('input_number.hp_envy_6230_pages_at_month_start')|int (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) + states('input_number.hp_envy_6230_pages_rollover_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) }}
          {% elif states('input_number.hp_envy_6230_pages_at_month_start')|int (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) - states('sensor.hp_envy_6230_total_pages_printed')|int (0) < 0 and states('input_number.hp_envy_6230_pages_at_month_start')|int (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) + states('input_number.hp_envy_6230_pages_rollover_allowance')|int (0) -   states('sensor.hp_envy_6230_total_pages_printed')|int (0) < 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: "Stampe Extra"
      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 (0) + states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) + states('input_number.hp_envy_6230_pages_rollover_allowance')|int (0) < states('sensor.hp_envy_6230_total_pages_printed')|int (0) %}
            {{ states('sensor.hp_envy_6230_total_pages_printed')|int (0) - states('input_number.hp_envy_6230_pages_rollover_allowance')|int (0) - states('input_number.hp_envy_6230_pages_monthly_allowance')|int (0) - states('input_number.hp_envy_6230_pages_at_month_start')|int (0) }}
          {% else %}
            0
          {% endif %}    
        {% endif %}
        
    hp_envy_6230_pages_overprint_remaining:
      friendly_name: "Stampe Extra Rimanenti"
      value_template: >-
        {% if states('sensor.hp_envy_6230_pages_overprint')|int (0) == 0 or (states('sensor.hp_envy_6230_pages_overprint')|int (0) % states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0) == 0) %}
          0
        {% else %}
          {{ states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0) - (states('sensor.hp_envy_6230_pages_overprint')|int (0) % states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0)) }}
        {% 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: "Sovrapprezzo"
      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 %}
            0
          {% elif states('sensor.hp_envy_6230_pages_overprint')|int (0) > 0 and ( (states('sensor.hp_envy_6230_pages_overprint')|int (0) - 1) % states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0) == 0 ) %}
            {{ (1 + ((states('sensor.hp_envy_6230_pages_overprint')|int (0) - 1) / states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0))) | multiply( states('input_number.hp_envy_6230_pages_overprint_block_cost')|int (0))|int (0)  }}
          {% else %}
            {{ (1 + ((states('sensor.hp_envy_6230_pages_overprint')|int (0) - 1) / states('input_number.hp_envy_6230_pages_overprint_block_size')|int (0))) | multiply( states('input_number.hp_envy_6230_pages_overprint_block_cost')|int (0))|int (0)  }}
          {% endif %}    
        {% endif %}  
      unit_of_measurement: '€'
1 Like

le schede su lovelace sono queste

@acuplush
Hi!

I think it is working! :smiley:

May need to wait for the next cycle to show correct values.

Thank you very much!

you must already set the correct printouts, otherwise next month you will accumulate more pages than the real ones.
go to the instantink site, check how many pages you have already printed this month and subtract them from the item “printed pages total at month start”
and set “rollower allowance pages” to 0

1 Like

I’m glad you got this sorted. I regret that I’ve been busy elsewhere so could not reply until now.

The initial setup will be as @acuplush just said: Go to the HP site and get the number of pages printed this month, then set the ‘Printed Pages Total at month start’ to the ‘Total Pages Printed’ minus that number. After that the page counts will look after themselves.

I am on the free tier and I did find a bug in that scenario, for transferring the ‘Rollover Allowance Pages’ into the following month. On the free tier there is no rollover from the standard allowance, but only from the paid part where over the fifteen pages per month are used.

As a quick fix for my situation I set the action of the automation for this part as follows:

  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_remaining'')|int > 0 %}
                {% if states(''sensor.hp_envy_5540_pages_overprint_remaining'')|int + states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int < 15 %}
                  {{ states(''sensor.hp_envy_5540_pages_overprint_remaining'')|int + states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int }}
                {% else %}
                  15
                {% endif %}
              {% else %}
                {{ states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int }}
              {% endif %}'

Obviously that is unsuitable for the paid plans, and the workaround I used previously was each month to check the History graph, then manually correct the rollover value.

I think what this really needs is something like a drop-down list to select the HP Instant Ink Tier, and then this part of the automation can refer to it to decide what to do within the rollover logic of the automation. I’ll take a look at how this can be done in the coming weeks.

2 Likes

I did, thanks :slight_smile:

Thank you it shows correct now!
One last question, the past month i have 2 rollovers left, do i need to set them manually now somewhere?
meeneem

Yes, set the two rollover pages in ‘Rollover Allowance Pages’, because that is the number of rollover pages you have (remaining) for that month.

1 Like

I have amended the automation and card to fix the rollover pages bug I mentioned above, so that the rollover pages for the free plan will correctly work. Although the logic looks OK (to me) I haven’t had a lot of time to test it. My rollover date is in a couple of weeks though… :slight_smile:

  • Added a new Select List to choose ‘Free’ or ‘Paid’
  • Tweaked the logic in the automation to use the Select List

Here is the updated code, for the Package file, and for the Lovelace Card

Package File - hp_envy_5540_instant_ink.yaml

# /config/packages/hp_envy_5540_instant_ink.yaml

##################################################################################################################
# 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: 500
    max: 10000
    step: 1
    icon: mdi:printer
    mode: box
# The current monthly allowance number of pages
# SET THIS ONCE FOR THE INAUGURAL RUN, and after then it is set by an automation
  hp_envy_5540_pages_monthly_allowance:
    name: Monthly Allowance Pages
    min: 15
    max: 300
    step: 1
    icon: mdi:printer
    mode: box
# The maximum number of rollover pages allowed.
# SET THIS ACCORDING TO YOUR PLAN
  hp_envy_5540_pages_rollover_monthly_max_allowance:
    name: Rollover Maximum Pages
    min: 0
    max: 600
    step: 1
    icon: mdi:printer
    mode: box
# The number of pages that remain of the rollover allowance for this month
# SET THIS ONCE FOR THE INAUGURAL RUN, and after then it is set by an automation
  hp_envy_5540_pages_rollover_allowance:
    name: Rollover Allowance Pages
    min: 0
    max: 600
    step: 1
    icon: mdi:printer
    mode: box
# The number of pages in each overprint block, when
# the Monthly Allowance plus Rollover are exceeded
# SET THIS ACCORDING TO YOUR PLAN
  hp_envy_5540_pages_overprint_block_size:
    name: Over Print Block Size
    min: 0
    max: 100
    step: 1
    icon: mdi:printer
    mode: box
# The cost of each overprint block, in £
# SET THIS ACCORDING TO YOUR PLAN
  hp_envy_5540_pages_overprint_block_cost:
    name: Over Print Block Cost
    min: 0
    max: 50
    step: 0.01
    icon: mdi:currency-gbp
    mode: box
# The start date of this period of the print plan
# SET THIS ONCE FOR THE INAUGURAL RUN, and after then it is set by an automation
input_datetime:
  hp_envy_5540_this_period_start_date:
    name: This Period Start Date
    has_date: true
    has_time: false
    icon: mdi:calendar
# SET THIS ACCORDING TO YOUR PLAN
input_select:
  hp_envy_5540_print_plan:
    name: Print Plan
    options:
      - Free
      - Paid
    initial: Free
    icon: mdi:printer

##################################################################################################################
# Sensors
##################################################################################################################
sensor:
- 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
          {% else %}
            {{ 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
# CHANGE THE CURRENCY CODE AS APPROPRIATE
    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: '£'

    # The number of pages remaining for this month in the chargeable Over Allowance area
    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 %}    

    # Next Renewal Date
    # WILL NOT WORK FOR 29th, 30th, 31st OF THE MONTH
    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 sensor for HP Envy 5540 Total Pages Printed
# YOU WILL NEED TO AMEND THE PRINTER URL
# YOU MAY NEED TO AMEND THE 'SELECT' - CHECK THE XML FILE OF YOUR PRINTER
- platform: scrape
  resource: http://my.hp_envy_5540.url.or.ip/DevMgmt/ProductUsageDyn.xml
  name: hp_envy_5540_total_pages_printed
  select: 'dd\:TotalImpressions[PEID="5082"]'
  value_template: >-
    {% if value == "" %}
      0
    {% else %}
      {{ value }}
    {% endif %}

##################################################################################################################
# Alternative sensors using the command line. These do not work in HASSOS
##################################################################################################################
#
# Used with input_datetime, & input_number, to track the state of HP Printer Instant Ink

# Gets the HP Printer Total Pages Printed from the printer's web service
# Inspired by: https://community.home-assistant.io/t/command-line-sensor-with-a-value-template-struggling/125957/3
# Needs package, 'xml-twig-tools', to be installed in Raspbian
#- platform: command_line
#  name: "HP Envy 5540 Total Pages Printed"
#  command: "xml_grep 'dd:TotalImpressions[@PEID=\"5082\"]' http://hp-envy.lan/DevMgmt/ProductUsageDyn.xml --text_only"
#  value_template: "{{ value }}"
# Gets the HP Ink Next Allowance Renewal Date, using the command line for the calculation

# It's a command line sensor because the command line is better at handling date arithmetic
#- 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 }}"




##################################################################################################################
# Automations
##################################################################################################################

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 is_state(''input_select.hp_envy_5540_print_plan'' , ''Free'') %}
                {## -=-=-= Free Plans accrue rollovers only from the OverPrint counter =-=-=- ##}
                {% if states(''sensor.hp_envy_5540_pages_overprint_remaining'')|int > 0 %}
                  {## -=-=-= If the OverPrint is in use, both the standard and rollover pages have been exhausted =-=-=- ##}
                  {{ states(''sensor.hp_envy_5540_pages_overprint_remaining'')|int }}
                {% else %}
                  {## -=-=-= If the OverPrint is not in use, then however many rollover pages that remain get carried over =-=-=- ##}
                  {{ states(''sensor.hp_envy_5540_pages_rollover_remaining'')|int }}
                {% endif %}
              {% else %}
                {## -=-=-= Paid Plans accrue rollovers from both Unused Standard Prints, and from the OverPrint counter =-=-=- ##}
                {% 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 %} 
                  {## -=-=-= If the OverPrint is in use, both the standard and rollover pages have been exhausted =-=-=- ##}
                  {{ 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 %}
                  {## -=-=-= The OverPrint is not in use, so the sum of the Unused Standard Pages plus the Unused Rollover get carried over, subject to the Max Rollover Allowance =-=-=- ##}
                  {{ 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 %}
              {% 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'') }}'

binary_sensor:
- platform: ping
  host: 192.168.1.50
  name: "hp_envy_5540"
  count: 2
  scan_interval: 30    

Lovelace Card - Status Card

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:
      - 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

Lovelace Card - Configuration Card

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_select.hp_envy_5540_print_plan
      - 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
2 Likes

Your work on this has been superb!! Maybe a HACS 2022 integration could be considered :thinking: :man_shrugging:t2: :grin:

@EventuallyFixed

I am trying to set the automation in a separate file in my automation folder/printer.

But i keep getting an error, i don’t know why because the code is the same, just a bit more sorted.

alias: HP OfficeJet 8022 Page Reset
description: >-
  Reset the Printer for Instant Inkt Pages.
  Reset on the 8 day of the month as start of the new monthly plan.

id: 0644d480-07a7-43bc-aed3-1cd6a9b01852
mode: single

trigger:
  - platform: time
    at: '00:30'

condition:
  - condition: template
    value_template: '{{ now().day == 08 }}'
  
action:
  - service: input_number.set_value
    entity_id: input_number.hp_officejet_8022_pages_rollover_allowance
    data_template:
      value: "{% if states('sensor.hp_officejet_8022_pages_allowance_remaining')|int +\
        \ states('sensor.hp_officejet_8022_pages_rollover_remaining')|int > states('input_number.hp_officejet_8022_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_officejet_8022_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int\
        \ + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"

  - service: input_datetime.set_datetime
    entity_id: input_datetime.hp_officejet_8022_this_period_start_date
    data_template:
      date: '{{ states(''sensor.date'') }}'

  - service: input_number.set_value
    entity_id: input_number.hp_officejet_8022_pages_at_month_start
    data_template:
      value: '{{ states(''sensor.hp_officejet_8022_total_pages_printed'') }}'

Error:

Logger: homeassistant.config
Source: config.py:464
First occurred: 20:20:28 (4 occurrences)
Last logged: 20:23:44

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'integer') for dictionary value @ data['condition'][0]['value_template']. Got None. (See ?, line ?).

You maybe know what i did wrong?

Hmmm… The error says, '(4 occurrences)` and there are four times that the ‘\n’ is in the Value statement. So I suggest you start by removing the ‘\n’.

If that does not work, I suggest you start with an unmodified version, then do the ‘sorts’ in smaller chunks, checking each time.

And the golden rule: Always start by taking a backup.