Instant Ink Countdown for Hp Printer Integration

sensor.hp_printer_printer este sensor no lo tengo disponible, pero tengo “sensor.hp_officejet_pro_8710” pero no da esa información

info: ‘HP OfficeJet Pro 8710 [****1]’
serial: *****Z
location: Despacho
state_message: ‘’
state_reason: null
command_set: >-
PCL3GUI,PCL3,PJL,Automatic,JPEG,PCLM,AppleRaster,PWGRaster,DW-PCL,802.11,802.3,DESKJET,DYN
uri_supported:

  • ‘ipp://192.168.1.***/ipp/print’
  • ‘ipps://192.168.1.***:443/ipp/print’
    friendly_name: HP OfficeJet Pro 8710
    icon: ‘mdi:printer’

me pueden ayudar

I was having a problem with this solution working. This is an edited post and I’ve included the solution for me, which was to use data_template: instead of data:. This seems to crop up quite regularly for me! :man_facepalming:t2:

- id: hp_instant_ink_per_month
  alias: HP Instant Ink per Month
  trigger:
  - platform: time
    at: '06:00:00'
  condition:
  - condition: template
    value_template: "{{ now().day == 24 }}"
  action:
  - service: var.set
    data_template:
      entity_id: var.printer_pages_at_month_start
      value: "{{ states('sensor.hp_printer_printer') | int }}"
1 Like

Hi,

Inspired by the posts above, I made an implementation of Instant Ink which should cover the monthly allowance, rollover and over-print scenarios. It uses only native HA components: sensors, input_date, input_number, and an automation.

Edit: Updates are available below, for

  • The Automation that resets and rolls-over values on the anniversary day.
  • Template Sensors, to remove the dependency on the deprecated ‘entity_id’ attribute.
  • Another option to calculate the next month renewal date (useful in HASSOS).

Also note that if your renewal date is on the 29th, 30th, or 31st, of the month, the ‘Next Renewal Date’ calculation will go askew where the number of days in the next month is less than that date - Feel free to offer a fix! :slight_smile:

It all hangs off a page of XML that’s provided by the printer - which I suspect is used by HP for the same purpose - and is found at: http://[Printer IP]/DevMgmt/ProductUsageDyn.xml.

Diving into the configuration, I have base information in input_number and input_datetime objects, which allows them to be set using cards in the front end of Home Assistant:

# 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 the automation - id: '1588925532080'; alias: HP Envy 5540 Page Reset.
  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 manually in a front-end card
  hp_envy_5540_pages_monthly_allowance:
    name: Monthly Allowance Pages
    min: 15
    max: 300
    step: 5
    icon: mdi:printer
    mode: box
# The maximum number of rollover pages allowed
# Set manually in a front-end card
  hp_envy_5540_pages_rollover_monthly_max_allowance:
    name: Rollover Maximum Pages
    min: 0
    max: 600
    step: 5
    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_5540_pages_rollover_allowance:
    name: Rollover Allowance Pages
    min: 0
    max: 600
    step: 5
    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_5540_pages_overprint_block_size:
    name: Over Print Block Size
    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_5540_pages_overprint_block_cost:
    name: Over Print Block Cost
    min: 0
    max: 50
    step: 0.05
    icon: mdi:currency-gbp
    mode: box
# The start date of this period of the print plan
# Set by the automation - id: '1588925532080'; alias: HP Envy 5540 Page Reset.
input_datetime:
  hp_envy_5540_this_period_start_date:
    name: This Period Start Date
    has_date: true
    has_time: false
    icon: mdi:calendar

Here I have some sensors to detect the total number of pages printed (using the printer’s XML service), and the next period renewal date. Note that I have my sensors in a separate config file from configuration.yaml.

# Gets the HP Printer Total Pages Printed from the printer's web service
# 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
- platform: command_line
  name: "HP Envy 5540 Total Pages Printed"
  command: "xml_grep 'dd:TotalImpressions[@PEID=\"5082\"]' http://<Printer IP>/DevMgmt/ProductUsageDyn.xml --text_only"
  value_template: "{{ value }}"
#
# Gets the HP Ink Next Allowance Period Start Date, using the command line to do the calculation.
# The command line method is simpler for 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 }}"
#

Now come the sensors to detect the number of pages remaining in the various buckets.
Generally the calculations are along the lines of:

