Custom Component for printer ink levels

There is lots of info on those xml pages, you can grab whatever you want :wink:

1 Like

I think itā€™s even possible without a python script, just a template sensor, that parses xml?

Didnā€™t think you could parse XML with the scrape sensor, have you an example ?
Thanks

not sure, i just did a seach on community
this for example :

sensor:
      - platform: scrape
        resource: http://USERNAME:PASSWORD@IP:PORT/xml/ix.xml
        select: "ds2"
        value_template: '{{ (value | int) / 10 }}'
        unit_of_measurement: "Ā°C"
        name: Zgoraj-T

copy pasted from here : Scrape Sensor - XML reading

Great thanks, sure I tried a while back and couldnā€™t get it working, Iā€™ll have another look

@hijinx, looks amazing i tried using the card, and i get the following :

code:

              - type: custom:vertical-stack-in-card
                title: HP OfficeJet 3830
                cards:
                  - type: custom:bar-card
                    columns: 3
                    direction: up
                    entities:
                      - color: Black
                        entity: sensor.ink_black_level
                      - color: Color
                        entity: sensor.ink_color_level
                      - color: Gold
                        entity: sensor.pagecount
                      - color: Black
                        # entity: sensor.canon_mg5300_black_ink_level
                      # - color: Black
                        # entity: sensor.canon_mg5300_photoblack_ink_level
                    height: 200px
                    max: 100
                    min: 0
                    padding: 2px
                    target: 20
                    title_position: bottom
                    icon_position: inside
                    unit_of_measurement: '%'
                    width: 100%

in lovelace:

Any idea?

need to comment the last - color: Black I think

1 Like

Let us know how you get on with using scrape to do it. Would be great to get rid of the python script if we donā€™t need it.

The challenge may be that you have the two different values with the same element name. From that example I read, it assumes the name is unique.

Iā€™m trying to get this working for my color laser jet m175nw and I keep getting ā€œremote end closed connection without responseā€ even from the command line (not just through HA) . It seems to have all the exact pages and title/heading names as the other printers so I thought it would workā€¦ any ideas why it might be refusing the connection through urllib?

(homeassistant) homeassistant@hassnuc:~/.homeassistant$ python3 ./get_hp_info.py
Traceback (most recent call last):
  File "./get_hp_info.py", line 8, in <module>
    id_req1 = urllib.request.urlopen('http://192.168.1.2/DevMgmt/ConsumableConfigDyn.xml')
  File "/usr/local/lib/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/local/lib/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/local/lib/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/local/lib/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1345, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/local/lib/python3.7/urllib/request.py", line 1320, in do_open
    r = h.getresponse()
  File "/usr/local/lib/python3.7/http/client.py", line 1321, in getresponse
    response.begin()
  File "/usr/local/lib/python3.7/http/client.py", line 296, in begin
    version, status, reason = self._read_status()
  File "/usr/local/lib/python3.7/http/client.py", line 265, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

this happens with the version with the 3rd request for page count also (i thought maybe the 3 requests were coming too quickly and it was some sort of connection/rate limit)

Hi,
It could be that this script doesnā€™t work for your laser printer - itā€™s dependant on the availability of some XML flles which it parses.

Easiest way to check - is to use your browser - and enter the following url:

http://192.168.1.2/DevMgmt/ConsumableConfigDyn.xml

Do you get a XML output? If you get an error message - then the printer either doesnā€™t have them or something else doesnā€™t allow you.

Simple other question - is your Raspberry PI on the same subnet?

Yes, in a browser I get an xml file at all 3 of those pages referenced, with fields named the same for product status, consumable percentage, etc. (donā€™t have the exact names off hand on mobile now, but they matched)

Yes they are on the same subnet.

you changed IP addresses in that .py file?

Yes I did you can see that in the error message also.

Ok,

So stepping back a bit - are you running a Hassio instance of Home Assistant, or something else?

Also - from a quick skim using Google - are you experiencing any network issues on your network?

Do you have Wireless Isolation set on your router? Does that make a difference?

Regards,
James.

Home Assistant in a Venv
All on same network/router
And no network issues that Iā€™m aware ofā€¦ here is a screenshot from my HA machine opening that url directly from browser. Excuse the mobile screenshot Iā€™m at workā€¦

Messing around a little last night I get a curl command to return the xml file so i thought maybe urllib was out of date but I updated it with no change to result

Yeah - regarding the URLLib bit - thatā€™s where my head was at, but you are on Python 3.7 - so assumed you were up to date.

I originally wrote a script which used Curl & xmllint and then used bash to pull it all together. But when I ran it through HA, it couldnā€™t find the Curl / xmllint at run time - so I moved it to Python instead.

So I suppose that could be another way of doing it?

Alsoā€¦ Iā€™ve noticed that the namespaces you have for the XML file are subtly different, so the script would need to be updated as well. But, I donā€™t think that error message (which is on the urlopen) would cause that problem.

This is when I tried using scrape and command line with XML and failed:

Thats what I got, which forced my hand to switch to Python.

I got this working in the latest HA docker image which is based on Alpine. No official ink package for apk.

bash-5.0# apk add ink
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.10/community/x86_64/APKINDEX.tar.gz
ERROR: unsatisfiable constraints:
  ink (missing):
    required by: world[ink]

So what that means is you have to either build the package yourself or build from source.

This will require you to compose your own docker image.

You can download the latest source tarā€™s hereā€¦ Put these in a location that you will copy over into the building of the image.

For me I used a /tmp folder in my HA configuration directory.

wget http://downloads.sourceforge.net/ink/ink-0.5.3.tar.gz
wget http://downloads.sourceforge.net/libinklevel/libinklevel-0.9.3.tar.gz

In your docker-compose fileā€¦

ENV LOCAL_HOMEASSISTANT_TMP=/config/tmp
COPY ink-0.5.3.tar.gz $LOCAL_HOMEASSISTANT_TMP
COPY libinklevel-0.9.3.tar.gz $LOCAL_HOMEASSISTANT_TMP

RUN set -e && \
    apk update && \
    apk upgrade && \
    apk add --no-cache libusb-dev coreutils perl alpine-sdk imagemagick openssh-client curl vim jq && \

This will install a dependency that is needed ā€˜libusb-devā€™. It also adds in basic development tools as well as some other stuff I use. Remove whatever packages you do not need.

You cannot compile the source files during image build. At least I have not found a way. So unfortunately you will have to do this manually after the image is up.

This is done via two shell scripts. You can do it in one but I split it up to be organized.

libinklevel.sh

#!/bin/bash

cd /docker/homeassistant/tmp
tar xzf libinklevel-0.9.3.tar.gz
cd libinklevel-0.9.3
./configure
make
make install

And

ink.sh

#!/bin/bash

cd /docker/homeassistant/tmp
tar xzf ink-0.5.3.tar.gz
cd ink-0.5.3
./configure
make
make install

I put each of these into a folder under the HA config directory called /bash_scripts. Make sure to make each of those scripts executable. chmod +x script.sh

Once the container is up and running do thisā€¦

sudo docker exec <container_name> /bin/bash /config/bash_scripts/libinklevel.sh
sudo docker exec <container_name> /bin/bash /config/bash_scripts/ink.sh

That should get you ink working on the latest alpine based HA image.

Working, Thank you men.

1 Like