vasqued2 / ha-teamtracker

Home Assistant integration that provides real-time scores in multiple professional (NBA, NFL, NHL, MLB, MLS, and more), college (NCAA), and international (soccer, golf, tennis, mma, racing) sports using ESPN APIs.
174 stars 20 forks source link

Can I notify if there is a game today? #93

Closed JeffCrum1 closed 1 year ago

JeffCrum1 commented 1 year ago

My wife missed a day game because ... well, she wasn't paying attention.

Of course, it was my fault :)

Is there a way to notify at 10am if her team has a game today and what time?

I know how to notify. But, need to know how to do the 'game today thing'.

vasqued2 commented 1 year ago

You should be able to set up an action and trigger it off of the date attribute.

RentreX commented 1 year ago

@JeffCrum1 I was thinking the same thing.

I created an automation that triggers when it's 30 minutes prior to start (as per the attribute kickoff in)

I haven't been able to see if it triggers at 30 minutes prior to start. Though just manually running it works so I'm hopeful.

alias: NHL
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.{your_sensor}
    attribute: kickoff_in
    to: in 30 minutes
condition: []
action:
  - service: notify.{your_device}
    data:
      title: >-
        {{state_attr('sensor.{your_sensor}', 'team_name')}} vs
        {{state_attr('sensor.{your_sensor}', 'opponent_name')}}
      message: Puck drops in 30 minutes
      data:
        image: https://www.freepnglogos.com/uploads/nhl-logo-png/image-logo-nhl-1.png
mode: single

Screenshot_20230512_234256_Chrome Screenshot_20230512_234250_Chrome

Change sensor.{your_sensor} to what your sensor is called eg. sensor.nhl

Also change notify.{your_device} to what your device is called eg. notify.iphone

I didn't add any conditions to my automations though I have set one for each sport (even works with sports that are not natively supported like Australian NRL)

The above example is based on team sports/2 people (UFC). So for golf or motorsports it would have to be adjusted a bit, but I haven't got around to adding them yet.

JeffCrum1 commented 1 year ago

Beautiful! Thanks so much for this.

RentreX commented 1 year ago

Not a problem at all.

If you need any help, let me know.

JeffCrum1 commented 1 year ago

Not a problem at all.

If you need any help, let me know.

So, the trigger didn't work. This is because the update interval is 10 minutes until before the game starts. Because of my computer time and the game start time, once it went under an hour, it was at 57 minutes. So, it never hit 30 minutes. It went from 37 to 27.

I changed: to: in 30 minutes

to

from: in an hour

I'll know later today how it works and report back.

Also, I am using "{{state_attr('sensor.{your_sensor}', 'team_logo')}}" for the image.

Again, thanks for the initial start.

I think I'll use multiple triggers in one and use the trigger.{stuff} to set the notification. Once I get that built, I'll report back here to.

ETA: My wife just told me that she did get a message at the 30 minute mark yesterday. I didn't think she did. So, HA/Teamtracker must be tracking it as opposed to just capturing the time every 10 minutes. Even though it doesn't display on the Teamtracker Card. So, 'to: in 30 minutes' does seem to work.

JeffCrum1 commented 1 year ago

Here is what I am using for the notification now and using multiple triggers on just one automation.


service: notify.{your_device}
data:
  title: >-
    {{state_attr(trigger.entity_id, 'team_name')}} vs 
    {{state_attr(trigger.entity_id, 'opponent_name')}} @<br>
    {{state_attr(trigger.entity_id, 'venue')}}
  message: Game starts {{state_attr(trigger.entity_id, 'kickoff_in')}}
  data:
    image: "{{state_attr(trigger.entity_id, 'team_logo')}}"
vasqued2 commented 1 year ago

The sensor updates every 10 minutes until it gets within 20 minutes of the start time. Then is starts updating every 5 seconds until the event is over, at which time it goes back to a 10 minute interval.

If the sensor happens to refresh 21 minute before game time, it will another 10 minutes before refreshing again, which will be at 11 minutes before the game and then refresh every 5 seconds. If the sensor happens to refresh 20 minutes before game time, it will start refreshing every 5 seconds then. The bottom line is you can't be guaranteed a minute-by-minute countdown until about 10 minutes before game time.

You have to trigger for 39, 38, 37, etc. Or you test for just the 3 in the first digit's position and make sure it ends in "minutes". Or you could do something w/ the delta between the start time in the date attribute and the current time. Depends what you want to do.

JeffCrum1 commented 1 year ago

Got it. Thanks for that. As said above, I went to from: in an hour for the change.

Here is the complete automation. I put an else on my start_term variable in case I add another sport and forget to add it to the if. Just change {team 1} and {team 2} to your teams. And, add as many team triggers as you want. Also, change {Wife's phone} to your device. Add more elif to the start_term for other sports.

Thanks to @vasqued2 and @RentreX for all of the help!

alias: Notify sports starting
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.{team 1}
    attribute: kickoff_in
    from: in an hour
  - platform: state
    entity_id:
      - sensor.{team 2}
    attribute: kickoff_in
    from: in an hour
condition: []
action:
  - service: notify.{Wife's phone}
    data:
      title: >-
        {{trigger.to_state.attributes.team_name}} vs 
        {{trigger.to_state.attributes.opponent_name}} @<br>
        {{trigger.to_state.attributes.venue}}
      message: "{{start_term}} {{trigger.to_state.attributes.kickoff_in}}"
      data:
        image: "{{trigger.to_state.attributes.team_logo}}"
mode: single
variables:
  start_term: >-
    {% if trigger.to_state.attributes.sport == 'baseball' %} First Pitch {% elif
    trigger.to_state.attributes.sport == 'hockey' %} Puck Drops {% elif
    trigger.to_state.attributes.sport == 'football' %} Kickoff {% else %} Game
    starts {% endif %}