Skipperro / pajgps-homeassistant

This integration allows you to use PAJ GPS devices (www.paj-gps.de) in Home Assistant.
MIT License
6 stars 0 forks source link

Support for Alarms #7

Open rsporsche opened 1 month ago

rsporsche commented 1 month ago

I want to integrate my GPS tracker with home assistant so that I can broadcast a notification via my smart speaker when an alarm is triggered by the tracker.

I would be happy to have a go at this myself but just wanted to raise the issue first in case there is any work in progress or for discussion first.

On first look, I am thinking this could be implemented by creating an entity that polls the /api/v1/notifications/{device} endpoint and raising an event when it returns new notifications.

Skipperro commented 1 month ago

Hi @rsporsche Yes, notifications and alarms are on my ToDo list, I just have to find some time to actually implement it. API supports it but there are just few things to consider if you want to do it properly.

  1. Decide how to track which alerts was already viewed in Home Assistant so things like restarts won't trigger same alert again.
  2. Decide if Home Assistant should mark notifications as read in API, so they will not show in finder portal or mobile app from PAJ GPS - ideally letting user decide during configuration.
  3. Do we want history of notifications in HA? If yes I thought to have HA sensor for each type of alert, like SOS and have it as boolean, so I can then see state change every time SOS button was pressed and make automation that would notify my (for example via smart speaker) if SOS turns on.

If you will need any help, let me know, but if you don't feel confident you can wait for me, I will be adding this feature at some point, hopefully in the next 2-3 months. I had already few requests for it and I need it for myself too :smile:

rsporsche commented 1 month ago

Thanks,

Sorry I just noticed that notifications is listed in the planned features, not sure how I missed that because I did read it.

I will probably wait if it's likely to happen in the next couple of months. I'd still be happy to contribute but I don't know if that's actually helpful or just creates more work.

My thoughts on your points:

  1. If the 'id' and/or 'dateunix' properties of the notifications can be relied on to be monotonic then perhaps it would be enough to record one of those and just filter anything older. Otherwise I don't see any alternative to the built in 'read/unread' concept which relates to point 2.
  2. If it's configurable it doesn't really matter but personally I would prefer not to mark the notifications as 'read' since I will still use the app. Mostly I just want a loud alarm to wake me up if my car moves unexpectedly in the night but when outside of the home I would still rely on the app.
  3. This wouldn't be important for me. I assumed that events would be written to the logbook but it doesn't seem so. Can a sensor entity write to the logbook? Since we're really talking about events and not state I think it would be awkward to represent it as a state. If it was implemented as a sensor and notifications are sent via automation triggered on state change, would that be reliable if the state is only momentarily in the 'on' state?
Skipperro commented 1 month ago

2. Mostly I just want a loud alarm to wake me up if my car moves unexpectedly in the night but when outside of the home I would still rely on the app.

You can pretty much do it already, without relying on notifications from PAJ GPS API. I'm doing something similar for getting notifications when my wife leaves her job and drives home.

You just have to define home area in your home assistant (with about 100m buffer) and then you can make automation like this:

image

alias: Car Alarm
description: Notify that car moved at night
trigger:
  - platform: zone
    entity_id: device_tracker.paj_gps_1234567
    zone: zone.home
    event: leave
condition:
  - condition: time
    after: "22:00:00"
    before: "06:00:00"
    weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
action:
  - service: notify.persistent_notification
    metadata: {}
    data:
      message: Car is moving at night!
      title: THIEF!
mode: single

This will trigger an action when your car leaves home area at night, between 22:00 and 6:00 Replace my notification with something that blasts alarm on your speaker and you are done. :smile:

rsporsche commented 1 month ago

Thanks, I'll give that a go. I think I would still like the other alarm types but this is a great start.