Badge number updated dynamically

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

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

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

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)?

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

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

Opened a bug report for this.

1 Like

Since 0.88.0 it appears to work
This works for me:

  action:
    - service: notify.ios_lolos_iphone
      data_template:
        message: 'The alarm has not been armed !'
        data:
          push:
            badge: '{{states("counter.iosbadgecount")}}'
            category: "armalarm"
          action_data:
            entity_id: alarm_control_panel.alarm

Hey,
Old discussion I know but this is not working anymore : I got the following error in logs : Value type for APS key [badge] is a number.

Any ideas ?

Sorry mate I don’t use badges anymore so no idea if this still works…

Wanted an easy way to manage badge number, just increase or decrement it. While searching I came across this topic so I’ll share my script in here if others would need something similar.

I created a helper input number element: input_number.app_badge_counter
Any automation or script that wants to edit the app badge counter, will just need to call one of these services to increase or decrement the badge counter. No notification will be shown by it, so each script can still send it’s own custom notification.

increase_app_badge_counter:
  alias: Increase app badge counter
  sequence:
  - service: input_number.increment
    data: {}
    entity_id: input_number.app_badge_counter
  - service: notify.mobile_app_iphone
    data:
      message: delete_alert
      data:
        push:
          badge: '{{ states.input_number.app_badge_counter.state }}'
  mode: single
  icon: mdi:counter
decrement_app_badge_counter:
  alias: Decrement app badge counter
  sequence:
  - service: input_number.decrement
    data: {}
    entity_id: input_number.app_badge_counter
  - service: notify.mobile_app_iphone
    data:
      message: delete_alert
      data:
        push:
          badge: '{{ states.input_number.app_badge_counter.state }}'
  mode: single
  icon: mdi:counter
1 Like

Is is somehow possible in the meantime (without a helper)? So simple +1 or -1 as in this wish?

1 Like

has anyone made this work?

The “problem” here is that push notifications do not work like this on iOS.

The server (Home Assistant) must know how many push notifications are still open. This applies to all apps in the App Store.

Hi!
I used this in my automations, to have the badge increase +1 every time the automation is run.
The problem is that this counter is never reset, so if the badge is 3, I open the app the badge disappears, at the next notification the badge is 4 and not 1

action:
  - service: counter.increment
    data: {}
    target:
      entity_id: counter.app_badge_counter
  - service: notify.mobile_app_inanu
    data:
      title: Home Assistant
      message: Test Badge
      data:
        push:
          badge: "{{ states.counter.app_badge_counter.state }}"
mode: single

You need to reset badge to 0 using service call. So most likely it is not possible to reset counter just by opening application. One of the reasons I also stopped to use these.

I assumed it was like this. The ideal would have been to be able to reset the counter when starting the Companion app

I was seeing this as well this morning and figured it out: once I changed my helper from template to input_number, everything worked great in the following form:

service: notify.mobile_app_name
metadata: {}
data_template:
  data:
    push:
      badge: '{{states.input_number.xyz.state}}'
  message: delete_alert