Show values which are only temporary available (printer toner status)

Hello,

i have a question, maybe someone of you already have implemented this?
I have a laser printer which is discovered by HA automatically by the IPP integration.
It shows me the toner status - but only when the printer is turned on (which is quite seldom used).
So mainly it shows “unavailable” as values.

I would like to show the last known value (e.g. 15%) in lovelace and ignore any “unavailable”.
Anyone of you already solved this or can give me a hint how to do this?

Thanks in advance,
/ Ralf

I have the same situation with my inkjet printer. I intend to try using the saver custom component ( https://github.com/PiotrMachowski/Home-Assistant-custom-components-Saver ) to capture the state when connected, but it hasn’t got to the top of my list yet :slight_smile: Hopefully someone else will have made a bit more progress and help us both :slight_smile:

Hello, I also had the same request for my printer and here is how I did:

  1. Add custom fields in configuration.yaml for each of the values you want to save:
    image
  2. Create an automation that update those fields each time the printer is online:
  • Trigger type: State
  • Entity: Your printer
  • Condition type: Template
  • Template content:
{{ trigger.to_state.state not in [trigger.from_state.state, 'unavailable'] }}
  • Action type: Call a service
  • Service: input_text.set_value
  • Entity id: input_text.printer_black_left (create an action for each of your custom fields)
  1. Now you can use your custom fields in a lovelace card to display the last known values of your printer
4 Likes

Tried that using mqtt but still having the problem, that after HA restarts, the values are shown as ‘unavailable’?
What i am doing wrong, maybe someone can give me a hint, thanks a lot in advance!

Config as following:


sensor:        
  - platform: mqtt
    state_topic: "printer/black"
    name: "Schwarz"
    icon: mdi:water
    unit_of_measurement: '%'
automation:
  - alias: printerink_black_2mqtt
    initial_state: true   
    trigger: 
      - platform: state
        entity_id: sensor.samsung_c48x_series_black_toner_s_n_crum_17020338859
    condition:
      condition: template
      value_template: >-
        {{ trigger.to_state.state not in ['unavailable', trigger.from_state.state] }}
    action:
      service: mqtt.publish
      data_template:
        topic: "printer/black"
        payload: "{{ trigger.to_state.state }}"

I’m using mqtt for this as well. I only publish the ink level when the printer is online so I don’t have the problem that the state is “unavailable” after a restart.

sensor:
  - platform: mqtt
    name: "Patrone Schwarz"
    unit_of_measurement: '%'
    state_topic: "printer/black"

automation:
  - alias: 'Drucker - Patrone Schwarz'
    trigger:
      - platform: state
        entity_id: sensor.ink_level_black
    condition:
      - condition: state
        entity_id: device_tracker.epsondrucker
        state: 'home'
    action:
      - service: mqtt.publish
        data_template:
          topic: printer/black
          retain: true
          payload: '{{ states("sensor.ink_level_black") }}'
                  

@dennis84de great idea!
Can you please post your config to discover the printer?

Thanks!

I use the fritzbox integration for this.

But you can use every decivce tracker for this.

As far as i know, FritzBox has been moved towards an integration?
I also have FritzBox, so just add it like in the example docs?
Will try…

The device tracker is configured in yaml for now.
The integration in the UI is for switches and thermostats.

device_tracker:
  - platform: fritz
    host: !secret fritzbox_host
    username: !secret fritzbox_username
    password: !secret fritzbox_password
    interval_seconds: 5
    consider_home: 60
    new_device_defaults:
      track_new_devices: false

Great idea man!!! Love it.
I tried to set it up in my env. However, it seems that my automation is not working.
I have the input fields setup, I have the automation setup as follows:

- id: '1596010583695'
  alias: Update Printer Ink Levels
  description: ''
  trigger:
  - entity_id: sensor.canon_mx720_multifunctional
    from: unavailable
    platform: state
    to: idle
  condition:
  - condition: template
    value_template: '{{ states.sensor.canon_mx720_multifunctional.state not in [states.sensor.canon_mx720_multifunctional.state,
      ''unavailable'']}}'
  action:
  - data:
      entity_id: input_text.printer_black_left
    entity_id: input_text.printer_black_left
    service: input_number.set_value
  - data:
      entity_id: input_text.printer_cyan_left
    entity_id: input_text.printer_cyan_left
    service: input_number.set_value
  - data:
      entity_id: input_text.printer_magenta_left
    entity_id: input_text.printer_magenta_left
    service: input_number.set_value
  - data:
      entity_id: input_text.printer_yellow_left
    entity_id: input_text.printer_yellow_left
    service: input_number.set_value
  mode: single

However, when I turn on the printer and becomes available - changes the state from unavailable to idle, the debug gives me this error:

Logger: homeassistant.components.automation
Source: core.py:1235
Integration: Automation (documentation, issues)
First occurred: 11:33:06 AM (1 occurrences)
Last logged: 11:33:06 AM

While executing automation automation.update_printer_ink_levels
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/__init__.py", line 409, in async_trigger
    await self.action_script.async_run(variables, trigger_context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 790, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 185, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 193, in _async_step
    await getattr(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 382, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1235, in async_call
    processed_data = handler.schema(service_data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 208, in __call__
    return self._exec((Schema(val) for val in self.validators), v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 287, in _exec
    raise e if self.msg is None else AllInvalid(self.msg, path=path)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/validators.py", line 283, in _exec
    v = func(v)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 817, in validate_callable
    return schema(data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 272, in __call__
    return self._compiled([], data)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 594, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/usr/local/lib/python3.8/site-packages/voluptuous/schema_builder.py", line 432, in validate_mapping
    raise er.MultipleInvalid(errors)
voluptuous.error.MultipleInvalid: required key not provided @ data['value']

I do not know to what data[‘value’] it’s referring to.

Any ideas? and thank you very much.

Your action section needs to look a bit more like this:

  action:
  - data_template:
      value: '{{ states(''sensor.hp_photosmart_5520_series_black_ink'') }}'
    entity_id: input_text.printer_black_left
    service: input_text.set_value
  - data_template:
      value: '{{ states(''sensor.hp_photosmart_5520_series_cyan_ink'') }}'
    entity_id: input_text.printer_cyan_left
    service: input_text.set_value
  - data_template:
      value: '{{ states(''sensor.hp_photosmart_5520_series_magenta_ink'') }}'
    entity_id: input_text.printer_magenta_left
    service: input_text.set_value
  - data_template:
      value: '{{ states(''sensor.hp_photosmart_5520_series_yellow_ink'') }}'
    entity_id: input_text.printer_yellow_left
    service: input_text.set_value
  mode: single

Awesome, I did the modifications and now it works perfectly. Thank you very much.