Thermostat controller change relay during heating

Hello guys,

I would like to ask for help with thermostat climate controller component. During the heating operation I would like to continously check a sensor (input pin) and change which switch should be turned on/off.

This is my sensor:

binary_sensor:
  - platform: gpio
    pin: 17
    id: is_night_power_available

And this is the thermostat component :

climate:
  - platform: thermostat
    name: "Szoba 1"
    sensor: room1_temp1
    id: room1_heater
    default_target_temperature_low: 20 °C
    min_heating_off_time: 30s
    min_heating_run_time: 30s
    min_idle_time: 3s
    heat_action:
      then:
        - if:
            condition:
              - binary_sensor.is_on: is_night_power_available
            then:
              - switch.turn_off: room1_relay_day
              - delay: 2s
              - switch.turn_on: room1_relay_night
            else:
              - switch.turn_off: room1_relay_night
              - delay: 2s
              - switch.turn_on: room1_relay_day
              
    idle_action:
      - switch.turn_off: room1_relay_night  
      - switch.turn_off: room1_relay_day    

So based on the sensor value when the heating starts it activates relay_day or relay_night, which is working fine.
But it can happen that during heating the sensor changes its value…So how can I react to it? I must run again the heat_action section, but it only runs once it starts heating.

Thanks for help in advance!

Put the automation under the binary sensor. When the state changes it will evaluate the condition and take action.

binary_sensor:
  - platform: gpio
    pin: 17
    id: is_night_power_available
    on_state:
      then:
        - if:
            condition:
              - binary_sensor.is_on: is_night_power_available
            then:
              - switch.turn_off: room1_relay_day
              - delay: 2s
              - switch.turn_on: room1_relay_night
            else:
              - switch.turn_off: room1_relay_night
              - delay: 2s
              - switch.turn_on: room1_relay_day

Thank you very much!
I think it will solve the issue, my final code is : (I also need to check if thermostat is in the heating mode)

  - platform: gpio
    pin: 17
    id: is_night_power_available
    on_state:
      then:
        - if:
            condition:
              and:
                - lambda: 'return id(room1_heater).mode == "CLIMATE_MODE_HEAT";'
                - binary_sensor.is_on: is_night_power_available
            then:
              - switch.turn_off: room1_relay_day
              - delay: 2s
              - switch.turn_on: room1_relay_night
            else:
              - switch.turn_off: room1_relay_night
              - delay: 2s
              - switch.turn_on: room1_relay_day  
        - if:
            condition:
              and:
                - lambda: 'return id(room2_heater).mode == "CLIMATE_MODE_HEAT";'
                - binary_sensor.is_on: is_night_power_available
            then:
              - switch.turn_off: room2_relay_day
              - delay: 2s
              - switch.turn_on: room2_relay_night
            else:
              - switch.turn_off: room2_relay_night
              - delay: 2s
              - switch.turn_on: room2_relay_day

For both of the thermostats.

I will check it out after work.