I want to show how many person are at home on the icon badge.
What is the best way to achieve this.
In other systems I would use something like "IF xxx home and yyy home THEN badge=2, IF xxx home and yyy not home THEN badge=1 and so on but I dont know if IF THEN is allowed in HA.
Try something like this:
sensor:
- platform: template
sensors:
people_home_count:
entity_id:
- device_tracker.person_1
- device_tracker.person_2
- device_tracker.person_3
value_template: >-
{% set count = 0 %}
{% if is_state("device_tracker.person_1", "home") %}
{% set count = count + 1 %}
{% endif %}
{% if is_state("device_tracker.person_2", "home") %}
{% set count = count + 1 %}
{% endif %}
{% if is_state("device_tracker.person_3", "home") %}
{% set count = count + 1 %}
{% endif %}
{{count}}
I have tried it out with my device trackers but my log returns the following errors, but I am not quite sure what they mean.
2017-12-17 15:08:12 ERROR (Thread-2) [homeassistant.util.yaml] while scanning for the next token
found character '%' that cannot start any token
in "/home/homeassistant/.homeassistant/sensors.yaml", line 45, column 12
2017-12-17 15:08:12 ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/home/homeassistant/.homeassistant/sensors.yaml", line 45, column 12
@jeppper Sorry about that, I wrote it at work so I didn’t get to test and yes I did have an error. Try it again, I edited the above post with the corrections.
no problem at all I am glad for your help.
I still get an error though. Is “>-” after value_template correct?
2017-12-17 17:09:06 ERROR (MainThread) [homeassistant.config] Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['people_home_count']['value_template']. Got '{% set count = 0 %} {% if is_state("device_tracker.xxxxx", "home" %}\n {% set count = count + 1 %}\n{% endif %} {% if is_state("device_tracker.xxxxx", "home" %}\n {% set count = count + 1 %}\n{% endif %} {{count}}'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
@jeppper Copy the code again and then change the device_tracker. There were a couple of errors in my script (but I did correct it above) one of them was I forgot to close the brackets on the if statement lines, and I see from your error you still have that missing. You have:
{% if is_state(“device_tracker.person_1”, “home” %}
when it needs to be this:
{% if is_state(“device_tracker.person_1”, “home”) %}
so just delete what you have and paste the above script and change your device_trackers so all my bad code is gone
that did the trick Now no errors shows up anymore and I get a badge with number of persons at home. I will test it more tomorrow when persons enter/leave the house.
Thanks very much for helping for now
I have now tried it out for some days and it works perfectly!!
Anyhow I would like to use this “Count” result for showing on the iOS badge. Do you maybe know how?
Not exactly sure. I know you can set the badge with a push notification but unsure if it’s incremental or if it’s just set with the notification.
I have tried the following configuarion but nothing happens.
action:
service: notify.ios_jespersiphone
data:
title: "Home"
message: "xxx arrives"
data_template:
push:
badge: "{{ states.sensor.people_home_count.state }}"
@jeppper I tried a few things as well, it looks like it doesn’t accept data_templates. As a workaround you could have multiple automations and a condition for each 1, 2, 3, 4 etc. people home and trigger the appropriate badge number. Not ideal, but it should work until templating is added.
I have tried to make multiple automations and it seems to Work
Please feel free to comment.
- alias: Notify iOS when 2 to 1 home
trigger:
- platform: state
entity_id: sensor.people_home_count
from: '2'
to: '1'
action:
service: notify.ios_jespersiphone
data:
title: "Home"
message: "Someone leaving Home"
data:
push:
badge: 1
sound: "solemn.mp3"
#------------------------------------------------------------
- alias: Notify iOS when 2 at home
trigger:
- platform: state
entity_id: sensor.people_home_count
to: '2'
action:
service: notify.ios_jespersiphone
data:
title: "Home"
message: "Everybody home"
data:
push:
badge: 2
sound: "US-EN-Morgan-Freeman-Someone-Is-Arriving.wav"
#------------------------------------------------------------
- alias: Notify iOS when 0 to 1 at home
trigger:
- platform: state
entity_id: sensor.people_home_count
from: '0'
to: '1'
action:
service: notify.ios_jespersiphone
data:
title: "Home"
message: "Someone entering Home"
data:
push:
badge: 1
sound: "US-EN-Morgan-Freeman-Someone-Is-Arriving.wav"
#------------------------------------------------------------
- alias: Notify iOS when 0 at home
trigger:
- platform: state
entity_id: sensor.people_home_count
to: '0'
action:
service: notify.ios_jespersiphone
data:
title: "Home"
message: "Everybody left Home"
data:
push:
badge: 0
sound: "solemn.mp3"
Opened a bug report for the bug where you cant use a template to set the badge #
A fix has just been rolled out server side to coerce badge to int.
Awesome thank you.
Is it now possible to use a template to set the badge?
@jeppper I just tried it and it works!! Thanks @robbiet480
- alias: 'Send Templated App Badge'
initial_state: on
trigger:
- platform: state
entity_id: input_number.testing_app_badge
action:
- service: notify.ios_jeremys_iphone
data_template:
message: "Testing"
data:
push:
badge: '{{states.input_number.testing_app_badge.state|int}}'
Yes now it Works!!
Now it would just be perfect if it would be possible to have the numbers on the badge showing without the need of a message.
former post “badge not working” in iOS category