TryFi integration with helpers, scripts, and automations

This article is being written on August 29, 2022 using Home Assistant 2022.8.7, HACS 1.27.1, and TryFi 0.0.17. 

First and foremost: TryFi integration devs - thanks for your hard work!

This is also not strictly a "home" automation, although all of the data and actions are being handled on Home Assistant. This home project is tied to my dog's Fi collar - I am not receiving payment of any kind from Barking Labs/TryFi nor am I claiming any kind of license/trademark over anything mentioned here.

There are two projects - a basic notification automation if Astrid hasn't met her step goals for the day by a certain time, and a more complex automation mimicking a "Safe Zone" in TryFi terms, or something similar to a "Zone" in Home Assistant.

What the Projects Need

  • Nabu Casa, or some method to reach your Home Assistant instance remotely.
  • HACS (to install TryFi)
  • TryFi integration (through HACS)
  • Your Fi login & password, and an active Fi collar subscription

The TryFi integration was quick to set up through HACS. Once it syncs to your account, you have the basics - distance and step counts for the day/week/month. It will also give you access to the light, battery level, "lost" mode", and the "device_tracker.astrid_tracker". This last one contains latitude and longitude information as attributes. (Strangely enough, battery level is also an attribute.)

Goal for Project 1 - The Step Goal Automation

Use the Step Count from the Fi Collar to trigger an automation that pings our phones at 9pm if Astrid hasn't gotten her steps for the day.

The user story:  We want Astrid to be a healthy dog who gets her exercise in every day. I want to customize what information I get and when.

Setting Up Helpers for the Step Count

Helpers are a way to store small amounts of information (aka variables) that can be used as an entity throughout Home Assistant. You can hard-code input booleans instead of creating it as a helper - this was a proof-of-concept worked well. For this part of the project, we will store Astrid's daily step goal (10,000 steps), as well as an early deadline (9PM) to warn me that she might not make her steps that day.

Settings>Devices & Services>Helpers>ADD HELPER>Number

Astrid Step Goal was set as a Number helper, with a Step size of 10000. Changed the maximum value to 20000, but that doesn't matter.

Settings>Devices & Services>Helpers>ADD HELPER>Date and/or time

Astrid Time Check for Step Goal was set as a Date And/Or Time helper. The time was set to 9:00 PM.

 

If these helpers need to be adjusted later, using Developer Tools>States allows for changes. (Time changes to a 24-hour notation.)

Creating a Message Automation

Settings>Automations & Scenes>ADD AUTOMATION

This is a basic automation. A trigger, a condition, and an action. The Trigger is based on time, and we use the Astrid Time Check for Step Goal helper for the time.

platform: time
at: input_datetime.astrid_goal_check

No message is needed if Astrid has met her step goal, so that comes in as a numeric state Condition. The "sensor.astrid_daily_steps" comes from the TryFi integration. The "input_number.astrid_step_goal" is the helper that was set for 10000.

condition: numeric_state
entity_id: sensor.astrid_daily_steps
below: input_number.astrid_step_goal

The Action is a straightforward Call Service, where the Service is "Notifications: Send a notification with notify".

service: notify.notify
data:
  message: Astrid needs her steps
  title: Walking Time!

To troubleshoot this project, consider replacing the helper names with the actual numbers. It would be clever if the message included Astrid's actual steps for the day, to know how much walking might be necessary.

Goal for Project 2 - The Lock

  1. Use the HACS TryFi integration to track my dog's current latitude/longitude
  2. Press a button in Lovelace to "lock in" those coordinates to helpers through a script
  3. Use an automation to receive an alert on our phones if the Fi collar's current location leave the helper's coordinates

The user story: We run a 5-minute errand and leave our dog in the car and we lower the windows. But what would happen if someone used that gap to get into the car and steal our MVP - most valuable puppy? She can be tracked by her Fi collar, but it would be great to know as quickly as possible.

The Fi software app at this moment could solve it, if we wanted to set up a safe zone every time we park. Set it up, take it down...it's work. Likewise, zones in Home Assistant are hard-coded, which is not ideal when out and about.

Test Your Device Tracker Attributes

The script will need the latitude and longitude attributes from the device_tracker.

Using Developer Tools>States, I searched using device_tracker, and noticed a list of attributes, which included "latitude" and "longitude".

Using Developer Tools>Template, the template editor can be used to test the exact format for the entity's attribute.

In this case, it was

{{ state_attr('device_tracker.astrid_tracker', 'latitude') }}

The result type: number was the latitude listed in the Developer Tool>States that I had looked at.

I used "gps accuracy" to preserve Astrid's privacy

Creating Helpers

