Integrating Domoticz with a Printer

Published: April 18, 2021

I created scripts that let you conveniently manage a printer installed on the system. The first script automatically retrieves the status of the printer installed on the system and sends it to Domoticz. The second lets you clear the system print queue.

Requirements

  • A Linux-based device (such as Raspberry Pi) with Python 3 installed

  • A connected and configured printer

  • The following Python package installed on the device: requests

  • The script saves the last printer status it read to a file to avoid sending unnecessary requests to Domoticz, so make sure it has write permission for that location

Installation and usage

Prepare the scripts

Install the required Python packages.

pip3 install requests

Clone the repository into any directory on your device, for example your home directory.

cd
git clone https://github.com/paulomac1000/Domoticz-System-Printer

Go to the script directory.

cd Domoticz-System-Printer

Printer status display

Open the Domoticz panel in your browser, then select the "Setup" tab and "Hardware".

Add a device named "Printer" with the type "Dummy (Does nothing, use for virtual switches only)". Leave the other options unchanged. Confirm by clicking "Add".

The newly added device should appear in the list above. Now click the "Create Virtual Sensors" button in the "Type" column.

A dialog will open where you need to add a new sensor named "Printer status" with the type "Text".

Now go to the "Utility" tab.

Find the new sensor and click "Edit".

The newly opened dialog contains an "Idx" row. Save its value somewhere.

Go back to the console. First, check the name of your printer using the command below.

lpstat -p -d

You will see something similar to the screenshot below. In my case, there is one extra printer created by VNC Viewer, but it is disabled, so I don't worry about it. The one I'm interested in is HP_Deskjet_2640_series.

Next, configure the script.

nano systemPrinterStatus.py

Go to line 6 and replace the printer name with the one you just found.

printer_name = 'your printer name'

Next, go to line 8 (the code below) and replace the URL with the address of your Domoticz instance, or leave it unchanged if the script will run on the same device.

DOMOTICZ_SERVER = "127.0.0.1:8080"

Then go to line 12 (the code below) and enter the Idx identifier of the device you added.

printer_device_id = 99

Close the file using ctrl+x and save the changes. To run the script, execute it from the command line.

python3 systemPrinterStatus.py

If everything worked correctly, you can add the script to crontab.

crontab -e

crontab does not support running jobs more often than once per minute, and we want to run it every 15 seconds so we don't miss a status change. To do this, we'll use a workaround — add the script 4 times to run every minute, but delay each entry by 15 seconds. Add the following entries to the crontab file:

* * * * * sleep 00; timeout 15s python3 /home/pi/Domoticz-System-Printer/systemPrinterStatus.py
* * * * * sleep 15; timeout 15s python3 /home/pi/Domoticz-System-Printer/systemPrinterStatus.py
* * * * * sleep 30; timeout 15s python3 /home/pi/Domoticz-System-Printer/systemPrinterStatus.py
* * * * * sleep 45; timeout 15s python3 /home/pi/Domoticz-System-Printer/systemPrinterStatus.py

Close the file using ctrl+x and save the changes.

Print queue clearing

Go to the script directory in the Domoticz home directory.

cd /home/pi/domoticz/scripts

Create a script that clears the print queue by calling systemPrinterClearQueue.py from the repository in your home directory. To do this, create the script with the nano command.

nano printer_clearQueue.sh

Paste the following content into it.

#!/bin/bash
python3 /home/pi/Domoticz-System-Printer/systemPrinterClearQueue.py

Close the file using ctrl+x and save the changes.

Open the Domoticz panel, expand "Settings", select "Hardware" and find the hardware you added earlier with the name of your printer.

Now click the "Create Virtual Sensors" button in the "Type" column. A dialog will open where you need to add a new sensor named "Clear print queue" with the type "Switch".

Now go to the "Switches" tab.

Find the new sensor and click "Edit".

Select the "Push On Button" switch type, then enter script://printer_clearQueue.sh in the "On Action" field. Save the changes and check that it works.

Comments (0)

No comments yet.

Add a comment

Comments are published after moderation. Your e-mail address stays private.