Showing Home/Away Bade on Presence Binary Sensor

Hi all. I’m trying to have a sensor show the text HOME/AWAY based on the state of a bayesian binary sensor the same way that my trackers will do, using a badge. The bayesian filter itself is working fine (so far as I can tell) but I’ve been unable to get a sensor set up that will use the result of that to show an icon that includes the text HOME/AWAY.

Below is a screenshot of what I have currently.

37%20pm

The tracker ‘Daniels Tile’ is a straight up tracker and works fine and shows what I’m looking for with that ‘HOME’ text in the red bubble. The other two badges are firstly the Bayesian filter, which I’d eventually hide, and a binary_sensor with device_class of presence. It’s my understanding a templated binary_sensor is the way to go for this having done some reading and no luck adding the device_class to the bayesian sensor itself.

My binary_sensor.yaml file contains the following for those two relevant sensors

- platform: 'bayesian'
  name: 'richard_presence'
  device_class: presence
  prior: 0.65
  probability_threshold: 0.90
  observations:
    - platform: 'state'
      entity_id: 'device_tracker.richards_iphone'
      prob_given_true: 0.8
      prob_given_false: 0.4
      to_state: 'home'
    - platform: 'state'
      entity_id: 'device_tracker.richard_s_iphone'
      prob_given_true: 0.7
      prob_given_false: 0.2
      to_state: 'home'
    - platform: 'state'
      entity_id: 'binary_sensor.home_away'
      prob_given_true: 0.85
      prob_given_false: 0.3
      to_state: 'off'
- platform: 'template'
  sensors:
    presence_ui_richard:
      friendly_name: "Richard"
      device_class: presence
      value_template: >-
        {{ is_state('binary_sensor.richard_presence', 'on') }}

both sensors are showing a state of ‘on’ for the object and if the badge on the UI is clicked they display as ‘home’, which I believe is correct for these sensors. Just the UI doesn’t have the red badge with the word ‘Home’ I’d like to see which I was hoping “device_class: presence” would do. The reason for wanting that is I’d like to replace the icon of the house with a photo of the person the bayesian filter applies to, at which point having the text home or away will help identify the state.

Worth noting when I am away the Icon itself changes to the away house icon, again without that text I’m hoping to see on the badge. So presence is being detected, just I cant get the UI to display as I’d like.

Any advice would be much appreciated.

You won’t be able to get that without it being a device_tracker device. Your’s is a binary sensor. You may be able to get what you want using MQTT and publishing the result to a topic. Then use this component to get it back as a device_tracker, which will add the word ‘home’, and ‘away’.

1 Like

Thanks for the response. I had been using this link as a guide but I guess its not doable or I’ve misinterpreted what was happening there?

I’ll have a look at the MQTT or if that all becomes too much I guess I can just use an templated image with the ‘Home’ or ‘Away’ text as part of the image, or I can change the colour of the outer ring based on state.

There are many types of devices in home assistant. These types are called ‘domains’. The guide you are following is creating a device in the binary_sensor domain. This device has a typical behavior in the UI. That behavior differs from other domains. The domain that displays the words ‘home’ and ‘away’ on a badge is the device_tracker domain. Do you now see the problem? No matter what you do with that guide, you are creating a binary_sensor device and it will not display home/away on the badge because it is not a device_tracker device.

Your solution is to somehow create a device tracker device. One solution would be to create a MQTT device tracker device and an automation. The automation would watch your binary_sensor, when the state of the binary sensor changes, it would send that change to the MQTT device_tracker topic. This in turn would update the device_tracker. Then in the ui, only show the device tracker and ‘hide’ the binary sensor. Does that make more sense?

1 Like

Thanks and yes, it makes sense. The hope had been that the UI behaviour for a binary_sensor with a ‘presence’ class would be similar to a device_tracker but thats clearly not the case. The ‘presence’ class had some UI impact in that the default icon became a house that would change state, but no text badge like I’d wanted. Given your excellent lead I’ve explored the MQTT option and it looks to be working as I’d like.

45%20am

Given I’d seen the question posted previously, codes as follows:

binary_sensor.yaml

- platform: 'bayesian'
  name: 'richard_presence'
  device_class: presence
  prior: 0.65
  probability_threshold: 0.90
  observations:
    - platform: 'state'
      entity_id: 'device_tracker.richards_iphone'
      prob_given_true: 0.8
      prob_given_false: 0.4
      to_state: 'home'
    - platform: 'state'
      entity_id: 'device_tracker.richard_s_iphone'
      prob_given_true: 0.7
      prob_given_false: 0.2
      to_state: 'home'
    - platform: 'state'
      entity_id: 'binary_sensor.home_away'
      prob_given_true: 0.85
      prob_given_false: 0.3
      to_state: 'off'

automations.yaml (below the HA Start trigger seemed to help with things getting out of whack just for initial startup)

  - id: "Richards Presence Updated"
    alias: "Richards Presence Updated"
    initial_state: true
    trigger:
      - platform: state
        entity_id: binary_sensor.richard_presence
      - platform: homeassistant
        event: start
    action:
    - service: mqtt.publish
      data_template:
        topic: "presence/richard"
        payload: >-
          {% if is_state("binary_sensor.richard_presence", "on") %}
            home
          {% else %}
            not_home
          {% endif %}

device_tracker.yaml

- platform: mqtt
  devices:
      richards_presence: "presence/richard"

Played around with getting an MQTT broker set up above which also seemed to be a necessary step. Something new to me but hey, least I have it now. And after the device was then added to known_devices, I set the ‘Picture’ and ‘Name’ to fit the UI

known_devices.yaml

richards_presence:
  hide_if_away: false
  icon:
  mac:
  name: Richard
  picture: /local/images/richard.png
  track: true

Unsure if its as clean as it could be, and I can certainly do with a few entity renames as that Bayesian observation list is hard to follow, but happy with the result.

Thanks Petro for the help.

6 Likes