Powerball Card / Winning Notification

Hi all,
Thought I would share my Powerball integration.
My wife and I live in a state that does not offer the lottery, but our neighboring state does. So we typically will make a day trip to get Powerball tickets and get them for a running 10 draws or so. This setup allows me to input the number set(s) from our ticket and get notifications if we win. I also have a card that lets me see information about the current/upcoming draw.

This is done using Powerball’s undocumented API. I had to find a custom component that could parse the JSON response properly. I came across the jsonrest.py script from mad-ady and created a repo just for the jsonrest including the manifest and init files.

Then the yaml setup for the sensors is:

input_datetime:
  my_powerball_end_date:
    name: My Powerball End Date
    has_date: true
    has_time: false

input_text:
  my_powerball_numbers:
    name: My Powerball Numbers

sensor:
  - platform: jsonrest
    name: Powerball Numbers
    resource: https://powerball.com/api/v1/numbers/powerball/recent?_format=json
    scan_interval: 300
  - platform: jsonrest
    name: Powerball Next Draw
    resource: https://powerball.com/api/v1/estimates/powerball?_format=json
    scan_interval: 300
  - platform: template
    sensors:
      powerball_winning_numbers:
        friendly_name: 'Winning Numbers'
        value_template: '{{ states.sensor.powerball_numbers.attributes.list[0].field_winning_numbers }}'
      powerball_multiplier:
        friendly_name: 'Multiplier'
        value_template: '{{ states.sensor.powerball_numbers.attributes.list[0].field_multiplier }}'
      powerball_draw_date:
        friendly_name: 'Drawing Date'
        value_template: '{{ as_timestamp(strptime(states.sensor.powerball_numbers.attributes.list[0].field_draw_date+" 00:00:00.0", "%Y-%m-%d,%H:%M:%S-%z")) | timestamp_custom("%m/%d/%y") }}'
      powerball_next_draw_days:
        friendly_name: 'Days To Next Drawing'
        value_template: '{{ (((as_timestamp(states.sensor.powerball_next_draw_date.state) - as_timestamp(states.sensor.date_time_iso.state)) / 3600 / 24)) | int }}'
      powerball_next_draw_date:
        friendly_name: 'Next Drawing Date'
        value_template: '{{ states.sensor.powerball_next_draw.attributes.list[0].field_next_draw_date }}'
      powerball_next_prize:
        friendly_name: 'Next Prize Amount'
        value_template: '{{ states.sensor.powerball_next_draw.attributes.list[0].field_prize_amount }}'
      powerball_next_prize_cash:
        friendly_name: 'Next Prize Cash'
        value_template: '{{ states.sensor.powerball_next_draw.attributes.list[0].field_prize_amount_cash }}'
      powerball_win:
        friendly_name: 'Powerball Win'
        value_template: >
          {% set list1 = states.sensor.powerball_draw_date.state.split('/') %}
          {% set vardate = '20' + list1[2] + '-' + list1[0] + '-' + list1[1] %}
          {% if as_timestamp(states.input_datetime.my_powerball_end_date.state) >= as_timestamp(vardate) -%}
            {% set winnum = namespace(value=0) %}
            {% set powerball = namespace(value=0) %}
            {% set totalwin = namespace(value=0) %}
            {% for numset in states.input_text.my_powerball_numbers.state.split(';') -%}
              {% for mynum in numset.split(',')[0:5] -%}
                {% for num in states.sensor.powerball_winning_numbers.state.split(',')[0:5] -%}
                  {%- if mynum == num %}{% set winnum.value = winnum.value + 1 %}{% endif -%}
                {%- endfor %}
              {%- endfor %}
              {%- if numset.split(',')[5] == states.sensor.powerball_winning_numbers.state.split(',')[5] %}
                {% set powerball.value = 1 %}
              {% endif -%}
              {%- if winnum.value < 2 and powerball.value == 1 %}{% set totalwin.value = totalwin.value + 4 %}
              {% elif winnum.value == 2 and powerball.value == 1 %}{% set totalwin.value = totalwin.value + 7 %}
              {% elif winnum.value == 3 and powerball.value == 0 %}{% set totalwin.value = totalwin.value + 7 %}
              {% elif winnum.value == 3 and powerball.value == 1 %}{% set totalwin.value = totalwin.value + 100 %}
              {% elif winnum.value == 4 and powerball.value == 0 %}{% set totalwin.value = totalwin.value + 100 %}
              {% elif winnum.value == 4 and powerball.value == 1 %}{% set totalwin.value = totalwin.value + 50000 %}
              {% elif winnum.value == 5 and powerball.value == 0 %}{% set totalwin.value = totalwin.value + 1000000 %}
              {% elif winnum.value == 5 and powerball.value == 1 %}{% set totalwin.value = totalwin.value + 40000000 %}
              {% else %}{% set totalwin.value = totalwin.value + 0 %}
              {% endif -%}
              {% set winnum.value = 0 %}
              {% set powerball.value = 0 %}
            {%- endfor %}
            {{ "$%.2f"|format(totalwin.value) }}
          {%- else -%}
            Not Playing
          {%- endif %}
      powerball_did_i_win:
        friendly_name: 'Powerball Did I Win'
        value_template: '{% if states("sensor.powerball_win") != "$0.00" and states("sensor.powerball_win") != "Not Playing" %}true{% else %}false{% endif %}'

