Sureflap Connect - Pet Door

Goodday,

When trying to get the pet i got a valid response (fist line)
After this there are a lot of errors.
as expected hass didn’t understand this.
Do i mis some components?

Thanks
Mark

root@Hass01:/home/homeassistant/.homeassistant/sure_petcare# python sp_cli.py ls_pets
Catname (*number*) is Inside
Traceback (most recent call last):
  File "sp_cli.py", line 156, in <module>
    main( sys.argv )
  File "sp_cli.py", line 80, in main
    CMDS[args.cmd[0]]( sp, args )
  File "sp_cli.py", line 110, in cmd_ls_pets
    print( '%s (%s) is %s' % (pdata['name'], pid, sp.get_current_status( pid ),) )
  File "/home/homeassistant/.homeassistant/sure_petcare/sure_petcare/__init__.py", line 761, in get_current_status
    loc = self.get_pet_location( pet_id, household_id )
  File "/home/homeassistant/.homeassistant/sure_petcare/sure_petcare/__init__.py", line 200, in get_pet_location
    raise SPAPIUnknownPet()
sure_petcare.SPAPIUnknownPet
root@Hass01:/home/homeassistant/.homeassistant/sure_petcare# python sp_cli.py ls_pets
Traceback (most recent call last):
  File "sp_cli.py", line 7, in <module>
    import sure_petcare
  File "/home/homeassistant/.homeassistant/sure_petcare/sure_petcare/__init__.py", line 9, in <module>
    import requests
ImportError: No module named requests

Hope I had seen this discussion before buying and installing Sure Flap (non smart) although with chip detection. I would love to make the existing one smart enough to detect and report.

  • Cat is out/ in
  • Exit and Entry count for day
  • Time of last exit
  • Time of last entry

I was thinking of using an RF Door sensor to detect the opening of the flap paired with an RF Bridge. I think with my limited ability can achieve detecting and reporting triggering of sensor but need pointers to the code. Do you think this is a good way to go about it or is there a simpler solution.

Appreciate any help provided,

Thank you for this! :slight_smile:
I’m very new to HA, but I have got the “sensor.surepet_connect” to give a Json output like this:
{"Kakan": "Inside","Cappuccino": "Inside","avg_battery": 24, "battery": 24,"flap_online": true,"hub_online": true,"lock_status": "Keep pets in","locked": true}
And that’s all good, so the “sensor” is working.
So in my configuration.yaml
I have added this so far under sensor:

  # Sure PetCare Flap Door
  - platform: sure_petflap
    username: !secret surepet_user
    password: !secret surepet_pass

So far so good, I get the Json above, so I assume it works. :slight_smile:
Then I try to add your “Template code” underneath (in the configuration.yaml (is that correct??))

So it looks like this (have two cats…):

  # Sure PetCare Flap Door
  - platform: sure_petflap
    username: !secret surepet_user
    password: !secret surepet_pass

  - platform: template
    sensors:
      Kakan_status:
        friendly_name: "Kakans Status"
        value_template: "{{ state_attr('sensor.surepet_connect', 'Kakan') }}"
      Cappuccino_status:
        friendly_name: "Cappuccinos Status"
        value_template: "{{ state_attr('sensor.surepet_connect', 'Cappuccino') }}"      
      flap_battery_status:
        friendly_name: "Sure Flap Battery"
        value_template: "{{ state_attr('sensor.surepet_connect', 'battery') }}"
        unit_of_measurement: '%'
      flap_batterystatus:
        friendly_name: Sure Flap Battery
        value_template: >
          {% if is_state('sensor.surepet_connect', 'unknown') %}
            110
          {% else %}
            {{ states.sensor.surepet_connect.attributes["battery"] | float}}
          {% endif %}
        icon_template: > 
          {% set battery_level_xod1 = states.sensor.surepet_connect.attributes.battery|default(0)|int %}
          {% set battery_round_xod1 = (battery_level_xod1 / 10) |int * 10 %}
          {% if battery_round_xod1 >= 100 or is_state('sensor.surepet_connect', 'unknown') %}
            mdi:battery
          {% elif battery_round_xod1 > 0 %}
            mdi:battery-{{ battery_round_xod1 }}
          {% else %}
            mdi:battery-charging-wireless-outline
          {% endif %}
        unit_of_measurement: '%'

  - platform: history_stats
    name: Kakans outside time (today)
    entity_id: sensor.Kakan_status
    state: 'Outside'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
  - platform: history_stats
    name: Kakans trips outside (today)
    entity_id: sensor.Kakan_status
    state: 'Outside'
    type: count
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
  - platform: history_stats
    name: Kakan outside (yesterday)
    entity_id: sensor.Kakan_status
    state: 'Outside'
    type: time
    end : '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      hours: 24
  - platform: history_stats
    name: Kakans trips outside (yesterday)
    entity_id: sensor.Kakan_status
    state: 'Outside'
    type: count
    end : '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    duration:
      hours: 24

