Badge number updated dynamically


#1

Hey there,

I’m trying to update the badge number dynmically in a push notification and I’m a little stumped.

I’m using the value from an input_number. Here is the line that is giving me the issue:

badge: '{{ (states.input_number.slider1.state | int) }}'

I’m receiving the following error:

WARNING (MainThread) [homeassistant.helpers.condition] Value cannot be processed as a number: unknown

Any help would be greatly appreciated.

Cheers


#2

Did you ever find out how to do this? I’m looking to do the same.
Thanks


#3

Hey there,

I did some digging around and based on my limited knowledge, you can’t use a variable number in the badge section. Depending on how long the variable is, you can create a different push notification for each number. In my case, the number will theoretically never pass more than 6 or so.

Here is my example configuration I’ve fudged together so far:

  one_beer:
    alias: One Beer
    sequence:
      - condition: numeric_state
        entity_id: input_number.beer_today
        below: 2
      - service: notify.iosapp_benjamins_iphone
        data:
          message: "{{ states.sensor.beer_today.state }} Beer Today"
          data:
            attachment:
              url: https://iambendick.com/counter/img/push/IMG_2020.JPG
              hide-thumbnail: false
            push:
              sound: "US-EN-Morgan-Freeman-Turning-On-The-Bar-Lights.wav"
              badge: 1
              category: "beer"
            action_data: # Anything passed in action_data will get echoed back to Home Assistant.
              entity_id: light.test
              my_custom_data: foo_bar
          
  two_beer:
    alias: Two Beer
    sequence:
      - condition: numeric_state
        entity_id: input_number.beer_today
        above: 1
        below: 3
      - service: notify.iosapp_benjamins_iphone
        data:
          message: "{{ states.sensor.beer_today.state }} Beer Today"
          data:
            attachment:
              url: https://iambendick.com/counter/img/push/IMG_2020.JPG
              hide-thumbnail: false
            push:
              sound: "US-EN-Morgan-Freeman-Turning-On-The-Bar-Lights.wav"
              badge: 2
              category: "beer"
            action_data: # Anything passed in action_data will get echoed back to Home Assistant.
              entity_id: light.test
              my_custom_data: foo_bar

#4

Thanks,

I actually spent time on this last night and I can almost get HA to work. Below is my config:

  action:
    - service: counter.increment
      entity_id: counter.iosbadgecount
    - service: notify.ios_lolos_iphone
      data_template:
        message: 'Message goes here'
        data:
          push:
            badge: '{{states.counter.iosbadgecount.state}}'

the key is to use data_template instead of data

I can see this in the data_event for the call_service:

{"service_data": {"data": {"action_data": {"entity_id": "alert.staircase_led_strip_lost"}, "push": {"category": "alert", "badge": "1"}}, "message": "Staircase led strip lost"}, "domain": "notify", "service": "ios_lolos_iphone", "service_call_id": "140065414224640-59"}

so the counter.iosbadgecount number is correctly sent, but the app doesn’t react to this and doesn’t show any badge number…
Now interestingly if I replace my courtner with a number, it doesn’t work either since I changed the first data into data_template.

@robbiet480 am I doing something that’s unsupported here or is it a bug?
also, is it possible to send a “silent” notification to the app to reset the badge (so I don’t have to dismiss a notification on my phone)?


#5

Reviving an old topic.

Did you ever get an answer to this? I am trying to do exactly the same thing and it is not working. Interestingly if I create a really basic script to send an IOS notification with a badge of 1:-

test_push:
  alias: Test Push
  sequence:
  - service: notify.ios_my_iphone
    data:
      message: "Test Push"
      data:
        push:
          badge: 1

I get a badge successfully. If I change data to data_template and nothing else, the push notification comes through, but no badge

test_push:
  alias: Test Push
  sequence:
  - service: notify.ios_my_iphone
    data_template:
      message: "Test Push"
      data:
        push:
          badge: 1

Would be very handy to be able to template up that badge counter, and also to be able to just put something like badge: +1 or badge: -1


#6

Not looked into it since my last post and at that time I could not programmatically set the badge number so I gave up on it