Help with Accurate Runtime Display for AC Smart Plug Card

Hello,
I’m creating a custom card for my AC unit, which is connected to a Zigbee smart plug.


I need help with the secondary information display. It should show:

  • Power consumption in Watts
  • Runtime (when on) or time since last use (when off)
  • Considers >10W as “on” and ≤10W as “off” due to occasional smart plug update issues

The problem is that the time display resets with every power consumption update, rather than showing the cumulative time since the last state change (on/off). I want:

  • When on: Display time elapsed since power exceeded 10W
  • When off: Display time elapsed since power dropped below 10W

I assume my current attempt is massively overcomplicated. How can I fix this issue?
Here is the YAML code I have so far:

  - type: custom:mushroom-template-card
    entity: sensor.zigbee_plug_ac_power
    icon: |
      {% if states('sensor.zigbee_plug_ac_power')|float > 10 %}
        mdi:fan
      {% else %}
        mdi:fan-off
      {% endif %}
    icon_color: |
      {% if states('sensor.zigbee_plug_ac_power')|float > 10 %}
        green
      {% else %}
        gray
      {% endif %}
    primary: Air Conditioner
    secondary: >
      {% set power = states('sensor.zigbee_plug_ac_power')|float %} {% set
      current_time = now().timestamp() | int %} {% if power > 10 %}
        {% if not state_attr('sensor.zigbee_plug_ac_power', 'last_active') %}
          {% set start_time = current_time | int(0) %}
        {% else %}
          {% set start_time = state_attr('sensor.zigbee_plug_ac_power', 'last_active') | int(0) %}
        {% endif %}
      {% else %}
        {% set start_time = states.sensor.zigbee_plug_ac_power.last_changed.timestamp() | int %}
      {% endif %} {% set time_diff = current_time - start_time %} {% set minutes
      = (time_diff / 60) | int %} {% set hours = (minutes / 60) | int %} {% set
      remaining_minutes = minutes % 60 %} {% if power > 10 %}
        Active: {{ power|round(1) }} W (Since {% if hours > 0 %}{{ hours }}h {% endif %}{{ remaining_minutes }}m)
      {% else %}
        Turned Off (Since {% if hours > 0 %}{{ hours }}h {% endif %}{{ remaining_minutes }}m ago)
      {% endif %}
    tap_action:
      action: more-info
    hold_action:
      action: none
    double_tap_action:
      action: none
    card_mod:
      style:
        mushroom-shape-icon$: |
          .shape {
            {% if states('sensor.zigbee_plug_ac_power')|float > 10 %}
              --shape-animation: spin 1s linear infinite;
            {% else %}
              --shape-animation: none;
            {% endif %}
            display: flex;
          }
          .shape::before {
            animation: var(--shape-animation);
          }
        .: |
          @keyframes spin {
            from {
              transform: rotate(0deg);
            }
            to {
              transform: rotate(360deg);
            }
          }
    state_display: |
      {{
        states('sensor.zigbee_plug_ac_power') | float > 10
          and states('sensor.zigbee_plug_ac_power')
          or 'Off'
      }}
    extra_attributes:
      last_active: |
        {% if states('sensor.zigbee_plug_ac_power') | float > 10 %}
          {{ now().timestamp() | int }}
        {% else %}
          {{ state_attr('sensor.zigbee_plug_ac_power', 'last_active') }}
        {% endif %}