In this case, we want to store two numbers - the latitude and longitude when I press the "lock" button. These will be used to compare Astrid's current location against the car's location.

Settings>Devices & Services>Helpers>Add helper>Number

Astrid Longitude Lock

Astrid Latitude Lock

 

While creating helpers, create a toggle for an input boolean that will be wired to a Lovelace button panel later.

Settings>Devices & Services>Helpers>Add helper>Toggle

Astrid Location Lock 

 

After finishing the helpers and saving them, go to Developer Tools>YAML>Reload INPUT NUMBERS - repeat for INPUT BOOLEANS

Writing the Lock Script

For this portion, we write a script, assigning current GPS location from the collar to the helpers we just made.

First, we'll add a new script: Settings>Automation & Scenes>Scripts>Add Script

Currently, Home Assistant doesn't like templates in the visual editor, so it will automatically switch you over to the YAML editor. You'll need information from the previous two steps - the device tracker's latitude/longitude attribute template and the input_number helpers.

The call service is to input a number. The number data comes from the Fi collar at the moment the script runs. The target are the helpers. The value in the helpers stick around until this script runs again.

service: input_number.set_value
data:
  value: '{{ state_attr(''device_tracker.astrid_tracker'', ''latitude'') }}'
target:
  entity_id: input_number.astrid_latitude_lock

Home Assistant doesn't always appreciate templates

After finishing the script and saving it, go to Developer Tools>YAML>Reload SCRIPTS.

Create the Lovelace button

This will be the visible part of the entire process - a button.

The first step is going to Overview, then selecting the dashboard this button show up in. Click the three vertical dots in the upper left corner, and edit dashboard. Next, add card, choose "Button", select the Entity - which is the name of the input boolean from before. In this case - "Astrid Location Lock" aka input_boolean_astrid_location_lock.

At this point, I also added conditional entity panels for the lock's latitude and longitude entity panels. (The condition is "Astrid Location Lock = on".) I renamed the panels with the "Name" field, and added the icon of mdi:dog for extra flavor. I set the icon height to 100 to the Astrid Lock button look nicer in the vertical stack card.

Longitude & Latitude are zeros for Astrid's privacy
 

The other two buttons on the left are information from the Daily Steps entity and the Collar light entity - these are both default information from the TryFi integration. Astrid's "maximum" is set to her normal 10000 steps. (The Step Goal helper would have worked great here - but there's a graphical error on the gauge when a number helper is used instead of a number.)

Make sure things are saved, then click the DONE button in the upper left.

Write the Lock automation

Settings>Automations & Scenes>Automations>Create Automation

This first automation is the simple one. The trigger is a "state" trigger, watching the button/input boolean entity. If it goes from "off" to "on", run the action "Call service" script that locks the longitude and latitude to the helpers.

Trigger:

platform: state
entity_id:
  - input_boolean.astrid_fi_lock_toggle
from: 'off'
to: 'on'

Action:

service: script.assign_current_astrid_coordinate_to_coordinate_lock_helpers
data: {}

Save the automation.

Write the "Leaving Lock" automation

This is the final part and the reason for all of the earlier work. The triggers compare the device tracker's current latitude and longitude attributes against the latitude and longitude helpers. The automation condition requires the lock to be on, and the action is to notify all users (my partner and I).

The visual editor works fine for this, but I included the YAML below.

Trigger:

platform: template
value_template: >-
  {{ states('input_number.astrid_latitude_lock')|float|round(2) !=
  state_attr('device_tracker.astrid_tracker', 'latitude')|float|round(2) }}

platform: template
value_template: >
  {{ states('input_number.astrid_longitude_lock')|float|round(3) !=
  state_attr('device_tracker.astrid_tracker', 'longitude')|float|round(3) }}

Condition:

condition: state
entity_id: input_boolean.astrid_location_lock
state: 'on'

Action:

service: notify.notify
data:
  message: Astrid has left her lock coordinates.
  title: Astrid is loose!

Save the automation, and go to Developer Tools>YAML>Reload AUTOMATIONS

That's all!

Other Things Considered

GPS drift could trigger an "Astrid is loose" scenario. Currently, Home Assistant can set Zones - but they are hard-coded within Home Assistant. In the future, maybe an automation will be able to create a Zone. I spent considerable time trying to account for GPS drift in the final automation - creating templates that would allow +/- 30 feet in the above/below for latitude and longitude. (Resulting in rounding to three decimal places.)

Conditional cards for Astrid's current GPS location - I don't think I care where she is, when the lock isn't on. 

 



Comments

Popular posts from this blog

Home Assistant Controlling Octoprint through MQTT - Chapter 1

Setting up MQTT on HA and Octoprint

Home Assistant Controlling Octoprint through MQTT - Chapter 2