Petsafe Smart Feed 2.0

Yes still working as far as I am aware, although this is not an integration as such but a way to build your sensors / rest_commands. Follow the instructions from @afishe2000 to get the Token (also summarised below):

  1. Get the unique code by posting the below curl (I use https://reqbin.com/curl) - you will get the code on your email:
curl --request POST \
  --url https://users-api.ps-smartfeed.cloud.petsafe.net/users/ \
  --header 'Content-Type: application/json' \
  --data '{ "email": "[email protected]" }'
  1. Use the code to get the “deprecatedToken” posting the below curl:
curl --request POST \
  --url https://users-api.ps-smartfeed.cloud.petsafe.net/users/tokens \
  --header 'Content-Type: application/json' \
  --data '{ "email": "[email protected]", "code": "XXX-XXX" }'
  1. Then finally get the thing_name (which you will need to get the specific device’s information and to post commands). Use the below curl with your deprecatedToken:
curl --request GET \
  --url https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders \
  --header 'Content-Type: application/json' \
  --header 'Token: yourtoken'

Once you done this it’s time to play. Feel free to use anything from my example below (under configuration.yaml)

  • The below allows you to dispense an x amount of food (change the amount number to dispense more or less food. Not sure what is the max amount, haven’t tested it). You can also add “slow_feed” as “True” (to slowly dispense the food), see example: ‘{ “amount”: “1”, “slow_feed”: “True”}’. Note: don’t forget to replace “THING_NAME” on the url with your thing_name and to use your deprecatedToken on the headers:
# Petsafe #
rest_command:
  feed_the_tigers:
    method: post
    verify_ssl: false
    url: "https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders/THING_NAME/meals"
    headers:
      Content-Type: application/json
      Token: !secret petsafe_token
    payload: '{"amount": "1"}' 
  • The below may help you create your sensors, which will help if you want to create automations for low food level for example. I went a bit extreme and created a device to capture all the details, which I could only achieve by creating a vacuum template (may not be your thing but I thought it would be nice to have just one device showing on my dashboard as opposed to all its sensors). Note: don’t forget to replace “THING_NAME” on the url with your thing_name and to use your deprecatedToken on the headers::
rest:
  - authentication: basic
    scan_interval: 310
    resource: https://api.ps-smartfeed.cloud.petsafe.net/api/v2/feeders/THING_NAME/messages?days=1
    headers:
      Content-Type: application/json
      Token: !secret petsafe_token
    sensor:
      - name: "Petsafe Notification"
        value_template: "{{ state_attr('sensor.petsafe_notification', 'message_type') }}"
        json_attributes:
          - "message_type"
          - "created_at"
          - "payload"

Important note: for the above rest command don’t use a lower “scan_interval” as Petsafe only allows pinging their server with a frequency equal or above 5 minutes

vacuum:
  - platform: template
    vacuums:
      pet_feeder:
        friendly_name: Pet feeder
        start:
          - service: rest_command.feed_the_tigers
        value_template: >-
            {% if states('sensor.petsafe_notification').lower() == 'unknown' %}
                error
            {% else %}
                docked
            {% endif %}
        battery_level_template: >-
            {{ (state_attr('sensor.petsafe_notification', 'payload').sensorReading1Infrared*100/29396)|round}}
        attribute_templates:
          notification: "{{ states('sensor.petsafe_notification') }}"
          food_level: "{{ states('sensor.food_level') }}"
          last_feed: "{{ as_timestamp(state_attr('sensor.petsafe_notification', 'created_at'))|timestamp_custom('%I:%M%p %d/%m/%y') }}"
          payload: "{{ state_attr('sensor.petsafe_notification', 'payload') }}"

# Petsafe sensors #
  - sensor:
      - name: "Petsafe last feed"
        state: "{{ as_timestamp(state_attr('sensor.petsafe_notification', 'created_at'))|timestamp_custom('%I:%M%p %d/%m/%y') }}"
  - sensor:
      - name: "Food level"
        state: >
            {% set food_level = state_attr('sensor.petsafe_notification', 'payload') %}
            {% if food_level.isFoodLow == 2 or states('sensor.petsafe_notification') == "FOOD_EMPTY" %}
                Feeder is empty!
            {% elif food_level.isFoodLow == 1 %}
                Feeder is almost empty!
            {% else %}
                Feeder is full
            {% endif %}
  - sensor:
      - name: "Food level %"
        state: >
            {% set food_level = state_attr('sensor.petsafe_notification', 'payload') %}
            {% if food_level.isFoodLow == 2 %}
                10
            {% elif states('sensor.petsafe_notification') == "FOOD_EMPTY" %}
                0
            {% else %}
               {{ (state_attr('sensor.petsafe_notification', 'payload').sensorReading1Infrared | float *100 / 29396) | round(1) }}
            {% endif %}
        unit_of_measurement: "%"

Hope this helps :wink:

Thanks so much for the details response @Zeunas. This seems like it should work for my intended purpose. I mainly want to be able to just monitor in HA that it is feeding the cat as planned for when we are gone for multiple days. It appears this route still maintains all the standard functionality of the original app (unlike some of the other esp based implementations I’ve seen on the Tuya/SmartLife feeders that replaces the factory firmware entirely, which I’d like to avoid for reliability sake.)

One last question, how are these devices about keeping time? I would assume with WiFi they are doing some form of time correction every now and then? My biggest annoyance of other non-connected auto feeders is really significant time drift. I really just want to make sure kitty gets fed routinely and reliably when on vacation and have HA notify me if it cannot confirm a feeding happened within the last X hours.

Exactly! You can still use the Petsafe app as normal. Assume you don’t have the feeder yet but you can probably install the app and create an account to test the points 1 - 3 to get your token.

Not sure if I understand your question, I mean once you set up your feeding schedule on the app (or even home assistant via automations), the feeding timing is pretty reliable. Although with this set up on home assistant you may have a delay up to 5 minutes from the point the food is dispensed to get the updated “Last feed” information, due to the “scan_interval” limitation I mentioned above. There’s another annoying thing which is the Winter time vs Summer time, the API doesn’t seem to update to Summer time, so at the mo it’s telling me the last feed was an hour earlier than scheduled, which it wasn’t - You may be able to fix it with a template. I just haven’t thought much about it because it doesn’t really affect the purpose of these sensors, as long as I know my cats have been fed in the last couple of hours. Hope this answers your question.

Ah I see. So if I understand correctly, while the feeder is still feeding at the right time, the reporting is incorrect due to sensor not accounting for DST. That’s not a huge concern, and as you noted maybe fixable with a template sensor. I’m less concerned with having HA aware of a feeding within minutes, or even hours, and moreso being able to raise an alert if it’s been half a day or a day and HA can’t confirm kitty was fed.

My issue with the feeder I have currently is quite literally over a period of weeks or months the internal clock will drift so much so that the feedings will eventually become an hour or more later from the intended (actual) time because of the ever increasingly incorrect device clock. I would think connected devices could easily correct this via NTP or some other solution within the software.

In that case I can confirm that you don’t need to worry about time drifting with the petsafe feeder at all. The only “concerns” you will have is to top it up when needed, although the feeder holds almost 6 L of food, and power cuts (which you can prevent with the backup battery option it has).

Awesome. Thanks so much for your help. Ordering one today.

1 Like

Thanks again so much @Zeunas .

I got the feeder setup and working in HA using your first two code blocks (rest_command and rest). I tried adding the third for the sensors and it gave me an error ( Invalid config for [vacuum]: required key not provided @ data[‘platform’]. Got None.). I think because this needs to be below an existing template sensor section in the code, still playing around with it.

I noticed it shows battery percentage. This has showed 25% for me before and after installing batteries in the device. Does the battery level server any purpose? Food level is still showing unknown for me although the app now shows it is no longer low. And to your point about the time info on the sensor, mine showed 11:00pm after a 6:00pm feeding (which tells me it’s likely based on UTC time since I am UTC-5). Like you said, I think that could be handled via a template maybe. Feeding definitely works though and it is receiving the notifications from PetSafe cloud. I probaly just need to mess around with the code a bit more and get it just how I wawnt but this is really close. Thanks again!

Good one! Glad it´s working for you!

Yes, for this you will need the below sensor (forgot to mention when I 1st shared my config that you need to add the “template:” line before the sensor but if your vacuum attributes are working I assume you worked that out :sweat_smile:). Feel free to share your config I’ll try my best to help, although I’m not an expert a lot is trial an error and you may find that you can simplify my config further

template:
  - sensor:
      - name: "Food level"
        state: >
            {% set food_level = state_attr('sensor.petsafe_notification', 'payload') %}
            {% if food_level.isFoodLow == 2 or states('sensor.petsafe_notification') == "FOOD_EMPTY" %}
                Feeder is empty!
            {% elif food_level.isFoodLow == 1 %}
                Feeder is almost empty!
            {% else %}
                Feeder is full
            {% endif %}

Ah yes! There´s quite a big chunk of information I have not shared on my summary, you may want to have a read through the initial post from @afishe2000 (here) where he shows the details you can get from the rest sensor and depending on the level of information you want on your rest sensor you may want to add a few more “json_attributes”. For the purpose of having battery percentage you will need to add “battery_voltage” to json_attributes and then create a sensor such as the one shared by @j6s33m (here). I should also mention that I don’t have batteries on my feeder and the battery % you see it’s the actual food level (my bad for not telling you earlier), so once you create your battery sensor you just need to update the name on the sensor under the vacuum template. If you still want to keep a food level % sensor you can add it to the attributes of the vacuum template instead.


Try adding " - timedelta(hours = 5)" to the last_feed attribute template of the vacuum, like below (you may find a better template to take into account DST, do share if you find it :wink: ) :

last_feed: "{{ (as_timestamp(state_attr('sensor.petsafe_notification', 'created_at')) - timedelta(days = 1))|timestamp_custom('%I:%M%p %d/%m/%y') }}"```

Okay this helped a lot actually. I still banging my head on the time/timezone issue. The timedelta thing doesn’t work as intended yet. I think I can figure this out eventually, just gonna take a lot of trial and error for me. It seems like time manipulation in HA is entire rabbit hole all on it’s own lol. Thanks again for all your help though, I learned quite a bit through your examples in regards to rest in HA and manipulating data into sensors, so thanks for that! I will let you know once I finally figure it out.

1 Like

That’s the beauty of HA for me and finding the solution is the best reward (in addition to be able to help others when they get stuck). Good luck!

Hiya, just revisiting this old post and was wondering how did you manage to use Pyscript add-on back then? It seems that the Petsafe Smartfeed repo uses a new Petsafe API, which for now has only affected my ability to send a command to drop some food but I have a feeling that the deprecatedToken old API will eventually stop working…

I still have work to do, but I’m in the process of building a HACS integration for both the feeders and litter boxes. Never build an HA integration before so I’m sure it’s not perfect and probably has bugs, but I’m learning! I should have it in HACS within a week or so

Thought about doing too but not savvy enough and I only have the feeder. Let me know once you release, happy to be a tested. :wink:

1 Like

It’s not quite perfect and I’m sure there are some bugs but it’s working for me so if you want to give it a try, GitHub - dcmeglio/homeassistant-petsafe

1 Like

Simple and easy to set up! Working fine with my feeder, thanks! Is this based on the new API they’ve released a while ago? The one they don’t use the deprecated token.

Perhaps you could include a food level % based on the two infrared sensors the feeder has.

What does the Feeding Paused switch do? Wasn’t aware of it.

Going to keep an eye on it and will let you know is something goes wrong or if I have any suggestions. Great work so far!

1 Like

Nevermind, just checked and I can see it’s based on the new API. Super!

It will report if the food is full/low/empty with the Food Level entity. I’m not totally sure how to interpret the values that come back from the sensors so I’m not sure how to make it more accurate than that.

It lets you temporarily pause your feeding schedules

1 Like

There are two infrareds, infrared 1 gives you the top level of the container, the lower the infrared reading the lower the food and I assume it’s to measure the levels ‘full’ and ‘low’, whereas the infrared 2 will give you the levels ‘low’ and ‘empty’. Although, come to think of it I did build a sensor to give me the % but to be honest I think the sensor you created is more than enough, the infrared readings are not that precise anyways.

Ah! Didn’t know about that, thanks.

Just have one last suggestion for easier integration with google assistance for eg. (took this idea from the Litter-Robot integration) which is to create one device, under the domain ‘vacuum’, with all the information from the sensors including the button to feed, my example below:

vacuum:
  - platform: template
    vacuums:
      pet_feeder:
        friendly_name: Pet feeder
        unique_id: pet_feeder
        start:
          - service: rest_command.feed_the_tigers
        value_template: >-
            {% if states('sensor.petsafe_notification').lower() == 'unknown' %}
                error
            {% else %}
                docked
            {% endif %}
        battery_level_template: "{{ states('sensor.feeder_battery') }}"
        attribute_templates:
          notification: "{{ states('sensor.petsafe_notification') }}"
          food_level: "{{ states('sensor.food_level') }}"
          food_level_%: "{{ states('sensor.food_level_2') }}"
          last_feed: "{{ as_timestamp(state_attr('sensor.petsafe_notification', 'created_at'))|timestamp_custom('%I:%M%p %d/%m/%y') }}"
          battery_installed: >-
            {% if is_state_attr('sensor.petsafe_battery', 'is_batteries_installed', false) %}
                Not intalled
            {% elif is_state_attr('sensor.petsafe_battery', 'is_batteries_installed', true) %}
                Installed
            {% endif %}
          payload: "{{ state_attr('sensor.petsafe_notification', 'payload') }}"

And btw, please share your “Buy Me a Coffee” link when you can :wink:

Just installed this via HACS. Setup was a breeze. I did only have 2 entities for the first few minutes (child lock, and feeding pause) but shortly after the remaining entities populated (feed button, battery level, food level, last feeding, and signal strength). I am using the SmartFeed 2.0 (Amazon.com). Excellent work @dman2306. Thank you very much for your efforts on this.

1 Like

Let me think about the vacuum. Something seems “wrong” about making a pet feeder a vacuum, but interesting to see that’s how Litter Robot works so I’ll take a look at that. No donation necessary, but my link is Buy Me a Coffee :slight_smile:

2 Likes