Thanks for the kind comment @AJStubbsy, while I’m on my break from the workplace over the coming holidays, I’ll look into what doing that will look like.
Thank you for your respond,
I think i have found it:
value_template: '{{ now().day == 08 }}'
changed the day == to 8, without the 0. and then no errors anymore… Don’t know why 08 gave the error.
one other question, is it possible to get a notification when the cycle restarts, so for me on the 8 day of the month, and the automation did his reset, than send a notification.
Or maybe also when you have 5 pages left for the month or someting?
Something like this, but i do not know how to format it correctly:
---
# Automation for resetting the printer instant Inkt Plan at the 8th day of the month
#
alias: Printer 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 == 8 }}'
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'') }}'
> - service: notify.mobile_app_galaxy_s8_rick_sm_g955f
data:
message: Pages Reset
Probably. Most things are possible, but it’s a question of how, and of time. What do you mean by, ‘a notification?’
It’s very likely that a notification would be implemented in a separate automation.
For an email, there is the additional prerequisite of a working connection to an SMTP server, and what choice of wording to use.
At this time I don’t know what a notification to the Home Assitant App would mean, in terms of what to do.
Ow true, sorry i forgot to explain better. (did not even know about email notifications haha)
Example, i found a shutdown notification example:
trigger:
platform: homeassistant
event: shutdown
action:
- service: notify.mobile_app_galaxy_s8_rick_sm_g955f
data:
message: Home Assistant wordt afgesloten
This uses the notify to send a message to my phones home assistant app.
I am looking for something like this when for example my Total Pages Left hits the 10.
Or when the monthly cycle resets.
I thought it would maybe be possible to include it in the existing printer automatisation, or is it better to create a sepperate one?
This is a great contribution to HA and the community. I’ll have to wait for the add-on unfortunately, as I don’t use packages! I’m only joking as I know it would be difficult to design for an version of HA you don’t run but this might spark the interest of someone who uses HAOS to get involved, so thank you very much
Notifications for overprinting anc costs
---
#
# Printer HP OfficeJet 8022 Overprinted
#
description: Printer Aantal Pagina's in plan Overschreden
alias: Printer HP OfficeJet 8022 Overprinted
id: '1638880810920'
mode: single
trigger:
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '0'
to: '1'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '1'
to: '2'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '2'
to: '3'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '3'
to: '4'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '4'
to: '5'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '6'
to: '7'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '8'
to: '9'
- platform: state
entity_id: sensor.hp_officejet_8022_pages_overprint_cost
from: '9'
to: '10'
condition:
- condition: numeric_state
entity_id: sensor.hp_officejet_8022_pages_overprint
above: '0'
action:
- service: notify.all_phones
data:
message: >-
Aantal Pagina's voor deze Maand is overschreden, huidige extra kosten
zijn € {{ states('sensor.hp_officejet_8022_pages_overprint_cost') }},-.
Nog {{ states('sensor.hp_officejet_8022_pages_overprint_remaining') }}
pagina's voordat er weer € {{
states('sensor.hp_officejet_8022_pages_overprint_cost') }},- bijkomt.
title: Printer - Aantal Maandelijkse pagina's Overschreden
The package is great, thanks
By default, sensor.date
doesn’t exist therefore Allowance Days Remaining was showing up for me as 19077… Adding this to configuration.yaml
fixed it for me…
sensor:
- platform: time_date
display_options:
- 'time'
- 'date'
- 'date_time'
As of the new HA versions, some templates need a default value.
It is giving some new errors now:
Error initializing 'template' trigger for 'Printer - HP OfficeJet Alert X Pages Remain': ValueError: Template error: int got invalid input 'unknown' when rendering template '{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %} true {% endif %}' but no default was specified
Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:356
First occurred: 23:51:27 (2 occurrences)
Last logged: 23:51:27
TemplateError('ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}' but no default was specified') while processing template 'Template("{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}")' for attribute '_attr_native_value' in entity 'sensor.hp_officejet_8022_total_free_pages_remaining'
TemplateError('ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}' but no default was specified') while processing template 'Template("{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}")' for attribute '_attr_native_value' in entity 'sensor.hp_officejet_8022_allowance_days_remaining'
Can someone please help me fix the errors? I do not know where to put the default values (and what values are recommended.
And one larger error:
2022-09-10 23:51:27.535 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 419, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1904, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1696, in forgiving_int_filter
raise_no_default("int", value)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1393, in raise_no_default
raise ValueError(
ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}' but no default was specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 535, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}' but no default was specified
2022-09-10 23:51:27.553 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: int got invalid input 'unknown' when rendering template '{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}' but no default was specified') while processing template 'Template("{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int }}")' for attribute '_attr_native_value' in entity 'sensor.hp_officejet_8022_total_free_pages_remaining'
2022-09-10 23:51:27.560 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1608, in strptime
return datetime.strptime(string, fmt)
File "/usr/local/lib/python3.10/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/local/lib/python3.10/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data 'unknown' does not match format '%Y-%m-%d'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 419, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1904, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
return __context.call(__obj, *args, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1611, in strptime
raise_no_default("strptime", string)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1393, in raise_no_default
raise ValueError(
ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}' but no default was specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 535, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}' but no default was specified
2022-09-10 23:51:27.566 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}' but no default was specified') while processing template 'Template("{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8022_next_renewal_date'), '%Y-%m-%d'))|int - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int ) / 86400)|int }}")' for attribute '_attr_native_value' in entity 'sensor.hp_officejet_8022_allowance_days_remaining'
2022-09-10 23:51:42.848 WARNING (MainThread) [homeassistant.components.template.trigger] Error initializing 'template' trigger for 'Printer - HP OfficeJet Alert X Pages Remain': ValueError: Template error: int got invalid input 'unknown' when rendering template '{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %}
true
{% endif %}' but no default was specified
2022-09-10 23:51:42.864 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %}
true
{% endif %}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 419, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1904, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1696, in forgiving_int_filter
raise_no_default("int", value)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1393, in raise_no_default
raise ValueError(
ValueError: Template error: int got invalid input 'unknown' when rendering template '{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %}
true
{% endif %}' but no default was specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 535, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 421, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: int got invalid input 'unknown' when rendering template '{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %}
true
{% endif %}' but no default was specified
2022-09-10 23:51:43.196 ERROR (MainThread) [homeassistant.components.homeassistant_alerts] Timeout fetching homeassistant_alerts data
2022-09-10 23:51:43.236 WARNING (MainThread) [homeassistant.components.template.trigger] Error evaluating 'template' trigger for 'Printer - HP OfficeJet Alert X Pages Remain': ValueError: Template error: int got invalid input 'unknown' when rendering template '{% if states('sensor.hp_officejet_8022_total_free_pages_remaining')|int == states('sensor.hp_officejet_8022_free_pages_remaining_notification')|int %}
true
{% endif %}' but no default was specified
I don’t know what the recommended values are (I haven’t got this configured in the same way as you) but you can add a default by putting it in brackets after the int i.e. |int(0)
You might as well give 0 a try, as it’s just to get past the initial setup and then it’s actual value will be known.
Thanks for the reply!
I tried with a few values, but still errors. Do i neet to put (0) after EVERY “int”? even in the middle of the line of code?
Example
'{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int**(0)** + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int**(0)** }}'
Sorry it’s probably quite late to reply to this, hopefully you got it sorted. You should set a default value for any of the states that are going to generate the error. It’s only a temporary state, so no harm.
{{ states('sensor.hp_officejet_8022_pages_allowance_remaining')|int(0) + states('sensor.hp_officejet_8022_pages_rollover_remaining')|int(0) }}
Should stop the error.
For one of your errors, I vaguely remember some issue about as_timestamp. Might be worth having a look through the forums to see if there is a better way to write that template.
Thanks! i Sorted it out with help
Thank you for all the work you’ve done on this topic - you helped me a lot to build my HP Instant ink Dashboard
Hi there,
I’ve been using this for a while and have recently been experiencing some issues (can’t pinpoint when this happened, unfortunately).
It seems that the issues are connected to the fact that I’m not able to get the total pages printed from the xml anymore.
The following sensor returns “unknown”
- platform: scrape
resource: http://IP/DevMgmt/ProductUsageDyn.xml
name: hp_officejet8720_total_pages_printed_xml
select: 'dd\:TotalImpressions[PEID="5082"]'
value_template: >-
{{ value|int(-1) }}
This is the value I’m trying to get:
Meanwhile, hp_officejet8720_total_pages_printed
(which is basically the backup of the xml value) is stuck at a number about 20 pages ago.
Did I miss something?
I’ve already tried the following options for select:
'dd:TotalImpressions[PEID="5082"]'
'dd:TotalImpressions PEID="5082"'
'dd\:TotalImpressions PEID="5082"'
Would be amazing if anybody had an idea why I’m getting the unknown for this
The scrape sensor has been updated. Change it to this:
scrape:
- resource: http://IP/DevMgmt/ProductUsageDyn.xml
sensor:
- name: "hp_officejet8720_total_pages_printed_xml"
select: 'dd\:TotalImpressions[PEID="5082"]'
value_template: >-
{% if value == "" %}
0
{% else %}
{{ value }}
{% endif %}
You could also follow the github instructions and use the scrape sensor UI integration but you don’t have to.