<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def dynamic_presence(self):
 """ Determine presence based on bed heating level and end presence time reported by the api. Idea originated from Alex Lee Yuk Cheung SmartThings Code. """


        # self.heating_stats()

        if not self.presence:
            if self.heating_level > 50:
                # Can likely make this better
                if not self.now_heating:
                    self.presence = True
                elif self.heating_level - self.target_heating_level >= 8:
                    self.presence = True
            elif self.heating_level > 25:
                # Catch rising edge
                if self.past_heating_level(0) - self.past_heating_level(1) >= 2 \
                    and self.past_heating_level(1) - self.past_heating_level(2) >= 2 \
                        and self.past_heating_level(2) - self.past_heating_level(3) >= 2:
                    # Values are increasing so we are likely in bed
                    if not self.now_heating:
                        self.presence = True
                    elif self.heating_level - self.target_heating_level >= 8:
                        self.presence = True

        elif self.presence:
            if self.heating_level <= 15:
                # Failsafe, very slow
                self.presence = False
            elif self.heating_level < 50:
                if self.past_heating_level(0) - self.past_heating_level(1) < 0 \
                    and self.past_heating_level(1) - self.past_heating_level(2) < 0 \
                        and self.past_heating_level(2) - self.past_heating_level(3) < 0:
                    # Values are decreasing so we are likely out of bed
                    self.presence = False

        # Last seen can lag real-time by up to 35min so this is
        # mostly a backup to using the heat values.
        # seen_delta = datetime.fromtimestamp(time.time()) \
        #     - datetime.strptime(self.last_seen, '%Y-%m-%dT%H:%M:%S')
        # _LOGGER.debug('%s Last seen time delta: %s', self.side,
        #               seen_delta.total_seconds())
        # if self.presence and seen_delta.total_seconds() > 2100:
        #     self.presence = False

        _LOGGER.debug('%s Presence Results: %s', self.side, self.presence)