‘Number of pages printed at the start of the period’
plus ‘Number of pages of this month’s allowance’
[plus ‘Number of pages of this month’s rollover allowance’]
minus ‘Number of pages printed right now’

# 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/time_date/
# Use of sensor.date forces a daily update
    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
    hp_envy_5540_pages_allowance_remaining:
      friendly_name: "Allowance Pages Remaining"
      value_template: >-
        {% 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 %}
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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_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 %}
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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_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 %}    
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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('sensor.hp_envy_5540_pages_overprint')|int > 0 %}
          {{ (1 + (states('sensor.hp_envy_5540_pages_overprint')|int / 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 %}
            0
        {% endif %}    
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date

Finally, there’s an automation which runs at the start of each ink period, which here is the 3rd day of the month. This cannot be edited in the front end.

- id: '1582325531280'
  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'
  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_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'') }}'
  - 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_allowance_remaining')|int +\
        \ states('sensor.hp_envy_5540_pages_rollover_remaining')|int > states('input_number.hp_envy_5540_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_envy_5540_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_envy_5540_pages_allowance_remaining')|int\
        \ + states('sensor.hp_envy_5540_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"

Earlier I mentioned a card that can be placed on the front end, and here it is:

cards:
  - entities:
      - entity: input_datetime.hp_envy_5540_this_period_start_date
      - entity: input_number.hp_envy_5540_pages_at_month_start
      - 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_overprint_block_size
      - entity: input_number.hp_envy_5540_pages_overprint_block_cost
    title: HP Envy 5540 Instant Ink Configuration
    type: entities
type: vertical-stack

And to display the current values…

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
    title: HP Envy 5540 Instant Ink Status
    type: entities
type: vertical-stack

So hopefully this will help some folks. :slight_smile:

Also hopefully there are no bugs or typos! :slight_smile:

