High food drive dogs are always hungry, they’ll do anything to get a refill, even lie to your face.
Voldo is one of those, “did you feed the dog?” is a common question around the house and more often that we would like to admit he manages to double-dip.
I’ve been wanting to find a way to automatically track how many times we feed him daily and finally I had an idea. I got a couple of left over Zigbee door/window sensors so I attached one to the big pink container we use to store food.
Step by step config
Counter helper
This can be now done through the GUI. Go to Settings > Devices & Services > Helpers and click the Create helper button.
Pick Counter and give it a meaningful name. In my case I used “Voldo food count” which will create an entity named counter.voldo_food_count
. Leave the initial value at 0 and step size at 1.
Automation
Let’s go step by step. First we want to create an automation that will trigger when the sensor changes from closed to open.
We won’t set any conditions and put the mode as single because we don’t need this to run more than once at the same time.
alias: "Voldo: Food counter"
description: ""
mode: single
trigger:
- platform: state
entity_id:
- binary_sensor.voldo_food_contact
from: "off"
to: "on"
condition: []
Now we need an if action that will reset the counter to zero if this is the first time the automation triggers for today. This can be done by comparing the last_changed
value fro the counter to the current date.
We do this in a condition template, and negate it because we only want to run the action if the dates are different.
action:
- if:
- condition: template
value_template: "{{ not states.counter.voldo_food_count.last_changed.day==now().day }}"
then:
- service: counter.reset
data: {}
target:
entity_id: counter.voldo_food_count
We feed Voldo three times a day, so we want to get a Telegram notification when anyone opens the container and the count is already at three. I have a notify service with a Telegram bot already set up, so it’s as easy as calling it with a set message.
- if:
- condition: state
entity_id: counter.voldo_food_count
state: "3"
then:
- service: notify.telegram_bot
data:
message: 🐶 Voldo has already eatin three times today. Don't let him trick you!
The last action runs every time, it will just increase the counter by 1.
- service: counter.increment
data: {}
target:
entity_id: counter.voldo_food_count
Extra
As of now everything works and we could mark this as done, but we don’t have a good way to check how many feedings we have and I don’t want to add a big number into a dashboard.
I’m already using badges, so what if I just added a new one for Voldo? This is what it looks like:
To do this I just created a new person in Settings > People. I didn’t put any tracking devices in, but I wanted the number to show in the same place we have the location for me and Lu.
This turned out to be a bit tricky, since there’s no way to directly set an entity state natively in Home Assistant. After going through the forums I found a Python script that does just this.
Just copy that code into python_scripts/set_state.py
inside your Home Assistant configuration folder and enable the integration needed to run these as services by adding python_script:
to your configuration.yaml
.
Now you can add a last action to the automation that will sync the current count to the new person entity, like this:
- service: python_script.set_state
data:
entity_id: person.voldo
state: "{{ states.counter.voldo_food_count.state }}"