Help with self written helper/sensor

Hello together,
i need a little help with my small „presence“ project.
My idea:
I have a zigbee button in my house and i want to use it to change the state of presence in my house.
So one push should change between two states (no one at home / someone at home). With this state i want to work in further automatisations together with for example door sensors and motion sensors.
As far as i have understand it, i need to write a helper/sensor which changes his state by pressing the zigbee button. But i have no idea how the sensor/helper have to look like in yaml. In my opinion it should just be a small code.
Can anyone help me with the code and maybe a short explanation to it?
Thanks in advance!

Hi,
I think what you want to create is an Input Boolean helper (or Toggle as it’s known in the UI). Do that via Settings\Devices & Services\Helpers. No yaml code needed.
Then your automation can have a trigger based on the zigbee button press & the action will be to turn on the new helper.
Hope that helps for you.
Cheers

Rather than an automation and input boolean you can use a triggered binary template sensor:

# configuration.yaml

template:
  - trigger: # can add more triggers later
     trigger: state
     entity_id: button.zigbee_button
     not_to:
       - unknown
       - unavailable
    conditions: [] # for later if needed
    binary_sensor:
      - name: House Presence
        device_class: presence
        state: "{{ not states('binary_sensor.house_presence')|bool(true) }}"

Your button will toggle the state of this binary sensor every time it is pressed.
You can add more triggers and conditions later as per your request.

Thank you both for your help.
I will try both solutions tomorrow and see what works better for me.
Just a question to this template sensor:
Where are the two different states in this code? What is the part „not_to“ for?

Trank you!

It will trigger every time the button’s state changes (a button’s state is the date and time it was pressed). If the button state becomes unavailable or unknown for some reason it will not trigger because of the not_to.

Ok but how can i use this state for my automatisation?
For example i want to make an automation like this:
If the door sensor changes to open and the state of the helper is „not at home“ HA should send a message. If the state of the door sensor changes to open and the state of the helper is „at home“ there is no need for sending a message. So in my opinion i need two different states for my helper. Everytime the button gets pressed the state must change. If the state is the date and time of the button press i don‘t know how to use this for my further automation?
Thanks for your help so far!

You don’t use the button state in your automation, you use the template binary sensor state.

The template binary sensor does switch state from home/on to away/off whenever you press the button.

So in an automation:

triggers:
  - trigger: state
    entity_id: binary_sensor.door
    from: 'off'
    to: 'on' # door opens
conditions:
  - condition: state
    entity_id: binary_sensor.house_presence
    state: 'off' # and you are away
actions:
  action: <send your message>

Ok thank you for the explanation and the code!
I will try it as soon as my button will be delivered (i hope today).