Then my automation:

- id: '1577941963380'
  alias: '[Notify] Powerball Win'
  description: ''
  trigger:
  - entity_id: sensor.powerball_draw_date
    platform: state
  condition:
  - condition: state
    entity_id: sensor.powerball_did_i_win
    state: 'true'
  action:
  - data:
      message: '{{ states("sensor.powerball_win") }}'
      notification_id: 1234
      title: Powerball Win
    service: persistent_notification.create
  - data:
      message: '{{ states("sensor.powerball_win") }}'
      title: Powerball Win
    service: notify.mobile_app_tj_iphone
  - data:
      message: '{{ states("sensor.powerball_win") }}'
      title: Powerball Win
    service: notify.mobile_app_iphone

The card requries the config-template-card plugin. Then the card yaml is:

card:
  entities:
    - entity: sensor.powerball_winning_numbers
      icon: 'mdi:numeric-7-circle'
      name: ''
    - entity: sensor.powerball_next_prize
      icon: 'mdi:ticket'
      name: ''
    - entity: sensor.powerball_win
      icon: 'mdi:cash'
      name: ''
  show_header_toggle: false
  style:
    .: |
      .card-header {
        padding: 16px;
      }
      #states div {
        margin: 0px
      }
  title: '${"Last: " + vars[0] + " | Next: " + vars[1]}'
  type: 'custom:hui-entities-card'
entities:
  - sensor.powerball_winning_numbers
  - sensor.powerball_next_prize
  - sensor.powerball_win
type: 'custom:config-template-card'
variables:
  - 'states[''sensor.powerball_draw_date''].state'
  - 'states[''sensor.powerball_next_draw_days''].state'

image
image

For multiple number sets they just need to be separated by a semi-colon.
I would like to make a custom card for this, but my javascript skills are fairly limited! :man_shrugging:
Hopefully this will be useful to my fellow gamblers. :laughing:

5 Likes

Here is my sensor for Mega Millions:

# /config/rest.yaml

 - scan_interval: 900
   resource: https://www.megamillions.com/cmspages/utilservice.asmx/GetLatestDrawData
   sensor:
    - name: Mega Millions Next Draw Date
      state_class: measurement
      value_template: "{{ (value_json['string']['#text'] | from_json )['NextDrawingDate'] }}"
    - name: Mega Millions Prize Amount
      state_class: measurement
      value_template: "{{ (value_json['string']['#text'] | from_json )['Jackpot']['NextPrizePool'] }}"
    - name: Mega Millions Prize Amount Cash
      state_class: measurement
      value_template: "{{ (value_json['string']['#text'] | from_json )['Jackpot']['NextCashValue'] }}"

Thanks for sharing your Powerball integration! It sounds like a great setup you have going on. I especially like the idea of getting notifications if you win - that must be really exciting. I’m not much of a gambler myself, but I have been using a platform called UFABET for my sports betting. It’s been a lot of fun, and I’ve actually won a decent amount of money on there. I’m always looking for new ways to try my luck, so I might have to check out Powerball too. Thanks again for sharing your setup!

I can imagine how frustrating it must be to not have access to the lottery in your own state, but it’s awesome that you’ve found a workaround. Your setup sounds really impressive - I love the idea of getting notifications if you win! It’s really cool that you were able to find a custom component to parse the JSON response properly. I’ve dabbled in APIs before, and I know that can be a bit of a challenge sometimes. Speaking of gambling, have you ever checked out w88? It’s a really popular online gambling platform that has a ton of games to choose from. They even have a sportsbook if you’re into that kind of thing. I think you might enjoy it!

Can someone verify this method still works or not?
I get.
Configuration warnings:
Platform error ‘sensor’ from integration ‘jsonrest’ - Integration ‘jsonrest’ not found.

I read that it maybe because of the new Restful integration?
If so anyone know how to adapt the above to the new Restful method?
This is the only method I can find.

No, it doesn’t work anymore. Powerball turned off the public API that was used to retrieve the numbers from.

Ok thanks for the answer. Anyone using scrape?

I tried but i couldnt make it work. I got an api from downtack.com and they copied the format from powerball.com

Hi Ramon, Welcome. Help me figure this out. did you use examples from downtack.com or do you pay $50.00 a month to get powerball numbers from there api?

Sorry for taking this long to reply. they gave me a free plan and told me how to create another script