Page 1 of 1

Script to check if printer is still on when we go to bed

Posted: Sunday 19 July 2015 10:54
by ThinkPad
We have a printer (HP LaserJet 1010) connected to a printerserver (HP JetDirect 175x).
Because i hate standby usage of devices, especially when not using them because we're not home/sleeping, i have some 'standby-killing' switches in Domoticz.
The printerserver is behind a KaKu switch which is controlled by Domoticz (turned on from 6:30 - 23:00 when there is someone home, otherwise off).
The printer is turned on/off by hand (we don't print very often, so don't want it being on the whole day, only turn it on after we have hit the 'Print' button, page is cached by printerserver until it is printed by the printer).

When we go to bed we activate the 'sleeping' sequence on Domoticz, and the printerserver is being turned off. However, sometimes after printing we forget to turn off the printer itself and the printer is standby for the whole night. A waste of energy in my opinion. I created a little script that fetches the device status of the printerserver webpage. This script is being called when we activate the sleeping sequence in Domoticz and sends me a pushmessage to turn off the printer.

Code: Select all

#!/bin/bash
#ip='$1'
string="Device Not Attached"
    ping -c 1 $1 >/dev/null 2>&1
    if [ $? -ne 0 ] ; then #if ping exits nonzero...
        echo "Printerserver not responding to ping"
    else
        echo "Printerserver responds to ping"
        if curl -s "http://"$1"/index_info.htm" | grep -q "$string"; then
		    echo "Printer is turned off"
		else
		    echo "Printer is still on, please turn off"
		fi
    fi
You call it by running

Code: Select all

sh /home/pi/printer/printer.sh 192.168.4.2
For now i have only the 'echo' in it, in my own version i call a 'pushover.sh' script.
I could also place a KaKu-plug between the printer itself and turn it on when needed, and automatically turn off when we go to sleep. However this is easier to understand for my girlfriend. And i like fiddling around with creating scripts and such :mrgreen:


Scraping other info
The info returned by the webpage of my printerserver is very minimalistic. But most other networkprinters also report toner level and such. For example: you could modify this script to read the amount of toner and store it in a virtual Domoticz sensor (percentage/text) and/or send you a message when the toner is low.

To retrieve the status text (to display in a Domoticz text sensor for example) you could use something like this:

Code: Select all

curl -s "http://192.168.4.2/index_info.htm"|grep -Po '<td\s+class="if">\K.*?(?=</td>)'
It searches for the text in this td-class:

Code: Select all

<td class="if">Device Not Attached</td>

Re: Script to check if printer is still on when we go to bed

Posted: Sunday 19 July 2015 11:21
by ThinkPad
Question:
i now have the script in the 'OnAction' of the 'Sleeping' switch and that works. However i prefer using a Lua script to check for the 'DeviceChanged', as this is easier to backup/restore when installing Domoticz (i tend to forget those 'OnAction' things). I tried this tutorial: http://www.domoticz.com/wiki/Trigger_bash
But nothing happens.... When i call the bash script by hand it works fine.

Code: Select all

local sensor = 'Slapen'

commandArray = {}

if (devicechanged[sensor] == 'On') then
os.execute ("/home/pi/printer/printer.sh 192.168.4.2")
end

return commandArray

Re: Script to check if printer is still on when we go to bed

Posted: Monday 20 July 2015 14:53
by mrf68
When I run into problems when running scripts, normally the path to the script is incorrect, or the user is not allowed to run the script. So if you created it as ROOT, it probably won't run as PI, unless you corrected the security settings.

Re: Script to check if printer is still on when we go to bed

Posted: Monday 20 July 2015 14:59
by ThinkPad
mrf68 wrote:When I run into problems when running scripts, normally the path to the script is incorrect, or the user is not allowed to run the script. So if you created it as ROOT, it probably won't run as PI, unless you corrected the security settings.
Those are things i know as common causes for these issues, and have checked. But i will check again :mrgreen:

But calling a Bash script through Lua is difficult in debuggin. When i call the Bash script directly from Domoticz i would see an error in the log if something was not right. But with Lua nothing appears in the log.

Re: Script to check if printer is still on when we go to bed

Posted: Monday 20 July 2015 15:52
by mrf68
ThinkPad wrote: Those are things i know as common causes for these issues, and have checked. But i will check again :mrgreen:
I know you're a superuser (and noticed your nick on Tweakers), but I had to mention it, just in case.... :mrgreen:
ThinkPad wrote:But calling a Bash script through Lua is difficult in debuggin. When i call the Bash script directly from Domoticz i would see an error in the log if something was not right. But with Lua nothing appears in the log.
If it doesn't run, it won't show up in Domoticz log, but perhaps in /var/log/messages ?

Re: Script to check if printer is still on when we go to bed

Posted: Monday 20 July 2015 16:03
by pvm
You can also put some print statements in towards a temp logfile