5 Likes
# 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/time_date/
# Use of sensor.date forces a daily update
    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
    hp_envy_5540_pages_allowance_remaining:
      friendly_name: "Allowance Pages Remaining"
      value_template: >-
        {% 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 %}
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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_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 %}
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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_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 %}    
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date
#
# 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('sensor.hp_envy_5540_pages_overprint')|int > 0 %}
          {{ (1 + (states('sensor.hp_envy_5540_pages_overprint')|int / 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 %}
            0
        {% endif %}    
      entity_id: sensor.hp_envy_5540_total_pages_printed,input_datetime.hp_envy_5540_this_period_start_date

Where to place this code, is it part of the sensor?

@EventuallyFixed Can you help with this question?

Yes, these are Template Sensors.

I have since made changes to the code above, to take account of the breaking change for Template Sensors, caused by the removal of the ‘entity_id’ attribute.

It means that the Template Sensors now look like this:

- 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 %}
            {{ (1 + (states('sensor.hp_envy_5540_pages_overprint')|int / 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 %}
            0
          {% endif %}    
        {% endif %}    
      unit_of_measurement: '£'


hi,
i adapted your code to my printer (hp 6230) but i have problems with this sensor. as a result it returns me -145625 days.
how can i solve? what does your 86400 mean?
thanks

Hi,

I’m glad that this has been of some some interest! :smile:

Since I can’t shadow you on your PC, it’s going to be quite hard to debug.

  • I don’t know exactly what is meant by ‘adapted’, so I suggest you revert back to the backup you took of your configuration files before you made the changes, then insert these changes as-they-are, and finally set the values in the configuration card, and the printer IP address in the ‘HP Envy 5540 Total Pages Printed’ sensor. At that point, it ought to work.

From here, all I can do is share my dashboard page, and tell you how the date portion works.

  • The “input_datetime.hp_envy_5540_this_period_start_date” is set manually: It should be set to the date that is your particular Start Date for the current period of Instant Ink. You can see this date at the top of the “HP Envy Instant Ink Configuration” card, on the right-hand side of my dashboard. Note that all of the values in this card must be set (except for Total Pages Printed, which is grabbed from the printer). You must also set the automation to the same day number of the month, so that the automation will update it on that day. (The automation has been updated - see below)
  • The ‘Next Renewal Date’ is set in the Command Line Sensor, “HP Envy 5540 Next Renewal Date”, and it depends on the ‘This Period Start Date’ above being set.
  • The piece of code you are referring to, is subtracting the UNIX Timestamp value of ‘Today’ from the UNIX Timestamp value of the ‘Next Renewal Date’. That answer is in units of seconds, so the division by 86,400 converts that answer into units of days (86,400 is, of course, the number of seconds in a day: 60seconds * 60minutes *24hours).

Because you have a large and negative answer for this, it suggests that the ‘This Period Start Date’ is either not set, or is set to a date that is way in the past. So what is it’s value?

Edit:
I have now improved the automation, so that it uses the day number of the ‘This Period Start Date’, instead of having to enter this day number in to two places. The automation now looks like this:

- id: '1582325531280'
  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'
  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_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'') }}'
  - 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_allowance_remaining')|int +\
        \ states('sensor.hp_envy_5540_pages_rollover_remaining')|int > states('input_number.hp_envy_5540_pages_rollover_monthly_max_allowance')|int\
        \ %}\n  {{ states('input_number.hp_envy_5540_pages_rollover_monthly_max_allowance')|int\
        \ }}\n{% else %}\n  {{ states('sensor.hp_envy_5540_pages_allowance_remaining')|int\
        \ + states('sensor.hp_envy_5540_pages_rollover_remaining')|int }}\n{% endif\
        \ %}"

Text

hi @EventuallyFixed , thanks for the reply. I simply replaced 5540 with 6230 every time I found it and entered the ip of my printer.
but i have these errors

What is the value of your ‘input_datetime.hp_envy_5540_this_period_start_date’?

The “ input_datetime.hp_envy_5540_this_period_start_date ” is set manually: It should be set to the date that is your particular Start Date for the current period of Instant Ink. You can see this date at the top of the “ HP Envy Instant Ink Configuration ” card, on the right-hand side of my dashboard. Note that all of the values in this card must be set (except for Total Pages Printed, which is grabbed from the printer).

Screen grab what you have for this whole card (HP Envy Instant Ink Configuration) & attach, please.
What_Is_This_Set_To

Additionally, I see from your screenshot that ‘sensor.hp_envy_6320_total_pages_printed’ is set to Unknown.

  • Do you have a printer page at http://[ Printer IP ]/DevMgmt/ProductUsageDyn.xml? Without that page being available, the page tracking won’t work.
  • Did you set your Printer IP address in the ‘HP Envy 5540 Total Pages Printed ’ sensor?
  • Did you install the package, ‘xml_twig-tools’ in the underlying Operating System?
  • I guessed that the XML tag & attribute ‘dd:TotalImpressions[@PEID="5082"]’ held the total number of pages printed, and I was lucky enough to be correct. YMMV.

@EventuallyFixed here is the screenshot.
I tried to follow it step by step and I think the problem is on this sensor which gives me value 0.

- platform: command_line
  name: "HP Envy 6230 Next Renewal Date"
  command: "date -d '{{ states('input_datetime.hp_envy_6230_this_period_start_date') }} 1 month' +%Y-%m-%d"
  value_template: "{{ value }}"

the date sensor works fine, i think the problem is on the one mentioned earlier

yes, I have that page, I set the ip but it doesn’t read it to me.
oh,
I have not installed the package " xml_twig-tools "
but i think i can solve with the original integration sensor which works fine. I’ll just have to change part of the code

As per my code comments, that sensor is an OS Command, which works (for me) on Raspbian (linux). I’m pretty familiar with Linux, so I found it easier to do this, than to use Jinja. You may want to try that command in the OS, to check that it works.

An alternative option would be to use a Template Sensor instead. Something like this, which I have just thrown together and should work for where the day number is 1 to 28. Note that I have not tested this, other than to verify that a timestamp is produced from both possibilities of answer…

        {% if (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1 > 12) %}
          {{ as_timestamp(strptime( (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] , '%Y-%m-%d')) }}
        {% else %}
          {{ as_timestamp(strptime( states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[0]|int|string + "-" + (states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[1]|int + 1)|string + "-" + states('input_datetime.hp_envy_5540_this_period_start_date').split("-")[2] , '%Y-%m-%d' )) }}
        {% endif %}

@EventuallyFixed thank you for the help you are giving me.
I have tested your updated code but it does not give a valid result.
i am on hassos on raspberry4.

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.