Use HA state in ESPHome

I’m struggling with the logic on this so would appreciate some help, please.

I have a device tracker group in HA called group.residents (so it returns home or not_home) and want to use the state of that as a binary sensor in ESPHome to enable or disable the heating based on presence and to use in lambda to display differing text on the small display.

I’ve tried multiple different ways and failed each time - what is the best way to do this, please?

I seem to have it working but am pretty sure there is a more elegant way so I’m really keen for comments and feedback, please!

In HASS configuration.yaml I have:

binary_sensor:
# This binary sensor to indicate whether anyone is home - used with group.residents
  - platform: template
    sensors:
      someone_home:
        value_template: "{{ is_state('group.residents', 'home') }}"

then is ESPHome, I have:

binary_sensor:
  - platform: homeassistant
    id: "occupied"
    entity_id: binary_sensor.someone_home

And use it in the lambda like this:

          if (id(occupied).state) {
            // Binary sensor is ON, do something here
            it.print(160, 85, id(my_font_medium_small), id(my_text_color), TextAlign::CENTER, "HOME"); //occupied

          } else {
            // Binary sensor is OFF, do something else here
            it.print(160, 85, id(my_font_medium_small), id(my_text_color), TextAlign::CENTER, "OUT"); //occupied
          }

Is there a better way to do this?

Thanks.