How to start only HVAC of certain rooms based on presences?

Hi everyone, i managed to write a bunch of automations to domotize my HVACs and start them based on the lowest one and the production from my photovoltaic system.

At the moment i have a template sensor that tells which HVAC (between the ones that are still off) is the lowest, and so the automation picks that to start a new HVAC when there’s enough solar production, here is the sensor:

- platform: template
  sensors:
    pdc_da_avviare:
        unique_id: pdc_da_avviare
        friendly_name: 'Prossima PDC da avviare'
        value_template: >
            {%- set d = [
            {'unit': 'climate.daikin_soggiorno',
            'state': states('climate.daikin_soggiorno'),
            'temp': states('sensor.temperatura_pt') | float(100)},
            {'unit':'climate.daikin_camera_matrimoniale',
            'state': states('climate.daikin_camera_matrimoniale'),
            'temp': states('sensor.temperatura_camera') | float(100)},
            {'unit':'climate.daikin_camera_1',
            'state': states('climate.daikin_camera_1'),
            'temp': states('sensor.temperatura_camera_1')|float(100)},
            {'unit':'climate.daikin_camera_2',
            'state': states('climate.daikin_camera_2'),
            'temp': states('sensor.temperatura_camera_2')|float(100)},
            {'unit':'climate.daikin_camera_3',
            'state': states('climate.daikin_camera_3'),
            'temp': states('sensor.temperatura_camera_3')|float(100)}
            ] %}
            
            {{ (d | selectattr('state', 'eq', 'off') | sort(attribute = 'temp') | first)['unit'] }}

I would like to modify that based on presences so that the HVAC is started based on:

  • The person in that room is at home OR
  • That room’s temperature is below 16.5°C

How can i manage to do that?

Thanks

NOTE: If you need to check other files to understand how it works here’s the github with all the files, it’s not 100% up-to-date but i haven’t changed much