How to convert HAstate to KNX friendly state 0/1

Hi. I’m quite new to HA. I have an integration with HA / KNX and Ubiquity. My presence detection is set up but it leaves to states. Home /not_home. I would like to create a second state that converts home to 1 and not_home to 0. This then needs to be sent back to the KNX system.
What do I do?

Thanks in advance

Br.

Christian

You will need to do a few steps, first of read the links in this posts before blindly copying the code so you’ll know what it does.

Create a binary_sensor with template so you can set how it reacts to the state changes.

template:
  - binary_sensor:
    - name: "tracker_phone_a"
      state: >-
        {% if states('device_tracker.unifi_XXXXXXXXXX') == 'home' %}
          {{ 1 }}
        {% elif  states('device_tracker.unifi_XXXXXXXXXX') == 'not_home' %}
          {{ 0 }}
        {% endif %}

This will create the sensor Tracker of phone A which you can then expose in KNX
You can name it whats handy for you and ofc replace the XXXXXXX with whatever Unifi created.
I use the 2 checks for home and not_home to filter out any other state if your using zones.
If you just use those 2 then 1 line would suffise:

template:
  - binary_sensor:
    - name: "tracker_phone_a"
      state: "{{ iif (is_state('device_tracker.unifi_XXXXXXXXXXj', 'home'), 1, 0) }}"

In your configuration.yaml (or split file) you put the expose:

knx:
  expose:
    - type: binary
      entity_id: binary_sensor.tracker_phone_a
      address: "1/0/1"

This wil also make it readable from KNX.
Note that the entity_id starts with binary_sensor. after that you put in the name as created in the template.

As far as i know expose does not support templating so you will need todo these 2 steps.
If not i’m sure someone will pop in to correct me.

An alternative approach would be an automation using a state trigger for home/not_home and the knx.send service as action.
It would require extra work to be readable from the bus though (respond to GroupValueRead), so if this is needed I’d go for Charles’ solution.