binary_sensor:
  - platform: template
    sensors:
      lock_status:
        friendly_name: "Sure Flap"
        device_class: lock
        value_template: "{{ not states.sensor.surepet_connect.attributes.locked}}"
        icon_template : >
          {% if states.sensor.surepet_connect.attributes.locked %}
            mdi:lock
          {% else %}
            mdi:lock-open
          {% endif %}
      flap_status:
        friendly_name: "Flap online Status"
        device_class: connectivity
        value_template: "{{ states.sensor.surepet_connect.attributes.flap_online }}"
        icon_template : >
          {% if states.sensor.surepet_connect.attributes.flap_online %}
            mdi:wifi
          {% else %}
            mdi:wifi-off
          {% endif %}
      hub_status:
        friendly_name: "Hub online Status"
        device_class: connectivity
        value_template: "{{ states.sensor.surepet_connect.attributes.hub_online }}"
        icon_template : >
          {% if states.sensor.surepet_connect.attributes.hub_online %}
            mdi:wifi
          {% else %}
            mdi:wifi-off
          {% endif %}

But when I do “check config” I get’s lot’s of errors:
Invalid config for [sensor.template]: invalid slug Kakan_status (try kakan_status) for dictionary value @ data['sensors']. Got OrderedDict([('Kakan_status', OrderedDict([('friendly_name', 'Kakans Status'), ('value_template', "{{ state_attr('sensor.surepet_connect', 'Kakan') }}")])), ('Cappuccino_status', OrderedDict([('friendly_name', 'Cappuccinos Status'), ('value_template', "{{ state_attr('sensor.surepet_connect', 'Cappuccino') }}")])), ('flap_battery_status', OrderedDict([('friendly_name', 'Sure Flap Battery'), ('value_template', "{{ state_attr('sensor.surepet_connect', 'battery') }}"), ('unit_of_measurement', '%'.... (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

I tried to understand the documentation, but coming from the world of LUA in a Fibaro-box I have to learn. I’m not sure where to start looking for “errors”. Or what I have missed/messed up.
As soon as I remove the “template” stuff, it works. :slight_smile:

Can you just point out the basics? :slight_smile:

Thank you.

Found the problem. I had used capital letters in the “variables”. That’s not allowed I think… :slight_smile:
When I changed that it worked! Also, if anyone else is going down this path… The variables has to be added to the “recorder” if you are not including everything by default. :slight_smile: Otherwise the “statistics” will of course not work…

1 Like

Finally figured some stuff out! :slight_smile:

@asif Thank you for sharing!!

I have added a “lock_mode” variable to show HOW the door is locked. (“Keep pets in”, Keep pets out"… and so on).

Here is the template code (it’s my first time to make something, so be gentle). :slight_smile:

  lock_mode:
    friendly_name: "Lock Mode"
    value_template: "{{ state_attr('sensor.surepet_connect', 'lock_status') }}"
    icon_template : >
      {% if states.sensor.surepet_connect.attributes.lock_status == 'Keep pets in' %}
        mdi:application-import
      {% elif states.sensor.surepet_connect.attributes.lock_status == 'Keep pets out' %}
        mdi:application-export
      {% elif states.sensor.surepet_connect.attributes.lock_status == 'Lock both ways' %}
        mdi:lock
      {% elif states.sensor.surepet_connect.attributes.lock_status == 'Curfew mode' %}
        mdi:lock-clock
      {% else %}
        mdi:lock-open
      {% endif %}

I just added this in between the “jenna_status” variable and the “flap_battery_status” variable.

And lastly, my full “page”:

2 Likes

If anyone else is a crazy cat person like me and wants to treat their cats like people this is what I did to turn the state attributes of the cat flap into device trackers which can then be assigned to “people”.

I’m working towards being able to ask alexa/siri where the cats are.

binary_sensor:
  - platform: template
    sensors:
      buddy:
        friendly_name: Buddy
        device_class: presence
        value_template: "{{ is_state_attr('sensor.surepet_connect', 'Buddy', 'Inside') }}"
      richard:
        friendly_name: Richard
        device_class: presence
        value_template: "{{ is_state_attr('sensor.surepet_connect', 'Richard', 'Inside') }}"

This automation will create devices for your cats in known_devices.yaml and update the state of those devices when either the state of the cat attribute changes or home assistant is restarted and as long as track: true is set you can then assign them to people in the UI.

automation:
  - id: '1555327665592'
    alias: Cat Tracker
    trigger:
    - entity_id: binary_sensor.buddy
      platform: state
    - entity_id: binary_sensor.richard
      platform: state
    - event: start
      platform: homeassistant
    action:
    - data_template:
        dev_id: buddy
        location_name: >
          {% if is_state('binary_sensor.buddy', 'on') -%}
            home
          {%- else -%}
            not_home
          {%- endif %}
      service: device_tracker.see
    - data_template:
        dev_id: richard
        location_name: >
          {% if is_state('binary_sensor.richard', 'on') -%}
            home
          {%- else -%}
            not_home
          {%- endif %}
      service: device_tracker.see

I pulled the idea from this post

2 Likes

Thanks! I had to tweak it slightly for my virtual python Ubuntu install:

import sys,os

Hi Guys,

I have a SurePetcare Catflap Connect, which is a really great product.
Currently the official app shows statistic-data (Time Outdoor etc.) only of the last 6 months. So I‘m searching a way to get/export the statistic-data to my computer.
Is there any way to get the Data easy? I‘m not a programmer/developer, so I‘m searching an easy way :slight_smile:

Does anyone have problem with the component updating status? My is really slow. Does a poll like once in a hour our two. So my data in home assistant is not accurate. Iv’re tried to change the poll interval in the python scripts. But it doesn’t seem to help. Anyone else with the same problem?

Mine doesn’t poll very quick either, Im interested if it can poll quicker or if there’s a reason for the slow polling.

Hi to all.

I am a first time HA user so a real HA n00b :wink:

I installed hassio on ubuntu (docker methode) as per gist.github.com/frenck/32b4f74919ca6b95b30c66f85976ec58.

Hass.io (0.93.2) is running and I added some devices (Devolo, Nvidia shield) which HA discovered).

Next up was to install the SureFlap connected cat flap (I won’t bore you with the making a hole bit, etc :wink: ).

As per [xatr0z] posting above:

  1. Went in to my HA directory: cd /usr/share/hassio/homeassistant

  2. ran git clone https://github.com/rcastberg/sure_petcare.git

  3. and got an additional dir ‘sure_petcare’

  4. Next up would be to run sp_cli.py using the credentials used in the iOS/Android app to verify connection to the API with credentials (./sp_cli.py --update -e some-user@someplace,com -p my-password)
    5 But this ended in:

    Traceback (most recent call last):
    File “./sp_cli.py”, line 156, in
    main( sys.argv )
    File “./sp_cli.py”, line 76, in main
    sp.update()
    File “/usr/share/hassio/homeassistant/sure_petcare/sure_petcare/init.py”, line 341, in update
    self.update_pet_info()
    File “/usr/share/hassio/homeassistant/sure_petcare/sure_petcare/init.py”, line 444, in update_pet_info
    } for x in response_pets[‘data’]
    KeyError: ‘data’

A second run makes it even worse :frowning:
Traceback (most recent call last):
File “./sp_cli.py”, line 156, in
main( sys.argv )
File “./sp_cli.py”, line 76, in main
sp.update()
File “/usr/share/hassio/homeassistant/sure_petcare/sure_petcare/init.py”, line 342, in update
self.update_pet_status()
File “/usr/share/hassio/homeassistant/sure_petcare/sure_petcare/init.py”, line 528, in update_pet_status
response = self._get_data(url)
File “/usr/share/hassio/homeassistant/sure_petcare/sure_petcare/init.py”, line 560, in _get_data
‘LastData’: response.json(),
File “/usr/lib/python3/dist-packages/requests/models.py”, line 892, in json
return complexjson.loads(self.text, **kwargs)
File “/usr/lib/python3.6/json/init.py”, line 354, in loads
return _default_decoder.decode(s)
File “/usr/lib/python3.6/json/decoder.py”, line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python3.6/json/decoder.py”, line 357, in raw_decode
raise JSONDecodeError(“Expecting value”, s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Could any of the HA SureFlap guru’s help me out, especially someone who has it running on docker?. It would be nice to get the flap working so we can monitor our cat ‘Snoet’ (Dutch slang for ‘face’ but in a cute way).

Thanks a bunch.

I used the adjustments in this post above to get it working on hassio Sureflap Connect - Pet Door

Thanks for the hint. I did see the posting from Asif.

The one thing lacking is the absolute path or is it as simple as:
mkdir /usr/share/hassio/homeassistant/config/custom_components?

Guess I’ll have to do some trial and error.

Inside the container I access my config from config is at the root of the filesystem but I’m using the raspberry pi image. Just put the directory custom_components in the same directory as your configuration.yaml and from there follow asif’s instructions.

Getting there :wink:

I first made a skeleton dir in /tmp as described by Asif (so /tmp/config/custom_components/sure_flap.
Copied the mentioned files over into it and did the changes as provided by Asif (thanks man).

Changed the secrets.yaml (/usr/share/hassio/homeassistant/secret.yaml) adding the info as mentioned

Copied the skeleton dir to /usr/share/hassio/homeassistant/
(so /usr/share/hassio/homeassistant/config/custom_components/sure_flap) and reboot the vm (restarting hassio from the webGUI didn’t work for some reason/took it’s time)

Browsed to dev-state and checked the entities, no sure_pet sensor.
Tried ./sp_cli.py --update -e some-email -p some-pw, nasty errors :frowning:

Copied the skeleton dir to /usr/share/hassio/
(so /usr/share/hassio/config/custom_components/sure_flap)

Browsed to dev-state and checked the entities, no sure_pet sensor.
Tried ./sp_cli.py --update -e some-email -p some-pw, no return message, mmm
Tried ./hass_test.py,

So, at least the configs are ok and I am able to get info from the API which adds up (Our cat is currently out).

The sensor not showing up in the dev-state might be linked to this error available at the /dev-info page at the HA server:

Guess I have some more trial and error ahead :smile_cat:

PS had to mame an URL, only two allowed as n00b

Just a quick thanks to all the great work that has been put into this. Highly appreciated. Looking forward to installing my sureflap connect and get this going.

Has anyone tried the sureflap dualscan connect with this?

Also, any chance of bypassing the hub?

Thanks to everyone, I just bought a Sureflap-Connect + hub and will try this weekend.
I was wondering a few things before installing it :

  1. how often can I refresh the data?
  2. someone mentioned trying to capture the data from the hub to their server (DNS + MTM) to capture the data directly from the hub instead of querying their server. has this been done ? Could we dialog directly with the hub ?
    thanks

I use it with the cat flap connect rather than the pet door if that’s what you mean?

https://www.surepetcare.com/en-gb/pet-doors/microchip-cat-flap-connect

I don’t think any progress has been made towards talking direct to the flap without the hub.

All working fine until I updated, it seams to have run into issues with 0.94:

  File "/config/custom_components/sure_petflap/__init__.py", line 11, in 
    import utils as utils
ModuleNotFoundError: No module named 'utils'

Any ideas what to check or do please.

I ordered the cat flap dualscan connect (other vendors sold the cat flap connect only), but from what I see the dualscan is implicit on the connect model :slight_smile: